PowerCLI oneliners

A lot of these bits below are out-takes of relatively simple scripts I have used over time to do tasks in PowerCLI. Use at your own risk as always with scripts you find online. I will generally try to exlain a bit about the script where possible.

Getting Network Domain settings from your esxi-hosts without having to log into every host:

I came across a customer who had +300 ESXi hosts and needed to check why logging information didn’t work and one of the things we found was that not all hosts were in the same domain. The script gets the info and puts it in a CSV file for you.

get-VMHostNetwork * | select -Property VMhost, @{N=’DnsAddress’;E={$_.DNSAddress}}, @N={N=’SearchDomain’;E={$_.SearchDomain}}, @{N=’DomainName’;E={$_.DomainName}}, | Export-CSV DNS-Info.csv


vMotion fails due to CD stuck in drive

How often does this happen? You try to put a server into maintenance mode only to find that it doesn’t happen and when you check the vm has a cd drive (and if you are lucky it is only one). A good practice is not to leave CD’s in the drive. The below script can help with this. Keep in mind it may cause issues where the cd drive is in use (such as timeouts etc). Also since Linux needs to unmount the drive before it can eject it may cause issues.

Get-VM | Get-CDDrive | Where {$_.extensiondata.connectable.connected -eq $true} | Set-CDDrive -NoMedia -confirm:$false

See what VMs were restarted after a HA failure occured and where

It has happened to us all, a host suffers from a hardware failure and you had HA enabled. The VMs are moved to other VMs and restarted. The day after you are confronted with the question:”What VMs were restarted because of the hardware failure?” Below is a script where you can go back as many days as you like, here it is -5 in AddDays(-5) and you can just edit this date and run the script after logging on the vCenter(s). The output is tot he screen and formatted.

Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-5) -Type Warning | Where {$_.FullFormattedMessage -match “restarted”} |select CreatedTime,FullFormattedMessage | sort CreatedTime –Descending