Jump to content

Recommended Posts

Posted

Hi,

 

I want to make a batch file that will run one command if the computer is logged on and a different one if its logged off.

 

Any thoughts please?

 

Thanks

Posted (edited)

It needs to be a scheduled task.

 

Its so if a machine has been left logged on the machine gets a forced reboot. If it has been left logged off it shutdown. Our SCCM installs software when the machine is left logged off. But people leave them logged on. Its so i can get the software installed.

Edited by FN-GM
Posted

This vbscript will do it:

 

strComputer = "."

Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objShell = WScript.CreateObject("WScript.Shell")

Set colSessions = objWMI.ExecQuery("SELECT * FROM Win32_LogonSession WHERE LogonType = 2")

strCmd = ""

If colSessions.Count = 0 Then 
' No interactive users found
strCmd = "shutdown /s /t 0 /f"
Else 
' Interactive users found
strCmd = "shutdown /r /t 0 /f"
End If

objShell.Run strCmd

Posted

A scheduled task can run shutdown.exe (with switches) based on certain criteria! e.g. if the computer is in use, on battery, if the PC is asleep. seems perfect.

 

I use this to ensure all PCs shutdown at 6PM if they're logged in, logged in and asleep, or just asleep and no logged in.

Posted
Just a thought, cant you use a batch to check the value of %username%, if it's blank then it's logged off I would presume?

 

%USERNAME% will return the username of the account used to run the task and will never be blank.

Posted
This vbscript will do it:

 

strComputer = "."

Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objShell = WScript.CreateObject("WScript.Shell")

Set colSessions = objWMI.ExecQuery("SELECT * FROM Win32_LogonSession WHERE LogonType = 2")

strCmd = ""

If colSessions.Count = 0 Then 
' No interactive users found
strCmd = "shutdown /s /t 0 /f"
Else 
' Interactive users found
strCmd = "shutdown /r /t 0 /f"
End If

objShell.Run strCmd

 

Thanks @ChrisMiles i am going to give this a bash. Also is it possible to stick a line in so if a machine is on a particular IP range to just exit. I am thinking of doing it for machines that are currently being worked on in our workshop so they dont automatically shutdown at night. Thanks

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



×
×
  • Create New...