5

How can I Check if the Computer has been Locked using VBscript? I want to stop an app from running once the Computer has been locked and run it again when it is unlocked

Peter O.
  • 32,158
  • 14
  • 82
  • 96

2 Answers2

3

You can try checking for the existence of the logonui.exe process. If you find it, the computer is locked or not logged in.

Function IsLocked(strComputer)

    With GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        IsLocked = .ExecQuery("select * from Win32_Process where Name='logonui.exe'").Count > 0
    End With

End Function

To test the local computer, pass the PC name or a period. For example:

If IsLocked(".") Then MsgBox "Local computer is locked."
Bond
  • 16,071
  • 6
  • 30
  • 53
  • Thanks. I tried running it but IsLocked is false even when the computer is locked. I use windows 7 64 bit. – dilip kumarvignesh Sep 01 '14 at 05:18
  • Hmm. I'm also on Win7-64 and this works fine for me. I just created a script that calls this function once a second for 10 seconds and wrote the return value to a text file. I then ran the script, locked the computer after a few seconds, then put my password in to unlock the computer. `IsLocked()` returned `True` during the span where I had locked the computer and `False` otherwise. – Bond Sep 01 '14 at 13:43
  • Unfortunately I don't think this is a reliable method. If a computer is logged on only via RDP but not at the console, the LogonUI.exe will be running since the console is displaying the logon screen. However, the RDP session can/will still be active to the remote user who is connected to it. – blitz_jones Jun 26 '15 at 19:16
2

Working solution for multiple users logged on!
Not working for remote users... solution WIP

dim islocked

do
    call checklock
loop while  islocked = 0


'=================================
'Functions
'=================================

function checklock
    Dim computer : computer = "."

    If WScript.Arguments.Count = 1 Then
        computer = WScript.Arguments(0)
    End If

    If locked(computer) Then
        msgbox "debugging: locked"  
    Else
        msgbox "debugging: not locked"
        wscript.sleep 3000 'for debugging - allow time to enter lock screen
    End If      
end function


Function locked(computer)
    Dim wmi : Set wmi = GetObject("winmgmts://" & computer & "/root/cimv2")
    Dim lockapp_count : lockapp_count = wmi.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'lockapp.exe'").Count
    Dim explorer_count : explorer_count = wmi.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'explorer.exe'").Count
    locked = (lockapp_count >= explorer_count)
End Function

Other nitty gritty background:
Solution ISSUE (on machine w/ multiple users) I was using this for a while until I added another user onto the machine. Now when the OTHER user has locked their screen, even though my screen is active, not locked, the VBS says it's locked, b/c logonUI.exe is running - so it throws a false positive. It's also tricky b/c the process is run by the system not by the user so you can't do a cross check. I do wonder though, if you could count the number of lockapp.exe processes and compare to the number of logonui.exe processes. However - I don't know if they are always one to one.

WIP Solution - this is found to work - yay - solution and code moved to the top of my comment. Count the number of active users and compare the count to the number of logonui.exe processes. To do this I'm trying to count how many times explorer.exe is found, then comparing to the number of logonui.exe instances.

Failed solution 1:
Compare count of logonui.exe to lockapp.exe count Reason - these must go together, i was assuming you'd only ever get 1 logonui process but multiple lockapp processes; wrong assumption.

h pic
  • 51
  • 3