Hi everyone
Experts help me with a script to shutdown VM's and ESXi hosts. I dont have testing environment refer the below scripts and let me know if there are any error.
In the script to shutdown ESXi hosts i dont want my management esxi hosts and vcenter servers to be shutdown, i want to shutdown manually, will the below script work for me.
SCRIPT TO Shutdown Virtual Machines
$VMs = Get-Content D:\VM.txt
Foreach ($VM in $VMs)
{
Shutdown-VMGuest $VM -Confirm:$false
Write-Host "$VM Shutdown initiated"
}
--------------------------------------------------------
SCRIPT TO Start Virtual Machines
$VMs = Get-Content D:\VM.txt
Foreach ($VM in $VMs)
{
Start-VM $VM -Confirm:$false -RunAsync
Write-Host "$VM Start initiated"
}
--------------------------------------------------------
SCRIPT TO shutdown esxi hosts.
#Connect-VIServer MyVIServer
# Get All the ESX Hosts
#$ESXSRV = Get-VMHost
$ESXSRV = Get-Cluster “MY CLUSTER” | Get-VMHost
# For each of the VMs on the ESX hosts
Foreach ($VM in ($ESXSRV | Get-VM))
{
# Shutdown the guest cleanly
$VM | Shutdown-VMGuest -Confirm:$false}
# Set the amount of time to wait before assuming the remaining powered on guests are stuck
$waittime = 0 #Seconds
$Time = (Get-Date).TimeofDay
do {
# Wait for the VMs to be Shutdown cleanly
sleep 1.0
$timeleft = $waittime - ($Newtime.seconds)
$numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count
Write "Waiting for shutdown of $numvms VMs or until $timeleft seconds"
$Newtime = (Get-Date).TimeofDay - $Time
} until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime)
# Shutdown the ESX Hosts
$ESXSRV | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)
}
Write-Host "Shutdown Complete"