FN-GM Posted November 22, 2012 Posted November 22, 2012 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
jamesreedersmith Posted November 22, 2012 Posted November 22, 2012 What does the command need to do? you could use a logon and logoff script though!
FN-GM Posted November 22, 2012 Author Posted November 22, 2012 (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 November 22, 2012 by FN-GM
jamesreedersmith Posted November 22, 2012 Posted November 22, 2012 Have a read of Log Off user when idle session exceed 8 hours by GPO Cheers James
ChrisMiles Posted November 22, 2012 Posted November 22, 2012 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
chazzy2501 Posted November 22, 2012 Posted November 22, 2012 you can deploy scheduled tasks using client side extensions.
FN-GM Posted November 22, 2012 Author Posted November 22, 2012 you can deploy scheduled tasks using client side extensions. im not asking for a script to deploy a sheduled task
chazzy2501 Posted November 22, 2012 Posted November 22, 2012 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.
shadowx Posted November 22, 2012 Posted November 22, 2012 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?
ChrisMiles Posted November 22, 2012 Posted November 22, 2012 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.
FN-GM Posted November 24, 2012 Author Posted November 24, 2012 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now