Script to reboot machines,

I'm wanting/trying to source a script that grabs a list of PCs in an OU in AD and checks the uptime.  If the PC is up for more than 7 days, we reboot it automatically unless someone is logged on.  In that case, we email the user saying that they need to restart.  They get this repeated warning until 14 days at which time the PC is restarted regardless.
Or best way to manage this other then scripting will also be option....
Craig PaulsenSenior Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AmitIT ArchitectCommented:
You cannot find server up time information from AD. You need to query server itself for uptime. Simple create a schedule task to restart your server after every seven days, do it after office hours. As far as email is concerned that is not the right way or feasible option. You better need to put a change ticket get it approved and restart your server. Then ask application team to check servers, if all is working well or not.
Craig PaulsenSenior Systems EngineerAuthor Commented:
Apologies if I was not clear, this script will be used to reboot workstations, not servers
AmitIT ArchitectCommented:
Can you tell the reason, why you want to reboot. Normally, client PC's we don't need to interfere, let user decide when to restart or not. Only one scenario, is patching, where you need user to reboot. For that you might be using WSUS or any other tool, which can push the patches and alert users to restart.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

yo_beeDirector of Information TechnologyCommented:
Here is a script I put together that you can push out to all computers as a scheduled task.
This script checks whether there is a logged on user session and then checks the computers uptime.
If there is a Logged on session and the time is between 7 and 14 days the script will prompt a message.  If the update is greater than 14 days the computer is rebooted. This script is only going to work for a logged on user.  

To get this to work for either or you will need a second scheduled tasks with a modified script that runs at the computer level that just checks for uptime greater than 14 days.



#User Task Schedule
$logonStatus = Get-WmiObject -Class Win32_Logonsession #| Where-Object {$_.LogonType -eq 2 -and $_.AuthenticationPackage -eq 'Negotiate'}
$Uptime = Get-WmiObject -Class Win32_OperatingSystem
$x = $Uptime.ConvertToDateTime($Uptime.LocalDateTime) –  $Uptime.ConvertToDateTime($Uptime.LastBootUpTime) | Select days

If ($logonStatus.LogonType -eq 2) 


{


If ($x.Days -gt 13)
{
{shutdown /r /f /t 300 /c "Your computer will be shutting down in 5 Minutes. Please save all work"}

}

Else
{
If ($x.days -gt 6)
{$wshell = New-Object -ComObject Wscript.Shell

$wshell.Popup('Your computer has been on for 7 or more days. Please restart your computer. If you do not restart prior to the 14th day your computer will be restart automaticlly and any files that are not saved maybe lost',0,"Done",33)}
}
}

Open in new window


#Computer Level Scheduled Task
$Uptime = Get-WmiObject -Class Win32_OperatingSystem
$x = $Uptime.ConvertToDateTime($Uptime.LocalDateTime) –  $Uptime.ConvertToDateTime($Uptime.LastBootUpTime) | Select days


If ($x.Days -gt 13)
{

{shutdown /r /f /t 300 /c "Your computer will be shutting down in 5 Minutes as a mandatory process by the system administrator. Please save all work"}

}

Open in new window

FloraCommented:
Yo_bee

interesting code. though, i am not author of the question, i was wondering to know to know what language this code is written and how to use this. i saved it in notepad with extension of vbs and it gave me error.
yo_beeDirector of Information TechnologyCommented:
The scripting language is Powershell which is also calling on wscript to present a message box for the user.

The -gt and -lt parts are greater than and Less than abbreviation.

The reason for the two different scripts is one is for computers that have a logged on user and the other is used is a computer is sitting idle without anyone on it.
yo_beeDirector of Information TechnologyCommented:
Also the shutdown part of the script is just calling on an exe with the arg. So you are able to leverage powershell to call on other items like exe's and other scripting languages like vbs
FloraCommented:
many thanks yo bee
yo_beeDirector of Information TechnologyCommented:
Revised Script for Computer based task.
This checked to see if there is any logon sessions and it so  nothing happens, but it there are no users logged on then the Shutdown.exe is run.  

You still schedule two tasks one for when no one is logged on and another that is scheduled 15 minutes later for users that are logged on.
The reason being is if the computer task passes and runs the shutdown then the user based one will not run, but if the computer see that there is a user on then the user task will run and it should shut the computer down.

#Computer Level Scheduled Task
$logonStatus = Get-WmiObject -Class Win32_Logonsession | Where-Object {$_.LogonType -eq 2 -and $_.AuthenticationPackage -eq 'Negotiate'} | Measure-Object 
$Uptime = Get-WmiObject -Class Win32_OperatingSystem
$x = $Uptime.ConvertToDateTime($Uptime.LocalDateTime) –  $Uptime.ConvertToDateTime($Uptime.LastBootUpTime) | Select days

If ($logonStatus.Count -eq 0 -and $x.Days -ge 14)

    {

        shutdown /r /f /t 300 /c "Your computer will be shutting down in 5 Minutes as a mandatory process by the system administrator. Please save all work"
    }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Craig PaulsenSenior Systems EngineerAuthor Commented:
apologies for only replying now, thanks Yo_Bee, you've given me something to work with....
Craig PaulsenSenior Systems EngineerAuthor Commented:
thanks Yo_Bee
yo_beeDirector of Information TechnologyCommented:
@Craig  Please note that there are two different scripts that need to run.
Are you aware of this?

If you want to continue in a private message dialogue I will be more than happy to assist.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.