Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Last active January 27, 2023 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoDBA/f7a19ebc4987e65df6bf0fc8981a5c4b to your computer and use it in GitHub Desktop.
Save nanoDBA/f7a19ebc4987e65df6bf0fc8981a5c4b to your computer and use it in GitHub Desktop.
This is meant for rebooting a local machine in a scheduled task and is not handling remoting or remote credentials
#requires -version 5.0
#Requires -RunAsAdministrator
<# DANGER REBOOTING!
This is meant for a local machine in a scheduled task
and is not handling remoting or remote credentials
#>
[CmdletBinding()]
param (
[Parameter()][int]$DelayMinutes = 15 #default to 15 minute delay if this parameter is not supplied
)
Begin {
if (![System.Diagnostics.EventLog]::SourceExists("PowerShellRebootScript")){
New-EventLog -Source 'PowerShellRebootScript' -LogName Application
}
}
Process {
$delaySeconds = $DelayMinutes * 60
$restartTime = (Get-Date).AddMinutes($DelayMinutes)
$msg = "This computer is being restarted. Save your work before $(Get-Date $restartTime -Format "yyyy-MM-dd HH:mm:ss zzzz") or abort the restart by running shutdown /a"
shutdown /r /f /d P:4:1 /t`$($delaySeconds) /c "$msg" 2>$null
msg * $msg
if ($LastExitCode -ne 0) {
Write-EventLog -Message "($env:username) failed to reboot ($env:computername) ($LastExitCode) script: $($myInvocation.myCommand.path)" -Source 'PowerShellRebootScript' -EventId 65110 -EntryType Warning -LogName Application
}
else {
Write-EventLog -Message "($env:username) initiated reboot of ($env:computername) ($LastExitCode) script: $($myInvocation.myCommand.path)" -Source 'PowerShellRebootScript' -EventId 65109 -EntryType Information -LogName Application
}
}
<#
References:
https://stackoverflow.com/questions/18107018/powershell-with-shutdown-command-error-handling/18109510#18109510
https://www.ciraltos.com/writing-event-log-powershell/
https://devblogs.microsoft.com/scripting/how-to-use-powershell-to-write-to-event-logs/
https://www.andreafortuna.org/2019/06/12/windows-security-event-logs-my-own-cheatsheet/
https://community.spiceworks.com/topic/2001422-create-scheduled-task-with-run-as-highest-level-whether-user-is-logged-or-not
https://ss64.com/ps/remove-eventlog.html
https://stackoverflow.com/questions/13851577/how-to-determine-if-an-eventlog-already-exists/26004087#26004087
$myServer01_svcCred = Get-Secret myServer01_svcCred
$taskName = "Reboot myServer01"
$description = "Restart myServer01 with a 15 min delay"
$principal = New-ScheduledTaskPrincipal -UserId $myServer01_svcCred.UserName -LogonType ServiceAccount -RunLevel Highest
$paswd = $myServer01_svcCred.GetNetworkCredential().Password
# Create a new task action
$taskAction = New-ScheduledTaskAction `
-Execute 'powershell.exe' `
-Argument '-File C:\PowerShellScripts\Reboot.ps1 -DelayMinutes 15'
$taskAction
#create a new task trigger weekly at 8pm
$taskTrigger = New-ScheduledTaskTrigger -Weekly -At 8PM -DaysOfWeek Friday
$taskTrigger
# Register the scheduled task
Register-ScheduledTask `
-TaskName $taskName `
-Action $taskAction `
-Trigger $taskTrigger `
-Description $description `
-RunLevel Highest `
-User $($myServer01_svcCred.UserName) `
-Password $paswd
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment