We help IT Professionals succeed at work.

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.
Comment
Watch Question

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.

"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
With some external tools this should not be difficult. You should have your list of PCs ready, let's call it PCs.txt. Then, using psloggedon from www.sysinternals.com, we can do as follows:
@echo off

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

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Remove the word "echo" from line 6 after you have successfully tested the script. In the current state it will NOT peform a shutdown, only print that out.

I have set up the grace period to one second - which is very very short, and you might want to change that (/t ).

With the above check, IIS and similar services using an interactive login prevent the PC from shutting down. If that should be the case, first remove the >nul from line 5 to see who is logged in, and add filter expressions accordingly to the findstr command after an additional /c:. For IIS, the findstr looks like
   findstr /V /c:"NT-AUTHOR" /c:"locally" /c:"IWAM" >nul

The script will first check if a PC is on (using PING). This prevents from trying to access a dead computer, which lasts very long until it is considered dead when we try to access it.
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.

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

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

Author

Commented:
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Ok, 2000 machines, that is much. If many need to get shutdown, that will last a while to complete. I would prepend the shtudown command with a
   start "shutting down %%C" /min shutdown ...
to let that run in-parallel. The script is not waiting for the remote machine to confirm the shutdown, which should speed up things a bit.

Author

Commented:
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>
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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).

Author

Commented:
The first one returns Success the second one gives me a successful ping. Pinging a known unused name it gives Failure.



Author

Commented:
The solution as given did not work until it was modified.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
So you are running it without the ping test now?