Using PowerShell Schedule Job to Reboot a Computer

I have to reboot a server tonight and well, I don’t want to have to VPN in to do it tonight. I know there is a Restart-Computer cmdlet which is cool but i need to schedule it, so i decided to use the PS Scehduled Job and came up with this

register-ScheduledJob -Name systemReboot -ScriptBlock {

Restart-Computer -ComputerName $server -Force -wait

Send-MailMessage -From -To -Subject "Rebooted" -SmtpServer

} -Trigger (New-JobTrigger -At "11/28/2012 6:00pm" -Once) -ScheduledJobOption (New-ScheduledJobOption -RunElevated) -Credential (Get-Credential)

Sorry, it’s one long line, but copy and paste it and you’ll be happy you took the time to figure it out.

Two important notes here are that I used the New-ScheduledJobOption to force it to run elevated, which is more often than not, needed.

The other part, which sort of threw me for a loop was that you have to specify credentials even if they are the creds you are currently logged in as. The reason for this is that if you don’t you wont have the creds in the job pass through to the script.






About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on November 28, 2012, in WMF (Powershell/WinRM) and tagged , , , , , , , , . Bookmark the permalink. 6 Comments.

  1. Hi,
    I have this idea on having a powershell script which query a SQL database for a computername and reboot time. Based on that information, it will the use PSRemoting to register a scheduled job to reboot the server. I am new in PowerShell and would like to get some advice on how to accomplish this. Appreciate your response.

    • Thats completely possible, you dont even need the DB since the last reboot is stored in WMI. you should ask in one of the forums, such as the technet or forums or even in the freenode #powershell channel which im generally in.

      • In this case, the DB is required as each servers may have different reboot time. To kick start windows update before rebooting would be a neat as well.

  2. Thanks for this post. I also found that if I wanted to pass the $server variable into the scriptblock I had to use the -argumentlist parameter.

  3. 2 comments:
    This command only works in PowerShell 3 or 4; so it doesn’t work in a default install on Server 2008R2. You need to upgrade PowerShell first.
    Should there be a space in $server-Force ?

  4. schtasks /create /sc once /tn restart /tr “shutdown – r -f “”restart””” /st 02:00

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: