简体   繁体   English

如何查找用户在某个时间实例已登录或锁定

[英]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. 该脚本将使用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. 如果有多个用户,则必须查找LogonUI.exe进程及其正在运行的会话。您可以使用下面的代码对其进行测试。 Works fine on my terminal server. 在我的终端服务器上工作正常。 Please make sure that PowerShell is running with elevated permissions. 请确保PowerShell以提升的权限运行。

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 希望能有所帮助,斯坦尼斯拉夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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