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

VBScript检测远程工作站登录用户均返回本地用户名问题求助

核心错误原因
  1. 初始版本脚本仅在启动时查询了一次本地计算机的Win32_ComputerSystem信息,遍历工作站列表时反复复用本地查询结果,因此所有返回的用户名均为本地当前登录用户。
  2. 第一次修改后的脚本仍然没有解决核心问题:初始化WMI对象时使用的地址为winmgmts:\\.\root\cimv2,其中.代表本地计算机,仅在查询语句中增加where Name条件,依然是在本地WMI库中检索数据,并未实际访问目标远程工作站,因此无法获取远程设备的登录信息。
修复后的完整代码
Const ForReading = 1, ForWrite = 2, ForAppend = 8

Set Fso = CreateObject("Scripting.FileSystemObject")
listPath = Fso.GetParentFolderName(WScript.ScriptFullName) & "\serverlist_login.txt"

If Fso.FileExists(listPath) Then
    Set Ts = Fso.OpenTextFile(listPath, ForReading)
    strText = Ts.ReadAll
    Ts.Close

    Results1 = ""
    Results2 = ""

    For Each i In Split(strText, vbCrLf)
        i = Trim(i)
        If i = "" Then 
            ' 跳过空行
            Continue For
        End If

        ' 连接远程工作站WMI服务
        On Error Resume Next
        Set remoteWmi = GetObject("winmgmts:\\" & i & "\root\cimv2")
        If Err.Number <> 0 Then
            Results2 = Results2 & " 无法连接到工作站 : " & i & ",错误信息:" & Err.Description & vbCrLf
            Err.Clear
            On Error GoTo 0
            Continue For
        End If
        On Error GoTo 0

        ' 查询远程工作站登录用户
        Set systems = remoteWmi.ExecQuery("select UserName from Win32_ComputerSystem")
        For Each system In systems
            If system.UserName <> "" Then
                Results1 = Results1 & " User is connected  : " & i & vbCrLf & system.UserName & vbCrLf 
            Else
                Results2 = Results2 & " No user is connected : " & i & vbCrLf
            End If 
        Next
    Next

    ' 写入结果文件
    Set Ts = Fso.CreateTextFile("results_login.txt")
    Ts.WriteLine "Login Results" & vbCrLf & " Date And Time : " & Now & Vbcrlf
    Ts.WriteLine Results1
    Ts.WriteLine Results2
    Ts.Close()  

Else
    MsgBox vbTab &_
        "Error" & vbcrlf &_
        "Missing, the serverlist_login.txt to process. You" & vbCrLf & _
        "must create a serverlist_login.txt with an IP or" & vbCrLf & _
        "Computer Name, with one per line",4128,"Error"   
End If
运行前置要求
  • 执行脚本的账号需要具备目标远程工作站的WMI远程访问权限,建议将账号加入目标设备的本地管理员组
  • 目标工作站需要开启WMI服务,且防火墙放行135端口及WMI动态端口范围
  • serverlist_login.txt每行填写一个有效的工作站名称/IP,避免多余空格、空行

内容的提问来源于stack exchange,提问作者Edward Sheriff Curtis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 01:48:02