简体   繁体   中英

How to find user is logged in or locked at a instance of time

There are multiple users logged into the machine. for each user I wanted to print the status (logged in or locked) continuously for each user logged in to the machine. how can get the status of the user. this script will run on each user login using the Scheduler.

$user = $env:UserName
do 
{
    # Get system Status   (Locked or Logged in )
    print(status)
    Start-Sleep -s 30
} while(1)

How to get the status. Please help me on this.

in case there are multiple users, you have to look for LogonUI.exe process and the session it is running in. You can test it with code below. Works fine on my terminal server. Please make sure that PowerShell is running with elevated permissions.

for ($i = 0; $i -lt 10; $i++) {
    $process = @(Get-WmiObject -Class Win32_Process -Filter "Name = 'LogonUI.exe'" -ErrorAction SilentlyContinue | Where-Object {$_.SessionID -eq $([System.Diagnostics.Process]::GetCurrentProcess().SessionId)})
    if ($process.Count -gt 0) {
        Write-Host "Computer is locked"
    }
    else {
        Write-Host "Computer is unlocked"
    }
    Start-Sleep -Seconds 5
}

Hope it helps, Stanislav

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM