Skip to content

Need Help With Powershell Script for PC Uptime and Reboot

Featured Replies

The script below will tell me the uptime on PC's. I want to add the language that says if Uptime is greater than X amount of days then it will send a reboot in 15 Mins. Any help would be appreciated.

 

# This PS script provides Uptime and Pingstatus for list of computers.
#######################################################################
$names = Get-Content "C:\Test\computers.txt"
@(
foreach ($name in $names)
{
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = gwmi Win32_OperatingSystem -computer $name
$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
Write-output "$name Uptime is $($uptime.days) Days"
Write-Host "$name Uptime is $($uptime.days) Days" -ForegroundColor Green
}
else {
Write-output "$name is not pinging"
Write-Host "$name is not pinging" -ForegroundColor Red
}
}
) | Out-file -FilePath "C:\Test\results1.csv"

  • 2 weeks later...
if($uptime.days -gt 3){$wmi.Win32ShutdownTracker(300, 'provide a message for logged on users here.', 0, 2)}

This would reboot a machine, which is running for more than 3 days, after 5 minutes grace and also show a message to a logged on user.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.