Re: wmi and vb.net

From: Al Edlund (edlund_at_ameritech.net)
Date: 12/23/04


Date: Wed, 22 Dec 2004 19:27:16 -0600


This is pretty much of the template I use for both local and remote (I use
it on WinXP-SP2 source and target)
Al

Private Sub subWmiNetAdapterConfig()

Dim strComputer As String

Dim strUserName As String

Dim strUserPass As String

Dim strDomain As String

Try

' domain name can be required for remote when different than local

strDomain = txtWMI_Domain.Text

strUserName = txtWMI_UserName.Text

If strDomain <> "" Then

strUserName = strDomain & "\" & strUserName

End If

strUserPass = txtWMI_UserPass.Text

strComputer = txtWMI_Workstation.Text

' if target is blank do the local

If strComputer = "" Then strComputer = "."

' Build a connection options object

Dim cOptions As New ConnectionOptions

cOptions.Username = strUserName

cOptions.Password = txtWMI_UserPass.Text

cOptions.Authentication = AuthenticationLevel.PacketPrivacy

cOptions.Impersonation = ImpersonationLevel.Impersonate

cOptions.Locale = strLocale

Dim eOptions As New EnumerationOptions

eOptions.EnumerateDeep = False

eOptions.ReturnImmediately = True

eOptions.Rewindable = False

Dim mPath As New ManagementPath

With mPath

.Server = strComputer

' this is the namespace as a constant

.NamespacePath = strNameSpace

' this is the class name as a constant

.ClassName = strClassNetConf

End With

Dim mScope As New ManagementScope

With mScope

.Path = mPath

If strComputer <> "." Then

.Options = cOptions

End If

End With

' build query

Dim mQuery As New SelectQuery("SELECT * FROM " & strClassNetConf)

' build searcher

Dim mSearcher As New ManagementObjectSearcher(mScope, mQuery)

'subLogMessage("management searcher")

Dim strObjectId As String

Dim mObject As ManagementObject

For Each mObject In mSearcher.Get()

    ' do some kind of processing with the returned stuff

Next mObject

Catch err As Exception

subLogException(err)

subDisplayException(Nothing, err)

End Try

End Sub

"Bryan Z" <BryanZ@discussions.microsoft.com> wrote in message
news:5B8855A6-458E-4DB2-83B1-F38EEE5769BA@microsoft.com...
> Thanks for the reply. I set up auditing on the machine I am trying to
> connect to and also set WMI logging to verbose on both my machine and the
> machine I am trying to connect to. The only thing I can find on either
> machine is the following message:
>
> (Wed Dec 22 08:12:13 2004.172879093) : ConnectViaDCOM, CoCreateInstanceEx
> resulted in hr = 0x80070005
>
> That is on my machine, in the file wbemprox.log, I cannot find any
> references to my attempted login on the remote PC.
>
> Like I said in my previous message, wmitest.exe does work when I try to
> connect to other machines from my machine. So, I'm thinking that in my
> connect process in my vb program, I'm leaving a step out. I've looked at
> many posts in this newsgroup where other people had similar problems. But
> they were using C#, and it's been a very long time since I last used C or
> any
> other flavor of C. I'll try to find one of those posts and then post a
> link
> to them in this thread.
>
> Thanks for any info you might have.
>
> Bryan
>
> "Al Edlund" wrote:
>
>> there are differences among operating systems and the returned error
>> codes.
>> Could you maybe share what is at
>> the other end and what returns you are getting? Also one of the things
>> you
>> might consider doing is to turn on auditing in the remote system and see
>> what errors are being flagged over there.
>> al
>>
>> "Bryan Z" <Bryan Z@discussions.microsoft.com> wrote in message
>> news:F854A326-9DCB-4205-9C6A-A12574B4DB6A@microsoft.com...
>> > I've been messing around with WMI and VB.NET. I was just trying to
>> > write
>> > a
>> > test app first, to become more familiar with accessing WMI through a
>> > VB.NET
>> > program. The following code works fine as long as I point it to a my
>> > local
>> > computer. But, when I try to access a remote computer, I always get
>> > Access
>> > Denied. The username and password are for a Domain Admin account and
>> > I've
>> > double and triple checked WMI access settings on the remote computer.
>> > I've
>> > had a hard time finding reliable documentation on accessing a remote
>> > computer's WMI info via Visual Basic. I've found a bunch of stuff on
>> > vb
>> > scripting and WMI and I've tried to convert that code to real VB code,
>> > but
>> > I
>> > still get Access Denied.
>> > Here is the code, with usernames/passwords removed. Any help would be
>> > much
>> > appreciated!!
>> >
>> > ------------------
>> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>> > System.EventArgs) Handles Button1.Click
>> >
>> > Dim j As New ConnectionOptions()
>> >
>> > j.Impersonation = ImpersonationLevel.Default
>> > j.Authentication = AuthenticationLevel.Packet
>> > j.EnablePrivileges = True
>> > j.Username = "DomainAdmin"
>> > j.Password = "AdminPassword"
>> >
>> > Dim path As New ManagementPath("\\remotePC\root\cimv2")
>> > 'Dim path As New
>> > ManagementPath("\\.\root\cimv2:Win32_NTEventLogFile")
>> > Dim k As New ManagementScope("\\remotePC\root\cimv2", j)
>> > Dim disk As New ManagementObject(k, New
>> > ManagementPath("Win32_logicaldisk='c:'"), Nothing)
>> > disk.Get()
>> > 'k.Connect()
>> > 'k.Path = New ManagementPath()
>> > End Sub
>> >
>> > ----------------
>> >
>> > As you can see, I've been trying a few different things. I'm just
>> > trying
>> > to
>> > get this to connect at this point. Once I do that, then I'll try
>> > running
>> > some queries and such. And, also, I've tried running this while logged
>> > in
>> > as
>> > a Domain Admin. And the user and password combo work fine in the
>> > wmitest
>> > application.
>> >
>> > Thanks in Advance.
>> > Bryan
>>
>>
>>