+ Post New Thread
Results 1 to 11 of 11
Scripts Thread, If logged on - Batch file? in Coding and Web Development; Hi, I want to make a batch file that will run one command if the computer is logged on and ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,390
    Thank Post
    797
    Thanked 1,587 Times in 1,390 Posts
    Blog Entries
    10
    Rep Power
    427

    If logged on - Batch file?

    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

  2. #2
    jamesreedersmith's Avatar
    Join Date
    Sep 2009
    Location
    Ruskington
    Posts
    1,104
    Thank Post
    77
    Thanked 242 Times in 216 Posts
    Rep Power
    74
    What does the command need to do?

    you could use a logon and logoff script though!

  3. #3

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,390
    Thank Post
    797
    Thanked 1,587 Times in 1,390 Posts
    Blog Entries
    10
    Rep Power
    427
    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.
    Last edited by FN-GM; 22nd November 2012 at 09:38 AM.

  4. #4
    jamesreedersmith's Avatar
    Join Date
    Sep 2009
    Location
    Ruskington
    Posts
    1,104
    Thank Post
    77
    Thanked 242 Times in 216 Posts
    Rep Power
    74

  5. #5

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    This vbscript will do it:

    Code:
    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

  6. #6
    chazzy2501's Avatar
    Join Date
    Jan 2008
    Location
    South West
    Posts
    1,723
    Thank Post
    206
    Thanked 254 Times in 206 Posts
    Rep Power
    65
    you can deploy scheduled tasks using client side extensions.

  7. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,390
    Thank Post
    797
    Thanked 1,587 Times in 1,390 Posts
    Blog Entries
    10
    Rep Power
    427
    Quote Originally Posted by chazzy2501 View Post
    you can deploy scheduled tasks using client side extensions.
    im not asking for a script to deploy a sheduled task

  8. #8
    chazzy2501's Avatar
    Join Date
    Jan 2008
    Location
    South West
    Posts
    1,723
    Thank Post
    206
    Thanked 254 Times in 206 Posts
    Rep Power
    65
    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.

  9. #9

    Join Date
    Mar 2010
    Location
    shadowx@AllEvil:/
    Posts
    222
    Thank Post
    12
    Thanked 28 Times in 25 Posts
    Rep Power
    13
    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?

  10. #10

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Quote Originally Posted by shadowx View Post
    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.

  11. #11

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,390
    Thank Post
    797
    Thanked 1,587 Times in 1,390 Posts
    Blog Entries
    10
    Rep Power
    427
    Quote Originally Posted by ChrisMiles View Post
    This vbscript will do it:

    Code:
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Not a script as such, but a batch file loaded on startup
    By the_one_that_cant in forum Scripts
    Replies: 1
    Last Post: 4th October 2011, 10:33 AM
  2. Log on as batch job
    By Edu-IT in forum Windows
    Replies: 2
    Last Post: 19th March 2010, 09:49 AM
  3. Log on as batch job
    By ajames in forum Windows Server 2008
    Replies: 0
    Last Post: 29th April 2009, 12:32 PM
  4. run a batch file on logon with a CC3 Network
    By new-2-this in forum How do you do....it?
    Replies: 5
    Last Post: 17th July 2008, 07:57 PM
  5. prezzie batch file to collect dps logs
    By russdev in forum ICT KS3 SATS Tests
    Replies: 4
    Last Post: 16th May 2006, 12:16 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •