Run after reboot

# Initial actions
Write-Host “Performing initial actions before reboot…”
# Command to run a script block upon reboot
$scriptBlock = {
        Write-Host “This is the continuation after reboot.”
}
# Convert script block to a Base64 encoded string to pass it to the scheduled task
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($scriptBlock.ToString()))
# Creating the scheduled task
$action = New-ScheduledTaskAction -Execute ‘Powershell.exe’ -Argument “-EncodedCommand $encodedCommand”
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName “ContinueAfterReboot” -Description “My task to continue script execution after reboot”
# Restart the computer (uncomment in actual use)
Restart-Computer

Leave a Reply 0

Your email address will not be published. Required fields are marked *