Link to home
Start Free TrialLog in
Avatar of Mark_McCrea_me01
Mark_McCrea_me01

asked on

Script to Shutdown Logged off Computers

I want to have a script that will check if a computer in my AD or from alist of names has someone logged into it and if there is no one logged in then to shutdown that computer.

My network is multiple subnets with WAN connections, running server 2003 and XP SP3 clients.

The problem with this is I my scripting is very basic so I need a lot of help with this.
Avatar of George Khairallah
George Khairallah
Flag of United States of America image

Mark, it looks like you're looking for some sort of power management tools.
What you're trying to do with a script seems like it would be pretty tough to do remotely, while controlling hundreds or thousands of workstations.  

Theoretically, you can have the script residing on the workstations themselves, and have them run in a scheduled task on a regular interval, and check if there is a user logged in, and shut the machine down if it's not being used.
That would be a fairly simple ordeal, but honestly, I would just look for power management to accomplish what you're doing. There are a lot of products that do that, you can look at Faronics PowerSave, for instance.

One bonus with using a power management tool, is that your company will qualify for rebates with the electric company if you use those, provided you're in the US, and in the states that are doing the rebate programs.

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Qlemo, I think your solution would work pretty well.
I'm usually just reluctant to do something like this with small little scripts that run across the whole network, as I would be concerned to increase the constant traffic.
Since Mark is looking to have this available all the time, to always detect logged off workstations, and depending on the number of PCs he has, it could take hours, even with a 25ms ping. (an RPC connection to each workstation will take a minimum of 2 seconds on each workstation.) . Also, this script would have to run sequentially, increasing the time of execution tremendously.

I have dealt with issues of remote running scripts on workstations, and  I found that is far more efficient to trigger these scripts from within the workstation.
1- because completely eliminates network usage
2- it's a failsafe way to check whether a user is logged on, regardless of the workstations' RPC availability.
3- You can run the check on 1 machine or 5000 machines at the same exact  second with 0 impact to your network, and have all the machines shutdown within 1 minute.

There is nothing said about whether it runs all the time or once a day. As you can see, this script is not designed to run 24/7, only once in a while, thought be triggered by some event or schedule.

If i were to implement a "shutdown after 60 mins without login", which is kind of power-safe feature, I would do that per local (logoff) script.
Avatar of Mark_McCrea_me01
Mark_McCrea_me01

ASKER

I'll clarfiy a few things here. I would only run this on Friday or before holidays to make sure a school has shut down its computers or during lighening storms since some of the locations do not have good power. There are less then 2,000 computers involved.

Qlemo, I'll give that script a test in the next couple of days and let you know how it goes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is a problem with the script. The ping check part does not work.
when I take "ping -n 1 -w 25 %%C >nul && (" out the command runs fine and the compouters shutdown.

If I run it with the ping line I get nothing back on the screen. Turning Echo on I see:



D:\Data>shutdownscript2.bat
D:\Data>for /F %C in (PCs.txt) do (ping -n 1 -w 25 %C 1>nul && (psloggedon -l
-x \\%C 2>nul | findstr /V /c:"NT-AUTHOR" /c:"locally" 1>nul || (echo shu
tdown /m \\%C /s /t 1 /c "Automatic Shutdown by script" /f ) ) )

D:\Data>(ping -n 1 -w 25 STP109wLib 1>nul && (psloggedon -l -x \\STP109wLib
2>nul | findstr /V /c:"NT-AUTHOR" /c:"locally" 1>nul || (echo shutdown /m \
\STP109wLib /s /t 1 /c "Automatic Shutdown by script" /f ) ) )

D:\Data>(ping -n 1 -w 25 STP110wLib 1>nul && (psloggedon -l -x \\STP110wLib
2>nul | findstr /V /c:"NT-AUTHOR" /c:"locally" 1>nul || (echo shutdown /m \
\STP110wLib /s /t 1 /c "Automatic Shutdown by script" /f ) ) )

D:\Data>(ping -n 1 -w 25 STP112wLib 1>nul && (psloggedon -l -x \\STP112wLib
2>nul | findstr /V /c:"NT-AUTHOR" /c:"locally" 1>nul || (echo shutdown /m \
\STP112wLib /s /t 1 /c "Automatic Shutdown by script" /f ) ) )

D:\Data>
Please try the following:
   ping -n 1 127.0.0.1 >nul && echo Success || echo Failure
If the result is "Success", try
   ping -n 1 -w 25 STP109wLib
(or any other PC you know running).
The first one returns Success the second one gives me a successful ping. Pinging a known unused name it gives Failure.



The solution as given did not work until it was modified.
So you are running it without the ping test now?