PowerCLI “Oneliners”

Get ESXi hosts versioning:

PS C:\Windows\system32> Get-VMHost | Group-Object -Property version

List VM’s and host details

The other day there were some issues with the storage system of our management environment at our secondary site and we had to do some onsite troubleshooting. As a precaution we turned off the running VM’s, before doing this we wanted to know where the VM’s resides on which ESXi node with some extra info that could be useful in a troubleshooting scenario.

To achieve this I used the following PowerCLI script:

Get-VM |
Select Name,PowerState,
    @{N='VMHost';E={$_.VMHost.Name}},
    @{N='IP';E={$_.Guest.IPAddress -join '|'}}

This will give you an overview of the “running” VM’s on the corresponding ESXi node and could be useful if you want to locate your vCenter VM for instance.

To shutdown all VM’s in an automated fashion I used the following script:

Get-Cluster OMS-Alm | Get-VM | Shutdown-VMGuest -Confirm:$false -RunAync:$true

If you want to exclude the vCenter from shutting down then use:

Get-Cluster OMS-Alm | Get-VM | ?{$_.Name -ne "vCenterName"} | Shutdown-VMGuest -Confirm:$false -RunAync:$true

To view the results in a separate window add “Out-Gridview” to the script

List vlan-id’s

To prepare for an upcoming change to Upgrade one of our vCenters from version 6.0 (Windows based) to 6.7U2 I needed to collect some Datacenter object information. In this case it was a complete list of the vlan-id’s within this particular vCenter.

To achieve this you can use the following script:

Get-Cluster | Get-VMHost | Foreach-Object {$strClusterName = $_.Parent.Name; Get-VirtualPortGroup $_ | Select-Object @{N="Cluster";E={$strClusterName}},Name,VLanId}

Get CDP info per VMnic

Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} |
%{Get-View $_.ID} |
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
%{ foreach($physnic in $_.NetworkInfo.Pnic){
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
foreach($hint in $pnicInfo){
Write-Host $esxname $physnic.Device
if( $hint.ConnectedSwitchPort ) {
$hint.ConnectedSwitchPort
}
else {
Write-Host "No CDP information available."; Write-Host
}
}
}
}