Connect to a remote server with different user/password using .NET/WMI

I’ve seen various posts in the newsgroups how to use .NET to connect to a remote server to collect WMI information. 

Dim options As New ConnectionOptions()
options.Username = “DomainUserId”
options.Password = “password”

Dim scope As New ManagementScope(“\ServerNamerootcimv2”, options)
Dim strSVCquery As String = ConfigurationSettings.AppSettings(“NICquery”)

Dim objNICQuery As New WqlObjectQuery(strSVCquery)
Dim objNICsearcher As New ManagementObjectSearcher(scope, objNICQuery)

Dim envVar As New ManagementObject()

Dim objNICItem As PropertyData
Dim strNICColName As String

scope.Connect()
For Each envVar In objNICsearcher.Get
        For Each objNICItem In envVar.Properties
            strNICColName = objNICItem.Name
            If Not IsArray(objNICItem.Value) Then
                Console.WriteLine(“Item is NOT an array — ” & strNICColName)
            Else
                Console.WriteLine(“Item is an array — ” & strNICColName)
            End If
        Next
Next

Add a Comment