Last active
January 27, 2023 14:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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