Powershell - как проверить зарегистрированных пользователей на указанных c машинах - PullRequest
0 голосов
/ 08 января 2020

У меня есть код для проверки на локальных машинах зарегистрированных сеансов, как показано ниже

Get-WmiObject win32_networkloginprofile | ? {$_.lastlogon -ne $null} | % {[PSCustomObject]@{User=$_.caption; LastLogon=[Management.ManagementDateTimeConverter]::ToDateTime($_.lastlogon)}}

Можно ли проверить его для определенных c машин зарегистрированных сеансов, даже тех, которые имеют статус «отключен» ?

1 Ответ

1 голос
/ 08 января 2020

Очевидно, вам нужны права на целевых компьютерах:

Function Get-LoggedOnUser {
    param(
        [CmdletBinding()] 
        [Parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string[]]$ComputerName = 'localhost'
    )
    begin {
        $ErrorActionPreference = 'Stop'
    }
    process {
        foreach ($Computer in $ComputerName) {
            try {
                quser /server:$Computer 2>&1 | Select-Object -Skip 1 | ForEach-Object {
                    $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
                    # If session is disconnected different fields will be selected
                    if ($CurrentLine[2] -eq 'Disc') {
                        [pscustomobject]@{
                            UserName = $CurrentLine[0];
                            ComputerName = $Computer;
                            SessionName = $null;
                            Id = $CurrentLine[1];
                            State = $CurrentLine[2];
                            IdleTime = $CurrentLine[3];

                            LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
                        }
                        # LogonTime = $CurrentLine[4..6] -join ' ';
                    }
                    else {
                        [pscustomobject]@{
                            UserName = $CurrentLine[0];
                            ComputerName = $Computer;
                            SessionName =  $CurrentLine[1];
                            Id = $CurrentLine[2];
                            State = $CurrentLine[3];
                            IdleTime = $CurrentLine[4];
                            LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
                        }
                    }
                }
            }
            catch {
                New-Object -TypeName PSCustomObject -Property @{
                ComputerName = $Computer
                Error = $_.Exception.Message
                } | Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...