+ 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
    16,374
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468

    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,194
    Thank Post
    81
    Thanked 263 Times in 235 Posts
    Rep Power
    79
    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
    16,374
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    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 10:38 AM.

  4. #4
    jamesreedersmith's Avatar
    Join Date
    Sep 2009
    Location
    Ruskington
    Posts
    1,194
    Thank Post
    81
    Thanked 263 Times in 235 Posts
    Rep Power
    79

  5. #5

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    22
    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

    Join Date
    Jan 2008
    Location
    South West
    Posts
    1,842
    Thank Post
    219
    Thanked 271 Times in 219 Posts
    Rep Power
    70
    you can deploy scheduled tasks using client side extensions.

  7. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,374
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    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

    Join Date
    Jan 2008
    Location
    South West
    Posts
    1,842
    Thank Post
    219
    Thanked 271 Times in 219 Posts
    Rep Power
    70
    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
    14
    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
    22
    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
    16,374
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    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, 11:33 AM
  2. Log on as batch job
    By Edu-IT in forum Windows
    Replies: 2
    Last Post: 19th March 2010, 10:49 AM
  3. Log on as batch job
    By ajames in forum Windows Server 2008
    Replies: 0
    Last Post: 29th April 2009, 01: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, 08: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, 01: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
  •