You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中quser结合New-Object获取登录用户遇问题求助

解决方案

一、修复quser脚本的输出解析问题

原脚本直接将quser的原始文本输出赋值给LoggedInUsers属性,会导致结果格式混乱、错误无法处理。需要解析quser的输出为结构化数据,同时处理无用户登录或访问失败的情况。

解析quser输出的核心逻辑

quser的输出是固定格式的文本,需通过以下步骤处理:

  • 跳过第一行表头内容
  • 按多空格分割每行内容(兼容用户名含空格的场景)
  • 将解析后的用户信息与服务器名关联,生成结构化对象

二、解决RPC Server Unavailable错误

出现该错误的常见原因及对应解决办法:

  • 权限不足:运行脚本的账号需拥有目标机器的管理员权限
  • 防火墙限制:目标机器需开启WMI/RPC相关入站规则(如Windows Management Instrumentation (WMI-In)、Remote Desktop - User Mode (TCP-In))
  • WMI服务未启动:确保目标机器上的Windows Management Instrumentation服务处于运行状态
  • 网络问题:验证目标机器网络可达、DNS解析正常

三、优化后的完整脚本

Function Get-File($initialDirectory) {   
    Add-Type -AssemblyName System.Windows.Forms | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.InitialDirectory = $initialDirectory
    $OpenFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    $null = $OpenFileDialog.ShowDialog()
    $OpenFileDialog.FileName
}

$scopeservers = Get-File
if (-not $scopeservers) { exit }
$servers = Get-Content $scopeservers

foreach ($server in $servers) {
    $result = [PSCustomObject]@{
        Server        = $server
        LoggedInUsers = @()
        Status        = "Success"
    }

    try {
        # 执行quser并捕获输出,处理无用户的情况
        $quserOutput = quser /server:$server 2>&1
        if ($LASTEXITCODE -ne 0) {
            if ($quserOutput -match "No user exists") {
                $result.Status = "No users logged in"
            }
            else {
                $result.Status = "Error: $quserOutput"
            }
            $result
            continue
        }

        # 解析quser输出为结构化对象
        $headers = $quserOutput[0] -split '\s+' | Where-Object { $_ }
        for ($i = 1; $i -lt $quserOutput.Count; $i++) {
            $line = $quserOutput[$i] -split '\s{2,}' | Where-Object { $_ }
            $userObj = [PSCustomObject]@{
                Username   = $line[0]
                SessionName = if ($line[1] -notmatch '\d+') { $line[1] } else { $null }
                ID         = if ($line[1] -match '\d+') { $line[1] } else { $line[2] }
                State      = if ($line[1] -notmatch '\d+') { $line[3] } else { $line[2] }
                IdleTime   = if ($line[1] -notmatch '\d+') { $line[4] } else { $line[3] }
                LogonTime  = if ($line[1] -notmatch '\d+') { $line[5..($line.Count-1)] -join ' ' } else { $line[4..($line.Count-1)] -join ' ' }
            }
            $result.LoggedInUsers += $userObj
        }
    }
    catch {
        $result.Status = "Exception: $($_.Exception.Message)"
    }

    $result
}

脚本说明

  • 新增Status字段,明确标记每台机器的处理状态(成功、无用户、错误)
  • 解析quser输出为结构化用户对象,便于后续筛选、导出等操作
  • 捕获quser的错误输出,区分"无用户登录"和其他访问异常
  • 替换LoadWithPartialName为Add-Type(PowerShell官方推荐用法)

内容的提问来源于stack exchange,提问作者Damian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 00:06:27