PowerShell for Windows Admins

Feb 10 2015   1:36PM GMT

Starting VMs based on WSUS state

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Hyper-V
Powershell
WSUS

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: