Powershell to sequentially shutdown VM guests, then restart the host

A little while back, I developed a script that read in server names from a text file and rebooted them. It was pretty straightforward, but I realised that this would never work for virtual machines because it’s not the best idea just to shut down the host without taking the guests into consideration. So I cam e up with the following script; it essentially does the same thing twice, but against two different text files (guest and host). This is important primarily because it shuts the guests down, but only restarts the host (there are other minor differences but the script is heavily commented). The sequence is as follows: repeatedly shut guests down with 2-minute intervals until the list runs out, then wait 3 minutes and restart the host. It also performs some logging and emailing (tho’ of course, each server still logs the normal EventLog entries). Schedule this, and you can gracefully restart your virtual hosts on a routine basis.


#Script to reboot selected virtual servers
#Define hub transport server

 $smtp_server = “xxxx.someDomain.com”
#Define email sender and recipient.
#The intital text after the quotes after $sender are free-form;
#you can specify whatever name you want. This doesn’t work for $recipient
#as the system puts the real name on the email address.

 $sender = “senderEmail <senderEmail@somedomain.com>”
 $recipient = “recipientEmail <recipientEmail@somedomain.com>”
  #reads the contents of the specified text file one line at a time

  get-content someDrive:\someFolder\Scripts\rebootHVGuests.txt | Foreach-Object {
   #assigns $computerName to value from get-content

   #shuts the virtual guest down

   stop-computer -computername $ComputerName -force
#Script waits for 2 minutes before shutting down next guest

Start-Sleep -Seconds 120
#Script waits for 3 minutes before restarting hosts
Start-Sleep -Seconds 180
  #reads the contents of the specified text file one line at a time

  get-content someDrive:\someFolder\Scripts\rebootHVHosts.txt | Foreach-Object {
   #Define date and time variables

   $dateNow = get-date -displayhint date
   #assigns $computerName to value from get-content

   #Define email subject and body

    $msg_subject = “Planned system reboot of $ComputerName”
    $msg_body_text = “Server $ComputerName is being forcibly rebooted at: $dateNow”
    #Send it

    Send-MailMessage -to $recipient -from $sender -subject $msg_subject -body $msg_body_text -smtpserver $smtp_server
   # Writing an event log entry

    $EventLog = New-Object System.Diagnostics.EventLog(‘Application’)
    $EventLog.MachineName = “.”
    $EventLog.Source = “someCustomEvent”
    $EventLog.WriteEntry(“Now rebooting $ComputerName”,”Information”, $EventID)
   #restarts the virtual host

   restart-computer -computername $ComputerName -force



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s