[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Determine if a Workstation is locked via a script

Posted on 2009-04-16
Medium Priority
Last Modified: 2012-05-06
A recent MS security patch has broken my solution. So now I need to find another way. I need to be able to remotely determine if a Windows machine is locked or unlocked when a user is logged in. I use to be able to run a program on a remote machine interactively using, psexec -i, that would check the position of the mouse. If it had a crazy number, the machine was locked. Well apparently a recent patch has broken the -i option in psexec. So I can no longer remotely run programs that will interact with the console session. Atleast that is what it looks like.  Any ideas?  VBscript is prefered since all windows machines have that in our organization.
Question by:Unisys1
  • 3
  • 2
  • 2
LVL 47

Accepted Solution

Donald Stewart earned 2000 total points
ID: 24170791
Here's a script I have used in the past, you will also need lsgrab in the same directory as the script.

'Script version 2.0
'Valid command-line argument is:
'computer:computername|IP Address
'path:"x:\fullpath"|".\" (current directory
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
Set objArgs = WScript.Arguments
on error resume next
Dim strComputer, strMsg
strScriptPath = replace(wscript.scriptfullname,wscript.scriptname,"")
'Set the screenshot grabber path in relation to the script path
strLsGrab = chr(34) & strScriptPath & "lsgrab\lsgrab.exe" & chr(34)
strTempFile = WshSysEnv("TEMP")
'Get command-line arguments for a passed computername or IP address.
If objArgs.Count > 0 Then 
	 For I = 0 to objArgs.Count - 1
   If InStr(1,LCase(objargs(I)),"computer:") Then
   	strComputerArray = split(lcase(objargs(I)),"computer:")
   	strComputer = strComputerArray(1)
   ElseIf InStr(1,LCase(objargs(I)),"path:") Then
   	strPathArray = split(lcase(objargs(I)),"path:")
   	strPath = strComputerArray(1)
   End If
ElseIf objargs.count = 0 then
	strComputer = inputbox("Enter the name of the computer you wish to enable and retreive a screen capture " _
	 & "from","Enter a computername")
	If strComputer = "" Then wscript.quit
End If
if strPath = "" then strPath = ".\"
'If the passed computername contains any backslashes, remove them.
If InStr(strComputer,"\") Then strComputer = replace(strComputer,"\","")
dim bFatal
'Check to see if a user is logged in remotely.
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT UserName FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
   For Each objItem In colItems
      strUser = objItem.UserName
   if err.number <> 0 then 
    strUser = ""
    'msgbox err.number & " " & err.description
		bFatal = true
	 if err.number = 451 then strMsg = "Could not enumerate WMI class.  You may not have permissions on the remote computer."
   end if
If bFatal <> true then 
  Call Screenshot(strComputer)
  Msgbox "Cannot acquire remote screenshot." & vbcrlf & vbcrlf & strMsg,48,"Screenshot unavailable."
End If
Function Screenshot(strComputer)
	Set WshShell = WScript.CreateObject("WScript.Shell")
  'Finally, run the screen grabber command
  strCommand = strLsGrab & " /c:" & strComputer & " /p:" & chr(34) & WshSysEnv("TEMP") & "\" & chr(34)
  'strInput = inputbox("the path is: ","test",strCommand)
  Set oExec = WshShell.Exec(strCommand)
  'msgbox oExec.stdout.readall
  if instr(oExec.stdout.readall,"denied") then
    msgbox "You do not have sufficient permissions on '" & strComputer & "' to retrieve the remote screen capture."
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
    strFile = WshSysEnv("TEMP") & "\" & strComputer & ".jpg" 
    'msgbox strFile
    Set f = objfso.GetFile(strFile)
    set objfso = nothing
    if f.size < 20000 Then 'less than 20kb
       msgbox "Remote workstation was locked, so no screenshot was available.",48,"No screenshot created"
  	'msgbox "Now attempting to open " & chr(34) & WshSysEnv("TEMP") & "\" & strComputer & ".jpg" & chr(34)
  	   on error resume next
  	   wshshell.run chr(34) & WshSysEnv("TEMP") & "\" & strComputer & ".jpg" & chr(34),1,false
    End If
  End if
End Function

Open in new window

LVL 66

Expert Comment

ID: 24174145
Still use PSEXEC, but instead of running a script, just run tasklist, without the Interactive switch.

If you see logon.scr, should be locked.....

Or even this. Copy it to a batch file named locked.bat, in the same directory as psexec

psexec \\RemotePC -c c:\Path\To\locked.bat

@echo off
tasklist | find /i "logon.scr"
if %errorlevel%==0 goto lock
echo PC is unlocked.

echo PC is locked


Author Comment

ID: 24188806
Very nice simple script, this is only working if the screen saver is running.  Although will help in a pinch.  If they just locked their machine it doesn't help.  
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 47

Expert Comment

by:Donald Stewart
ID: 24188852
Did you try the option i gave above? it works for me, as I use it in conjunction with a remote assistance script
LVL 66

Expert Comment

ID: 24191065
Yea, assumed that it wouldnt have been a manual lock......

Let me investigate in the am....

Author Comment

ID: 24234170
dstewartjr, so I have to use this in conjunction with other software?
LVL 47

Expert Comment

by:Donald Stewart
ID: 24234195
You would download lsgrab from the link I provided, you then place it and the script in same folder and run the script from there.

I came across this from here:


Download and give this app a look


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question