There are many articles on the web about how to shutdown and poweron VMs with a script. My version of powering off VMs is here. Below is the script for powering on a list of VMs.
- First a CSV file (poweredonvms.csv) containing list of servers to be updated. It can also be generated automatically by the powering off vm script.
name vmserver01 vmserver02 vmserver03 vmserver04 vmserver05 vmserver06 vmserver07 vmserver08 vmserver09 vmserver10
- And here is the actual Windows Powershell script. To run from vSphere PowerCLI, remove "Add-PSSnapin VMware.VimAutomation.Core" at the beginning of the code. After powering on the vms, it sends out an email with the list of vms.
- It first checks if a vm is powered on. If it is powered on already, it will be skipped. Else it will be powered on.
Add-PSSnapin VMware.VimAutomation.Core $vcenter="<vcenter server>" #Connect to vcenter server connect-viserver $vcenter #Import vm name and ip from csv file Import-Csv poweredonvms.csv | foreach { $strNewVMName = $_.name #Generate a view for each vm to determine power state $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName} if ($vm.Runtime.PowerState -ne "PoweredOn") { Write-Host "Powering On $strNewVMName ----" Get-VM $strNewVMName | Start-VM Sleep 10 #For generating email $Report += $strNewVMName + " --- Powered on. `r`n" } } write-host "Sleeping ..." Sleep 300 #Send out an email with the names $emailFrom = "<sender email id>" $emailTo = "<recipient email id>" $subject = "List of servers powered on" $smtpServer = "<smtp server name>" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $Report) #Disconnect to vcenter server disconnect-viserver $vcenter -Confirm:$false