Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if computer is locked using VBscript

Tags:

vbscript

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

like image 332
dilip kumarvignesh Avatar asked Aug 30 '14 08:08

dilip kumarvignesh


2 Answers

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."
like image 57
Bond Avatar answered Nov 14 '22 08:11

Bond


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.

like image 2
h pic Avatar answered Nov 14 '22 08:11

h pic