Summary
This is a list of the short PowerCLI scripts I’ve found useful.
Hosts
Comparing Standard Switches between Hosts
Change $host1 and $host2 to appropriate values and this one-liner will show any switches that are on $host1 but not $host2. This will not show what is on $host2 but not $host1.
1 |
Get-VirtualPortGroup -VMHost $host1 -Standard | ? { $pg1 = $_; return ((Get-VirtualPortGroup -VMHost $host2 -Standard | ? { $_.Name -eq $pg1.Name }).Count -eq 0) } |
List of Hardware models (and number in use) across multiple vCenters
Given the array $vctrs, which contains a list of vCenter servers to examine, this script will collate all the hardware models in use and provide a tally of how many you have.
1 |
$models = @(); $vctrs | % { $vs = Connect-VIServer $_ -Credential $cred; $results = Get-VMHost -Server $vs | Group Model | Select Name, Count; Disconnect-VIServer -Server $vs -Confirm:$False; Write-Host $_; $results | ft; $results | % { $item = $_; $found = $false; $models | ? { $_.Name -eq $item.Name } | % { $_.Count += $item.Count; $found = $true; }; if ($found -eq $false) { $models += $item; } } }; $models | ft Name, Count |
VM
Autoupgrade Tools
Getting a list of VMs that are set to upgrade VMware Tools when the VM is restarted. Conversely, you can edit the line below to show the VMs that aren’t set to upgrade Tools.
1 |
Get-Vm | ? { $($_ | get-view).Config.Tools.ToolsUpgradePolicy -eq "upgradeAtPowerCycle" } | sort Name | select Name, GuestID |
List of VMs with attached networks
This will get the list of all VMs and the networks that they are connected to.
1 |
Get-VM | select Name, @{L="Networks";E={$_.ExtensionData.Network | % { Get-View "$($_.Type)-$($_.Value)" | Select -ExpandProperty Name}}} |
Trimming a list of VMs according to name
Simple one this is – if you have VMs which have an identifier in them, for example “dc” for domain controller then to search, it is best to convert the VM name to upper/lower case before doing a comparison.
1 |
Get-VM | ? { $_.Name.ToLower() -Like "*dc*" } |
Snapshots
This will show all the VMs in a vCenter that have a snapshot with their corresponding age.
1 |
Get-VM | ? { Get-Snapshot -VM $_ } | % { Get-Snapshot -VM $_ } | select VM, Name, @{L="Age";E={$($(Get-Date) - $_.Created).Days}}, @{L="Size";E={[Math]::round($_.SizeGB, 2);}} | sort Age -Descending |