# Place Computers to Sleep when users log off



## elprezidente (Jun 18, 2009)

Hi there!
This script is taken right from the EPA website. The purpose of it is so that you can have multiple computers run a scheduled task to execute this script so that it can detect the logged on status of the workstation. If the computer is logged off then it should go to sleep when the scheduled task calls on this script. The problem is that the script straight from the good old EPA doesn't work right off the bat. I'm new to VB script. I was able to clear out the syntax errors and get the script to run but it just places the workstation to sleep regardless if the computer is logged in or not. Below i've included the unedited VB script from the site. Any ideas where the problem might be?

*****Start of Script*************
'** Script Name: "standby-hibernate.vbs" **
Option Explicit
On Error Resume Next
Dim strComputer, sUserName, bLoggedOn, bReboot, objWMIService, 
colComputer, objComputer
Dim bStandby, objShell
strComputer = "."

Set objShell = WScript.CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer 
& "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
sUserName = objComputer.UserName
'WScript.Echo "UserName: " & objComputer.UserName
If sUserName <> "null" Then
bLoggedOn = True
End If
Next

If Err = 0 Then
If bLoggedOn Then
WScript.Echo strComputer & " 
is not Logged Off."
bStandby = False
Else
WScript.Echo strComputer & " 
is Logged Off."
bStandby = True
End If
Else
WScript.Echo "Error accessing computer: " 
& strComputer
bStandby = False
End If 
On Error Goto 0

WScript.Echo "bStandby: " & bStandby

If bStandby = True Then
WScript.Echo "Going into standby..."
'Go to standby
objShell.run "%windir%\psshutdown.exe -d -accepteula", 
0, False
Else
WScript.Echo "Not going into standby..."
End If


********END SCRIPT**************


----------



## TheOutcaste (Mar 19, 2009)

Seems to work OK for me, once I fixed the lines that were broken by word wrapping. I'd guess something you fixed wasn't quite right. See if this will work for you.
I didn't setup a scheduled task to see if it would put the system into standby, but it doesn't go to standby if anyone is logged on. Tested with both Admin and Limited accounts, and also tested on Win2K and it always gave the "Not going into standby..." message.

I added one more variable to display the actual Computer name too.


```
'** Script Name: "standby-hibernate.vbs" **
Option Explicit
On Error Resume Next
Dim strComputer, sUserName, bLoggedOn, bReboot, objWMIService, colComputer, objComputer
Dim bStandby, objShell, sComputerName
strComputer = "."

Set objShell = WScript.CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
sUserName = objComputer.UserName
sComputerName = objComputer.Name
WScript.Echo "UserName: " & objComputer.UserName
If sUserName <> "null" Then
bLoggedOn = True
End If
Next

If Err = 0 Then
If bLoggedOn Then
WScript.Echo sComputerName & " is not Logged Off."
bStandby = False
Else
WScript.Echo sComputerName & " is Logged Off."
bStandby = True
End If
Else
WScript.Echo "Error accessing computer: " & strComputer
bStandby = False
End If
On Error Goto 0

WScript.Echo "bStandby: " & bStandby

If bStandby = True Then
WScript.Echo "Going into standby..."
'Go to standby
 objShell.run "%windir%\psshutdown.exe -d -accepteula", 0, False
 Else
 WScript.Echo "Not going into standby..."
 End If
```
Jerry


----------



## elprezidente (Jun 18, 2009)

Jerry,
THANK YOU!!!!!!!!!
It works perfectly now. I added the correct script, with the scheduled task and it worked like a charm. You're the BEST!

Alfonso


----------



## TheOutcaste (Mar 19, 2009)

You're Welcome!

If your issue has been resolved you can mark this thread Solved by using the Thread Tools at the Top Right of this thread (above the first post) :grin:

Jerry


----------



## Squashman (Apr 14, 2005)

Are you setting that as a logoff script with a Group Policy or are you actually running it as a Scheduled Task?


----------



## elprezidente (Jun 18, 2009)

I created a startup script for the workstation OU on AD that auto creates the scheduled task. The script will create the task, make a copy of the VB script to the local drive (for laptops and workstations that connect in occasionally via VPN) and copy psshutdown.exe which is the application that places the computer to sleep. The scheduled task runs once an hour during the course of the day and only places the computer to sleep if the user is logged off. So far it's been like this all weekend and it's worked like a charm. There are a few users who still have a bad habit of leaving their workstations logged in but they're getting better at it.


----------



## elprezidente (Jun 18, 2009)

On a seperate note i can't for the life of me find where to mark this thread as solved. Is there some option i'm not seeing?


----------



## TheOutcaste (Mar 19, 2009)

TheOutcaste said:


> Thread Tools at the Top Right of this thread (above the first post)


 Should be a mark solved option in the drop down.


----------



## elprezidente (Jun 18, 2009)

Maybe I don't have enough posts to perform that action on a thread. I can't see it. Hopefully an admin can post it as solved for me.


----------



## Squashman (Apr 14, 2005)

elprezidente said:


> There are a few users who still have a bad habit of leaving their workstations logged in but they're getting better at it.


I hope you have a policy setup the Lock the Workstations with the ScreenSaver.

My Friend is the Admin for a large school district and this is what he uses to accomplish this task.
http://users.telenet.be/jbosman/applications.html


----------

