从网络主机(连接局域网的PC)获取操作系统信息 [英] Get Operating System Info from a network host (LAN connected PC)

查看:65
本文介绍了从网络主机(连接局域网的PC)获取操作系统信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从连接到本地网络的主机获取信息.以下代码在我的本地计算机中给出了操作系统名称,例如 Microsoft Windows 10 Pro .当我使用网络PC名称时,提示错误访问被拒绝

I am trying to get information from host connected to local network. Following code give operating system name like Microsoft Windows 10 Pro in my local pc. When I use a network pc name then prompt error Access Denied

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

My understanding is, it is not getting permission to collect information from that PC because there is no credential provided. So, my question is how to provide credential to these codes so that it can get permission to collect.

注意:我向远程PC上的Windows防火墙添加了 Windows管理规范(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

推荐答案

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

Finally I am able to pass credentials to WMI query. The following sub query information on remote PC using credentials.

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

这篇关于从网络主机(连接局域网的PC)获取操作系统信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆