Jump to content


  • 0
MarshMan0331

Need Help With Powershell Script for PC Uptime and Reboot

Question

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

1 answer to this question

Recommended Posts

  • 0
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
Answer this question...

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.