[CmdletBinding()] param( [string]$Csv ) $ErrorActionPreference = 'Stop' $quser = (& quser.exe 2>&1 | Out-String).Trim() Write-Host 'quser:' if ($quser) { Write-Host $quser } else { Write-Host 'quser не вернул данных.' } $rows = @(Get-CimInstance -ClassName Win32_LogonSession -ErrorAction Stop | Where-Object { $_.LogonType -in @(2, 10, 11, 12, 13) } | ForEach-Object { $type = switch ($_.LogonType) { 2 { 'Interactive' } 10 { 'RemoteInteractive' } 11 { 'CachedInteractive' } 12 { 'CachedRemoteInteractive' } 13 { 'CachedUnlock' } default { 'Other' } } [pscustomobject]@{ LogonId = $_.LogonId LogonType = $_.LogonType Type = $type StartTime = $_.StartTime AuthenticationPackage = $_.AuthenticationPackage Status = $_.Status } } | Sort-Object StartTime) Write-Host ("Интерактивных сессий: {0}" -f $rows.Count) $rows | Format-Table -AutoSize if ($Csv) { $parent = Split-Path -Parent $Csv if ($parent -and -not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent | Out-Null } $rows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "Отчёт сохранён: $Csv" } Write-Host 'Сессии только перечислены, завершение сессий не выполнялось.'