【问题标题】:Get Operating System Info from a network host (LAN connected PC)从网络主机(局域网连接的 PC)获取操作系统信息
【发布时间】:2018-10-05 14:11:00
【问题描述】:

我正在尝试从连接到本地网络的主机获取信息。以下代码在我的本地电脑中给出了操作系统名称,例如 Microsoft Windows 10 Pro。当我使用网络PC名称时提示错误Access Denied

我的理解是,它没有获得从该 PC 收集信息的许可,因为没有提供凭据。所以,我的问题是如何为这些代码提供凭据,以便获得收集权限。

注意:我在远程 PC 上的 windows 防火墙中添加了异常 Windows Management Instrumentation (WMI)。

Sub GetOS()
    If getOperatingSystem <> "" Then
        MsgBox getOperatingSystem()
    End If
End Sub

'------------- Function to get Operating System Info --------------

Public Function getOperatingSystem()
    Dim localHost       As String
    Dim objWMIService   As Variant
    Dim colOperatingSystems As Variant
    Dim objOperatingSystem As Variant

    On Error GoTo Error_Handler

    'localHost = "." 'Technically could be run against remote computers, if allowed
    localHost = "SCANNER-PC"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem In colOperatingSystems
        getOperatingSystem = objOperatingSystem.Caption '& " " & objOperatingSystem.Version
        Exit Function
    Next

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "Error No: " & Err.Number & vbCrLf & "Description: " & Err.Description
    Resume Error_Handler_Exit
End Function

【问题讨论】:

  • 第一次退出函数时,For Each 的目的是什么?您只对一个结果感兴趣吗?
  • 对于每个,因为如果我可以获得与operating system 相关的信息,那么我也将能够获得disk drive、memory (RAM)、Processor 信息。在那种情况下,如果有多个处理器、磁盘或内存,那么我可以获得所有这些信息。
  • 如果没有for each循环也可以获取信息,那就没问题了。我没关系。

标签: vba excel networking operating-system


【解决方案1】:

最后我能够将凭据传递给 WMI 查询。以下子查询使用凭据在远程PC上的信息。

Public Sub WMIQueryCRED()
Dim objSWbemLocator As Object
Dim objWMIService As Object
Dim colItems As Object
Dim strHost, strUserID, strPassword As String

strHost = "NetworkHost"
strUserID = "Domain\domainadmin"
strPassword = "Password"

    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strHost, "root\cimv2", strUserID, strPassword)
    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

    For Each objItem In colItems
        MsgBox objItem.Caption, vbInformation, "Successfull"
    Next

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2010-10-31
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多