Here's your script, rewritten in PowerShell (change the "$true" to "$false" in the Restart-Computer cmdlet if you want the restart to do anything):
Add-Type -AssemblyName PresentationCore,PresentationFramework
# Set the minimum number of days of uptime to issue the warning.
$WarnAfterDays = 1
# Set the initial restart countdown hours.
$RestartCountdownHours = 4
# The CIM object has a DateTime type for LastBootupTime. No conversion necessary
(Get-CimInstance Win32_OperatingSystem).LastBootupTime |
ForEach-Object{
$dlastReboot = $_
$dDiff = (Get-Date) - $dlastReboot
If ($dDiff.TotalDays -ge $WarnAfterDays){ # TotalDays takes into account fractional days E.G., 1 Day and Twelve Hours = 1.5 instead of
# the "Days" property that would have returned a value of 1 and forced a reboot
If ($RestartCountdownHours -gt 0){
[System.Windows.MessageBox]::Show(("Restart in {0} hours" -f $RestartCountdownHours),"Restart Reminder",'OK','Information')
Start-Sleep -Seconds ($RestartCountdownHours * 60 * 60)
Restart-Computer -Force -WhatIf:$true
}
Else{
[System.Windows.MessageBox]::Show("Restarting in 5 minutes", "Restart Reminder", 'OK', 'Information')
Start-Sleep -Seconds (5 * 60)
Restart-Computer -Force -WhatIf:$true
}
}
Else{
[System.Windows.MessageBox]::Show("Restarting now", "Restart Reminder", 'OK', 'Information')
Restart-Computer -Force -WhatIf:$true
}
}
As @MotoX80 pointed out, you need a desktop to display the message box and scheduled tasks don't have one (at least in the Scheduled Task GUI), so it's unlikely this will work the way you expect it to.