Try something like this
$vmsOn = Get-VM | Where {$_.PowerState -eq "poweredon"}
$vmsON | Export-Csv "C:\poweredon-vms.csv" -NoTypeInformation -UseCulture
$vmsOn | Stop-VM -Confirm:$false
$esx = Get-VMHost
$esx | Set-VMHost -State maintenance
$esx | Stop-VMHost
and
$esx = Get-VMHost
$esx | Set-VMHost -State connected
Import-Csv "C:\poweredon-vms.csv" -UseCulture | %{
Start-VM
}
If your VMs all have the VMware Tools installed, you can replace the ungracefull Stop-VM by a Shutdown-VMGuest.