Using Powershell v3 scheduling for off hours Hyper-V VM maintenance/configuring
A colleague wanted a script for a reconfiguration of a VM´s settings. Once the VM is powered off that is not a big issue. The smart thing is when using Powershell version 3 I can configure a scheduled job that I want to execute off hours.
Imagine that you have the task to add a vCPU to a VM but you can only do it during the service window that happens to be around midnight, I don´t know about you but I rather sleep then if I can automate it. Yes I could in earlier versions use scheduled task but now I am using Win 2012 and Hyper-V 3 and the latest powershell.
So what do I need to do then, Jan Egil Ring has made a blog post about scheduled jobs and I want to show an example regarding Virtual Machine management..
In Powershell version 3 there are 16 cmdlets regarding scheduled jobs
First I need a Trigger
$once = New-JobTrigger -Once -At 11:59PM
Then I need a job with the trigger, I could use a parameter -FilePath to use a script file instead of -ScriptBlock
Register-ScheduledJob -ScriptBlock {$vm = Get-VM TestVM2 ; Stop-VM -VM $vm ; Set-VM -VM $vm -ProcessorCount 2 ; Start-VM -VM $vm} -Trigger $once
And then just wait or sleep 😛
And here you can see a screendump of the changing, Now I changed the trigger time to not have to wait to midnight for the blog post but I think you can imagine…
To check that the Job went ok, you run the cmdlet Get-Job ( and if you do not get any job you might need to run import-module PSScheduledJob to get the right Get-Job in that session)
God luck in scheduling your VM tasks 🙂
Comments
[…] want, you can enable a scheduled job that suspends and resumes a VM replication. I created earlier a blog post about setting up scheduled jobs. The following Cmdlets am I using in this […]
By using
1 – $once = New-JobTrigger -Once -At 11:59PM
2 – Register-ScheduledJob -ScriptBlock {$vm = Get-VM TestVM2 ; Stop-VM -VM $vm} -Trigger $once
I’m guessing it would pause the VMReplication right? Cause that is what I’m looking for at the moment, and then reverse that into start.
Super new to PS commands and stuff but can I chance the -ONCE to -EVERYDAY or something like that?