Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMI_Test
Module Test
Sub Main()
WMI_ConnectionTest("10.0.0.155") ' < Remote BIOS Name in UCase or IP number
End Sub
Public Sub WMI_ConnectionTest(ByVal cDomain As String)
Dim _ms As ManagementScope
Dim _moc As ManagementObjectCollection
Dim _mo As ManagementObject
Dim _op As New ConnectionOptions
Dim _Domain As String
Dim n As Integer
'* Step 1 : check if the remote computer is on-line
n = WMI_GetPingStatus(cDomain)
'* Convert ping result to message text and show it
Console.WriteLine( cDomain & ": " & System.Enum.GetName(GetType(WMI_PingStatusType),n))
If n <> WMI_PingStatusType.OnLine Then Exit Sub
' adjust domain/remote PC name /IP address, if needed
If cDomain.LastIndexOf("\") <> -1 Then
_Domain = cDomain
Else
_Domain = "\\" & cDomain
End If
'* Step 2 : try to gain access to remote WMI root namespace
Console.Write("Open connection to " & cDomain & "\root: ")
_op.Impersonation = ImpersonationLevel.Impersonate
_op.Authentication = AuthenticationLevel.Packet
Try
_ms = New System.Management.ManagementScope(_Domain & "\root", _op)
_ms.Connect
Console.WriteLine("success.")
Catch e As Exception
Console.WriteLine("failed. " & e.Message)
exit sub
End Try
_ms = Nothing
'* Step 3 : try to gain access to the \root\cimv2 namespace
Console.Write("Open connection to " & cDomain & "\root\cimv2: ")
Try
_ms = New System.Management.ManagementScope(_Domain & "\root\cimv2", _op)
_ms.Connect
Console.WriteLine("success.")
Catch e As Exception
Console.WriteLine("failed. " & e.Message)
exit sub
End Try
'* Step 4 : retrieve OS info
_moc = WMI_GetObjects(_Domain, "Win32_OperatingSystem")
Console.Write("O.S.:")
For Each _mo In _moc
Console.WriteLine(_mo.GetPropertyValue("Caption") & ", " & _mo.GetPropertyValue("CSDVersion"))
Next
'* Step 5 : Retrieve installed logical printer info
Try
_moc = WMI_GetObjects( _Domain,"Win32_Printer")
Console.WriteLine( _moc.Count.ToString & " printer objects found.")
Catch e As Exception
Console.WriteLine("Could not retrieve printer object(s)." )
End Try
_ms = Nothing
End Sub
Private Function WMI_GetPingStatus(Byval cAddress as String) As Integer
'
' WMI_PingStatus
'
Dim _RetVal As Integer = -1
Dim _moc As ManagementObjectCollection = WMI_GetObjects("","Win32_PingStatus Where Address = '" & cAddress & "'")
Dim _mo As ManagementObject
For Each _mo In _moc
_RetVal = Convert.ToInt32(_mo.properties("StatusCode").Value)
Next
Return _RetVal
End Function
Private Function WMI_CreateConnection(ByVal cDomain As String) As ManagementScope
Dim _ms As ManagementScope
Dim _op As New ConnectionOptions
_op.Impersonation = ImpersonationLevel.Impersonate
' Create WMI Connection
Try
If cDomain = Nothing Then
_ms = New System.Management.ManagementScope("\root\cimv2", _op)
Else
Select Case cDomain.LastIndexOf("\\")
Case -1
_ms = New System.Management.ManagementScope("\\" & cDomain.ToString & "\root\cimv2", _op)
Case Else
_ms = New System.Management.ManagementScope(cDomain.ToString & "\root\cimv2", _op)
End Select
End If
_ms.Connect()
Catch e As exception
Messagebox.Show("Could not establish WMI connection with: " & cDomain,"Attention:", MessageBoxButtons.Ok, MessageboxIcon.Exclamation)
End Try
Return _ms
End Function
Private Function WMI_GetObjects(ByVal cDomain As String, ByVal cWMIObjectType As String) As ManagementObjectCollection
Dim _mos As ManagementObjectSearcher
Dim _moc As ManagementObjectCollection
Dim _ms As ManagementScope
' Create WMI Connection
_ms = WMI_CreateConnection( cDomain)
If Not _ms Is Nothing Then
' Retrieve WMI Objects
Try
_mos = New System.Management.ManagementObjectSearcher(_ms.Path.ToString, "Select * From " & cWMIObjectType)
_moc = _mos.Get
Catch e As Exception
Messagebox.Show("Could not retrieve object(s) from: " & cDomain,"Attention:", MessageBoxButtons.Ok, MessageboxIcon.Exclamation)
End Try
End If
Return _moc
End Function
Private Enum WMI_PingStatusType As Integer
EmdekError = -1
OnLine = 0
BufferTooSmall = 11001
DestinationNetUnreachable = 11002
DestinationHostUnreachable = 11003
DestinationProtocolUnreachable = 11004
DestinationPortUnreachable = 11005
NoResources = 11006
BadOption = 11007
HardwareError = 11008
PacketTooBig = 11009
RequestTimedOut = 11010
BadRequest = 11011
BadRoute = 11012
TTLExpiredTransit = 11013
TTLExpiredAssembly = 11014
ParameterProblem = 11015
SourceQuench = 11016
OptionTooBig = 11017
BadDestination = 11018
NegotiatingIPSEC = 11032
GeneralFailure = 11050
End Enum
End Module
End Namespace