error using wmi to access username on remote machine

andresleon

Member
Joined
Jun 22, 2005
Messages
9
Programming Experience
1-3
i am trying to get the username from a user logged in to a remote machine. I am trying to use wmi to obtain such info in the following sub:

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnGetUserName_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) _
 
[/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnGetUserName.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] objWMIService [/size][size=2][color=#0000ff]As[/color][/size][size=2] Management.ManagementObject
 
[/size][size=2][color=#008000]
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strPath [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "WinMgmts:" & _
 
"{impersonationLevel=impersonate}\\" & txtComputerName.Text & "\root\cimv2"
 
objWMIService = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObject(strPath)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] colComputer [/size][size=2][color=#0000ff]As[/color][/size][size=2] Management.ManagementQuery
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selectQuery [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] SelectQuery("Select * from Win32_ComputerSystem")
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] searcher [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObjectSearcher(selectQuery)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mgmObject [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementObject
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strUserName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strCompName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] mgmObject [/size][size=2][color=#0000ff]In[/color][/size][size=2] searcher.Get()
 
strUserName = mgmObject("UserName").ToString()
 
strCompName = mgmObject("name").ToString
 
[/size][size=2][color=#0000ff]Next[/color][/size][size=2] mgmObject
 
txt1.Text = strUserName
 
MsgBox("Done")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

the problem is that when i reach the following line:
VB.NET:
Dim searcher As New ManagementObjectSearcher(selectQuery)
i get the following error:

An unhandled exception of type 'System.Management.ManagementException' occurred in system.management.dll
Additional information: Invalid parameter

i think the problem is how i have constructed the winmgmts path, but all examples i have found follow this same patter. Can someone shed some light on this issue?

Thank you!
 
i am terribly sorry. The error i am getting appears to be in this line
VB.NET:
[size=2][size=2]objWMIService = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObject(strPath)
[/size][/size]

thank you for any help you be able to provide :)
 
i don't see connection ... actually, i'm not absolutely positive but i think that MngObj is accepting two arguments.

try this:

PHP:
 Dim opt As ConnectionOptions = new ConnectionOptions 
With opt
opt.Impersonation = ImpersonationLevel.Impersonate 
opt.EnablePrivileges = true 
opt.Username = username 
opt.Password = password
End With
 
Dim strPath As ManagementPath = new ManagementPath("\\\\" & txtComputerName.Text & "\\root\\cimv2") 
 
objWMIService = New ManagementObject(strPath, opt)


Cheers ;)
 
Thank you for your assistance Kulrom.
When i typed to code you provided, the intellisense in studio .net tells me that the managementobject does not support two parameters in the way you provided. it doesnt allow the opt parameter.

what other way could i use to link the managementobject with the connection setings?

thanks again.
 
after entering your suggestions, this is what my codes looks like...

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnGetUserName_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) _
 
[/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnGetUserName.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] objWMIService [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementObject
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] opt [/size][size=2][color=#0000ff]As[/color][/size][size=2] ConnectionOptions = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ConnectionOptions
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] opt
 
opt.Impersonation = ImpersonationLevel.Impersonate
 
opt.EnablePrivileges = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#008000]'opt.Username = username
 
[/color][/size][size=2][color=#008000]'opt.Password = password
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strPath [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementPath = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementPath("\\" & txtComputerName.Text & "\root\cimv2")
 
objWMIService = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObject(strPath, opt1)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] colComputer [/size][size=2][color=#0000ff]As[/color][/size][size=2] Management.ManagementQuery
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selectQuery [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] SelectQuery("Select * from Win32_ComputerSystem")
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] searcher [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObjectSearcher(selectQuery)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mgmObject [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementObject
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strUserName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strCompName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] mgmObject [/size][size=2][color=#0000ff]In[/color][/size][size=2] searcher.Get()
 
strUserName = mgmObject("UserName").ToString()
 
strCompName = mgmObject("name").ToString
 
[/size][size=2][color=#0000ff]Next[/color][/size][size=2] mgmObject
 
txt1.Text = strUserName
 
MsgBox("Done") 
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

again, the following code appears to be incorrect according to studio's intellisense:

VB.NET:
[size=2][size=2]objWMIService = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementObject(strPath, opt1)
[/size][/size]

i do see that the constructor allows for an objectGetOptions object, but i dont know how to use this, or even if its the same as a connection properties as you seem to be using in this case...

sorry for being such a pain... this code has been kicking my butt for a while now...
 
Back
Top