How to find Computer Screen is Locked or not using Powershell

Hi All,

I am performing a GUI Based Automation through PowerShell , but the problem I am facing is the script will only run on an interactive session if the machine is locked, it waits to get unlocked. So is there any way to know if the machine is locked and if yes then how to login or make it run, just saying :stuck_out_tongue:

Lets have some idea on the this, and see this possibility.

You want the script only to run while locked?

Look for EventID=4634 in the eventlog

1…10 | % {

$currentuser = gwmi -Class win32_computersystem | select -ExpandProperty username
$process = get-process logonui -ea silentlycontinue

if($currentuser -and $process){“computer is locked”}else{“unlocked”}

start-sleep 5

}

Amar,

This question came up a few years ago in one of my Server classes. Here is my posted solution. It is a combination of PowerShell and Advanced Auditing.

Jason