Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI in VB.Net

Status
Not open for further replies.

Tomeczek

Programmer
Oct 25, 2012
40
0
0
US
I am trying to write VB.Net program, which would connect to remote computers and retrieve specific system information using WMI.
I must admit, that I am not experienced in VB.Net (wrote tons of programs in VB6, though, several of them using WMI). I cannot pass the first phase: connect to remote (or even local) WMI namespace.
Here is the first part of the code, which fails:

Code:
Imports System.Management
.
.
        Dim myConnOpt As New System.Management.ConnectionOptions
        With myConnOpt
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet
        End With
        
        Dim myMgmtScope As System.Management.ManagementScope
        Dim sServer As String = "."	‘ or “<server name>”

        myMgmtScope = New System.Management.ManagementScope("\" & sServer & "\root\cimv2", myConnOpt)
        Try
            myMgmtScope.Connect()           ' connect to WMI namespace
        Catch
            If myMgmtScope.IsConnected = False Then
                MsgBox("Could not connect to WMI namespace on \" & sServer)
                Exit Sub
            End If
        End Try

If fails either on myMgmtScope = New ... (on Windows XP) or on myMgmtScope.Connect() on Windows7.

Can someone, please, try to help me?
Thanks!
 
Sorry, it always fails on myMgmtScope = New ...
 
Are you getting any error messages? If so, please post them so we can take a look.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
When running within the Studio, it stops on

myMgmtScope = New System.Management.ManagementScope("\" & sServer & "\root\cimv2", myConnOpt)

saying "Invalid parameter".

You can test it creating in the Studio new "Console application" and putting "Imports" line on top and the rest of the code under "Sub Main()".
You need to add "System.Management" reference (from .NET tab) in Project | Add Reference... menu.

Thanks for your help!
 
The command should have two slashes, sorry.

myMgmtScope = New System.Management.ManagementScope("\\" & sServer & "\root\cimv2", myConnOpt)
 
SOLVED!

The code is good. I moved the project on another machine and it works like a champ.
Possibly the first machine (XP) had corrupted WMI installed (that's what Google says).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top