0

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.

surendra
  • 551
  • 3
  • 13
  • 27
  • if it's rdp users, see https://stackoverflow.com/questions/48767130/how-to-get-the-status-each-logged-in-user-status-details/48769426#48769426 – Avshalom Feb 14 '18 at 09:05
  • not rdp user. local user. – surendra Feb 14 '18 at 09:19
  • `$processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")`; `$Status = $processinfo | Foreach-Object {$_.GetOwner().User} | Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"} | Sort-Object -Unique | ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } | Select-Object Computer,LoggedOn`; `$Status`; Does something like the above help? – Vivek Kumar Singh Feb 14 '18 at 09:31
  • Dear Vivek, Thanks, but even though I locked the user it showing the same status. Computer LoggedOn -------- -------- . DK25784 . DK25784 . DK25784 . DK25784 – surendra Feb 14 '18 at 09:53

1 Answers1

0

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

  • Dear Stanislav, if more than one user logged into the machine. then it always says system is locked. case is : first user logged in and locked. when we are checking status in the second user login. even though second user logged in it says system is locked because first user is locked. how to handle this situation? I want user wise. Please help. – surendra Feb 14 '18 at 11:21
  • Then there is no way how to find this through the username as this process runs under SYSTEM account. The only way is to bound such process to sessionID. See updated answer. – Stanislav Castek Feb 14 '18 at 12:33