7 min read
The best PowerCLI commands for admins: VM inventory management (part 1)
Discover the top PowerCLI commands for your everyday admin life – in Part 1 of this series, you'll learn more about...
ScriptRunner Blog
Part 3 of this series provides practical examples for the Get-View command. This is a great way to save time.
PowerCLI is an extremely useful extension to PowerShell designed specifically for managing VMware infrastructures. It allows administrators to perform a wide range of management and automation tasks quickly and efficiently. In this article, we will focus on the Get-View command, which provides a powerful way to access, filter, and sort advanced information about virtual machines (VMs).
The series is composed of the following articles:
The Get-View command is a key element of PowerCLI that provides access to advanced information and functionality in the VMware vSphere API. Unlike the Get-VM command, which is often used for basic administrative tasks, Get-View provides greater performance and flexibility, especially when dealing with complex queries and automation scripts.
An example of how Get-View can be used to quickly access advanced information is the following command:
$vmView = Get-View -ViewType VirtualMachine -Filter @{"Name"="WebServer01"}
This command returns a comprehensive list of information about the VM "MyVM" that is retrieved through the vSphere API.
Get-View is especially useful if you want to get advanced information about your VMs, or if you want to filter and sort this information specifically. For example, you can use Get-View to get the memory (RAM) and CPU usage of all VMs in your environment:
$vmViews = Get-View -ViewType VirtualMachine
foreach ($vmView in $vmViews) {
$vmName = $vmView.Name
$vmMemory = $vmView.Summary.Config.MemorySizeMB
$vmCpuUsage = $vmView.Summary.QuickStats.OverallCpuUsage
Write-Output "VM Name: $vmName"
Write-Output "Memory: $vmMemory"
Write-Output "CPU Usage: $vmCpuUsage MHz"
}
This script prints the name, memory, and CPU usage of each VM in your environment.
You can also use Get-View to filter VMs based on certain criteria, such as VMs older than 30 days:
$limitDate = (Get-Date).AddDays(-30)
$allVMs = Get-View -ViewType VirtualMachine
$oldVMs = $allVMs | Where-Object { $_.Config.CreateDate -lt $limitDate }
foreach ($oldVM in $oldVMs) {
Write-Host "Old VM Name: $($oldVM.Name)"
}
Get-View can also be integrated with automation scripts, for example, to generate regular reports or to perform maintenance tasks. You could use Get-View in combination with automation solutions like ScriptRunner to schedule and run regular tasks without manual intervention.
Another practical example of using Get-View is to monitor the state of VM space usage. You could create a script that identifies VMs with a high percentage of thin-provisioned memory in use, and store that information for later review or optimization:
$vmViews = Get-View -ViewType VirtualMachine
$threshold = 80
foreach ($vmView in $vmViews) {
$vmName = $vmView.Name
$vmProvisionedSpace = $vmView.Summary.Storage.Committed
$vmUsedSpace = $vmView.Summary.Storage.Uncommitted
$vmTotalSpace = $vmProvisionedSpace + $vmUsedSpace
if ($vmTotalSpace -ne 0) {
$vmUsedPercentage = ($vmUsedSpace / $vmTotalSpace) * 100
if ($vmUsedPercentage -gt $threshold) {
Write-Output "VM Name: $vmName"
Write-Output "Used Space Percentage: $vmUsedPercentage %"
}
}
}
This script prints the name and percentage of used space of each VM where the threshold of 80% was exceeded.
When using Get-View, you should note that some properties are not available directly in the View object. You might need to retrieve additional objects or call methods to get the information you want. The vSphere API documentation is a helpful resource to learn what properties and methods are available for each object.
Another important aspect is performance optimization. For large environments with many VMs, Get-View queries can be time-consuming. To improve performance, use filters to limit results to VMs relevant to your task. You can also use the -Property option to retrieve only the properties you need, which reduces the amount of data returned. The following code example shows the use of the Property parameter. Only the "Name", "Summary.Storage.Committed" and "Summary.Storage.Uncommitted" properties are retrieved. Using the filter and -Property option reduces the amount of data returned and improves query performance.
$vmNameFilter = "TestVM*"
$requiredProperties = @(
"Name",
"Summary.Storage.Committed",
"Summary.Storage.Uncommitted"
)
$ params = @{
ViewType = 'VirtualMachine'
Filter = @{"Name" = $vmNameFilter}
Property = $requiredProperties
}
$ vmViews = Get-View @params
In a nutshell, the Get-View command in PowerCLI provides administrators with a powerful way to access and efficiently manage advanced information about their VMware infrastructures. By combining Get-View with automation scripts and solutions such as ScriptRunner, administrators can save time and reduce the effort required to manage their environments.
Watch this session that dives deep into the transformative realm of VMware automation using PowerCLI, all while harnessing the versatile capabilities of ScriptRunner.
If you're looking to elevate your data center management skills, streamline operations, and unlock the power of PowerShell and PowerCLI, this webinar is tailored for you.
You will learn how ScriptRunner helps you to streamline, automate, delegate and monitor all your recurring VMware management tasks.
🎯 Centralized script and module management
💡 Smart scripting with ChatGPT and OpenAI
🔐 Secure credential and permission handling
👍 Flexible and secure delegation with auto-created web forms
⏰ Interactive, scheduled and event-driven script execution
🔎 Comprehensive monitoring and reporting
Request the webinar recording here!
ScriptRunner participated at a VMware User Group webcast, you can request it here:
Managing vSphere environments can be challenging, but with the right automation solution it doesn't have to be. PowerShell and the PowerCLI module are great, but typically only used by scripting experts.
What if you could use PowerShell to not only standardize and automate your recurring management tasks, but also delegate them to help desk teams and even create self-services for end users based on your scripts? This would allow you to empower these users to manage snapshots, reconfigure VMs, stop/start their VMs, or generate infrastracture reports.
Of course, not everyone is comfortable with the command-line, and you certainly don't want to give these users privileged access to your VMware environment.
So what does it take to automate and delegate your recurring tasks efficiently and securely in your organization with PowerShell?
May 4, 2023 by Philip Lorenz
Discover the top PowerCLI commands for your everyday admin life – in Part 1 of this series, you'll learn more about...
Jun 29, 2023 by Philip Lorenz
Part 3 of this series provides practical examples for the Get-View command. This is a great way to save time.
...
May 10, 2022 by Guy Leech
Perhaps it is because I began my professional coding career writing device drivers for Unix systems in C, back in the...
Philip Lorenz works as a DevOps and cloud engineer. In addition to this activity, he holds training courses and creates learning content on the topics of PowerShell, automation and cloud computing.