header image

Starting VMs based on WSUS state

Posted by: | February 10, 2015 Comments Off on Starting VMs based on WSUS state |

I use WSUS in my lab to update machines – means I only have to download updates once. The issue is that I have to start the VMs so they can communicate with the WSUS server. WSUS will start to flag warnings if machines haven’t contacted the WSUS server for more than that length of time.

 

Putting that together I can use the UpdateServices (WSUS) module and the Hyper-V module to start machines that have been turn off more than 7 days.

 

$date = (Get-Date).AddDays(-7)
Get-WsusComputer -NameIncludes ‘W12R2’ |
where LastReportedStatusTime -lt $date |
foreach {
  $psitem.FullDomainName
  $name = ($psitem.FullDomainName -split "\.")[0]

  $vm = Get-VM -Name $name -ComputerName server02
 
  if ($vm.State -eq ‘Off’) {

    Start-VM -Name $name -ComputerName server02
 
    Start-Sleep -Seconds 30
  }
}

 

Set a date 7 days in the past.

 

Use get-WsusComputer to pull the machine names. I filter on W12R2 (all my machine names contain an abbreviated OS type) and check the last status report time.

 

Any machines that haven’t reported – the VM data is retrieved and if the machine isn’t running it’s started. Machines take about 25-30 seconds to boot on my lab so a sleep of 30 seconds means I’m not starting them all at once.

under: Hyper-V, PowerShell, WSUS

Comments are closed.

Categories