Jump to content


MarshMan0331

Need Help With Powershell Script for PC Uptime and Reboot

Recommended Posts

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"

Share this post


Link to post
Share on other sites


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.

  • Like 1

Share this post


Link to post
Share on other sites

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...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...