1

Can anyone help with code snippet for checking if the workstation is locked? I tried with getting the document.title, so that when the workstation is locked it returns blank for document.title. This is not working.

I am encoding wscript in vbscript which is residing in HTML.

Any help with Javascript is also fine.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I think it would be helpful if you included which OS you are referring to. – Wayne Koorts Jan 12 '09 at 09:02
  • 1
    What are you trying to accomplish with this? More information would be helpful. – Tester101 Jan 13 '09 at 20:16
  • According to [this Microsoft article](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation), "There is no function you can call to determine whether the workstation is locked." This must be monitored using the SessionSwitchEventHandler. More information can be found on this question: https://stackoverflow.com/q/44980/2657515 – JonathanDavidArndt Mar 12 '20 at 02:30

4 Answers4

2

Normally you should be using a wmi query to look at the event log's security log. Howver, XP doesn't have a lock event ids (vista/win7/2008 does have both lock and unlock event ids).

XP DOES have an unlock event, event though. Most people don't realize the unlock event invovles BOTH a logon/logoff events (event ids 528/538, which occur at practically the same time). The Login Type will be EventType 7, and that's how you tell someone is unlocking the workstation. Login Type 7 (whether logon or logoff) is an unlock event.

For Vista/Win7/2008 the new workstation lock event id is 4800 wile the updated way of determining an unlock event is event id 4801. However, the source log in post Vista OSes is not the Security log, but the Microsoft-Windows-Security-Auditing log.

If you have Win7 your in luck, and should just write a wmi query. XP, I can't see a good way of doing it, unless you want to try idle time.

waydaws
  • 21
  • 2
1

One indicator that works (on Windows 7 SP1 with a single user and no remote desktop) is to check for the presence of the LogonUI.exe process, which is only there when the workstation is locked. See the following islocked.vbs, which can also be passed a remote computer name.

' Source:
'   http://community.spiceworks.com/scripts/show/1965-detect-screen-lock-status
Dim computer : computer = "."
If WScript.Arguments.Count = 1 Then
    computer = WScript.Arguments(0)
End If

Function IsWorkstationLocked( computer )
    Dim wmi : Set wmi = GetObject("winmgmts://" & computer & "/root/cimv2")
    Dim logonScreenCount : logonScreenCount = wmi.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'LogonUI.exe'").Count

    IsWorkstationLocked = (logonScreenCount > 0)
End Function

If IsWorkstationLocked(computer) Then
    WScript.Quit(0)
Else
    WScript.Quit(1)
End If
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

Not sure you can check the through a script. Most functions will run as expected. One thing that wouldn't work is sending keys (simulating key press or mouse moves), but you cannot do it from a script on html.
Perhaps you can check instead for idle time since the user's last activity?

Kobi
  • 135,331
  • 41
  • 252
  • 292
0

If there is a screensaver you may see a process like 'logon.scr'. That won't work though if you don't have the screensaver running. It is pretty easy with .Net though using this code.

You could create that as a COM object and call it from VBScript.

Rob Haupt
  • 2,104
  • 1
  • 15
  • 24