Jump to content
Sign in to follow this  
tbwalker

Reboot PC when idle AND logged out

Recommended Posts

tbwalker

I'm a bit of an amateur with AutoIt, but I have been able to create several useful scripts to help out at the office.  One script that has eluded me is a script that can run AS A SERVICE and detect that NO USERS are currently logged in.  We are a 24/hr facility running about 500 workstation 24 hours a day.  Many of these computers go 6 to 8 weeks without being REBOOTED.  To help make users aware that the computer they are on has not been rebooted in a while, I made the below script (with help from an old post '?do=embed' frameborder='0' data-embedContent>> ) as an option to help motivate users to reboot workstations more often...

If $CmdLine[0] = 0 Then
    $defaultDays = 7
Else
    $defaultDays = $cmdLine[1]
EndIf
$ret = DllCall("kernel32.dll","long","GetTickCount")

If IsArray($ret) Then
    $uptime = Round(($ret[0] / 3600000),1); convert miliseconds to hours
    $uptimeDays = Round(($uptime / 24),0)
    If $uptimeDays >= $defaultDays Then
    MsgBox(0,"Reboot Recommendation", "     This workstation has been running for " & _
    $uptimeDays & " days without rebooting."& @crlf & @crlf& "         This is a friendly reminder to let you know that rebooting this" & _
    @crlf & "        computer will help improve its overall speed and performance.",15)
    EndIf
EndIf

The above script detects how many days the workstation/pc has gone without a reboot.  I put it in the ALL USERS startup folder, and it would give users a nice little popup message suggesting that they reboot to improve pc performance.  After making the script, a co-worker suggested "Why don't you just have the script REBOOT the PC automatically when it reaches the desired day threshold, but AFTER THE CURRENT USER LOGS OUT and no one is logged in".

His suggestion sounds exactly like what I want, and I'm certain I can modify the script to run as a service and REBOOT the PC... but my only concern is how to get the script to detect if the computer is logged out (not IDLE, mind you, but completely logged out to the PRESS CTRL-ALT-DELETE TO LOGIN screen).

As a side note, we are still currently running Windows XP Professional until JULY of 2014...

Any suggestion as to how I can get my script to know if the computer is LOGGED OUT would be greatly appreciated.

 

Thanks in advance.

Share this post


Link to post
Share on other sites
DicatoroftheUSA

If rebooting "fixes" your computer then your computer is malfunctioning and should be fixed. I have computers that have been on for over a year with no issue.

Check for what is not letting go of memory, or use procmon to look for odd behavior. Make sure your drives are not getting too hot.

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
kylomas

tbwalker,

This might be useful...

#include <Constants.au3>
#include <WinAPISys.au3>
#include <array.au3>

$aRet = get_users()
_ArrayDisplay($aRet)

Func get_users()

    Local $strComputer = "localhost", $aLoggedOn[1][2], $iEle = 0
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

    $colSessions = $objWMIService.ExecQuery _
            ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")

    For $objSession In $colSessions
        $colList = $objWMIService.ExecQuery("Associators of " _
                 & "{Win32_LogonSession.LogonId=" & $objSession.LogonId & "} " _
                 & "Where AssocClass=Win32_LoggedOnUser Role=Dependent")
        For $objItem In $colList
            $aLoggedOn[$iEle][0] = $objItem.Name
            If $objSession.LogonType = 2 Then
                $aLoggedOn[$iEle][1] = 'Console'
            Else
                $aLoggedOn[$iEle][1] = 'RDP / Terminal Services'
            EndIf
            $iEle += 1
            ReDim $aLoggedOn[UBound($aLoggedOn) + 1][2]
        Next
    Next

    Return $aLoggedOn

EndFunc   ;==>get_users

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
tbwalker

DicatoroftheUSA, you are absolutely correct about computer malfunctioning.  Let’s just say that the company I work for is aware of the problem and has kicked back to the users the need to limp along with problems/issues for a few more months.  The auto-reboot idea is simply a stop-gap, Band-Aid solution to alleviate some of the user’s pains (granted, it is an unprofessional solution, but, hey, I’m just a minion of the corporate machine that I work for).  If I can document my efforts to alleviate some of the pain suffered by users, while at the same time documenting the reasons for my stop-gap solution, I’ll have covered my bases and insured future employment…. ;-)

Kylomas, THANKS FOR THE TIP!  I’ll be playing with your code this morning…. I really appreciate it!

Share this post


Link to post
Share on other sites
kylomas

Have you thought of using CCleaner to clean up the PCs? It really helps.

http://www.piriform.com/ccleaner

 

And it has an "/AUTO" parm for silent execution from a script. 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
tbwalker

Thanks for the tips MBALZESHARI and FUBAR.

I use CCleaner regularly and I think it’s great.  The issue is definitely hardware related (new clean image recently deployed house wide).  I was about to post a message here explaining the details of my IT situation, but it started to turn into a research paper.  Instead of boring you with that, I’ll just give you the bullet points of my situation.

  • Our company is aware that over 70% of our computers are dying due to capacitor/motherboard thermal failure
  • 60 days ago we deployed a new/clean base XP image to all 500+ workstation to help improve performance.
  • Positive feedback was given from all users.  A performance increase of 30%-40% was reported from all departments.
  • All workstations are on a domain and managed by group policy, restricting users from installing new software, locking external storage devices and restricting internet access to all but about 10% of the workstations
  • Our IT department gets about 30-40 support calls a day, with at least 10-15 of the calls being related to the failing hardware and temporarily fixed by a simple reboot
  • Management is aware that the computer workstations need to be replaced, but management is also stating that we cannot afford to purchase new computer until 4th quarter (220+ days from now).
  • Management knows that the only thing we can do on these overheating/dying machines is to simply reboot them every few days to keep them in running order…

Note:  It sucks being an IT guy who is at the mercy of management and is being told to “RIG” the dying equipment as best as possible and limp along for the next 220+ days….

 

Whew… that being said… the 30% of the workstations that do not have motherboard/thermal problems run beautifully on the new base image for weeks on end ( I have some that haven’t been rebooted in 60+ days).  The other 70% run great for 3-5 days… then start acting up until they are rebooted.  Management wont allow me to simply mandate to the staff that they need to reboot every 2-3 days (management wanting to hide the problem from the general employee population... par for the course...).

I hoped to come up with a stop-gap/automated method to reboot these machines during specific down-time conditions in order to keep them running well (or at least running until they completely die)…

 The reboot conditions I’d like to create a script execute a reboot under are:

  • If the computer has been up and running for over 48 hours.
  • If the computer has NO USER currently logged.
  • If the current time on the computer is between 1am-4am (our slowest time).

Again… I know and agree with ever one of you that this is a unprofessional, bubble-gum/duct tape solution that should never be used as a true solution, but this is the situation I am in, and in lieu of being allowed to take the professional route and bulk replace these dying computers, rebooting is currently my only recourse.

wow.... it feels good getting that off of my chest... I think I just saved myself a visit to the therapist....

Share this post


Link to post
Share on other sites
kylomas

The reboot conditions I’d like to create a script execute a reboot under are:

  • If the computer has been up and running for over 48 hours.
  • If the computer has NO USER currently logged.
  • If the current time on the computer is between 1am-4am (our slowest time).

 

This is easily done...what I gave you will get you started....

I also use a script that runs CCLEANER after every instance of IE.  This might be useful to you, let me know.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
BrewManNH

If I were you, I would tell the users to log out at the end of their work shift. And then at midnight, or whatever time is best for your company, run a remote reboot on all the computers once a week. You can use psshutdown with a text file containing the names of all the computers that should be rebooted so you don't inadvertently reboot the wrong computers/servers.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Bert

As a seasoned tech, I have a few suggestions you may be able to do:

1. Cooling issues sound like to me the PCs need to be cleaned with a vacuum to remove the dust. This can be done after hours without the employees knowing.

2. Using CCleaner - make sure you wipe the free space. I have found this greatly increases speed overall. The reason is the HDD is still indexing files that have been deleted. Is the HDD on the PCs thrashing?

3. This is a guess but on some PCs you can specify the fan speed for cooling.

4. How are the PCs placed on the desk? They have to be able to breath.

5. is it possible to look in startup using MSCONFIG to see what is really running and shut off what is not needed? A computer's resources are limited so no need running stuff that is not needed.

6. There is a screen saver that will log out a user if they are not active after a certain amount of time.

7. Has the firmware on the motherboard been updated?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

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.