There was a requirement in one of the projects to perform an unattended shutdown of multiple Virtual Machines during a planned Maintenance Window. We wrote a PowerCLI script to perform shutdown operation on multiple VMs with a delay of 20 seconds between each shutdown.
Requirements:
- PowerCLI
- PowerShell 5.1 or later
- VMware Tools on the Virtual Machines
Script:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer “vCenter_Server_IP_Address/FQDN” -Credential (Get-Credential)
$VMs = Get-Content C:\Scripts\VMShutdown\VMList.txt
foreach ($VM in $VMs)
{
Write-Host “Initiating Guest Shutdown for VM ” + $VM -ForegroundColor DarkRed
Shutdown-VMGuest -VM $VM -Confirm:$false
Start-Sleep -Seconds “20“
}
foreach ($VM in $VMs)
{
Get-VM $VM | Select Name, PowerState | Ft -AutoSize -Wrap
}
Disconnect-VIServer -Confirm:$false
Note: Virtual Machines are shutdown gracefully and requires VMware Tools to installed on machine.
Just replace the vCenter_Server_IP_Address/FQDN, Path of File Containing VMs Name and Sleep Interval after each shutdown operation with the details of your environment. BOOM!!
Happy Scripting!!
Thanks! Nice work and great help when it’s needed