Unknown parameter with WMI mini-application...

R

R Hedges

Hi there-I'm an AMATEUR VB.NET programmer, who has hacked
together a small VB.NET application that can get
information from remote machines using the
System.Management namespace. MY APPLICATION DOES WORK,
but not quite in the way I want to. My management object
is defined like this:

Dim disk As New ManagementObject(myScope, New
ManagementPath("win32_operatingsystem='TheMachine'"),
Nothing)

However, it does not work correctly-the properties are
not retrieved for reasons I don't know. The original
code that I...stole from had a different ManagementPath,
but it does work.

Dim disk As New ManagementObject(myScope, New
ManagementPath("win32_logicaldisk='c:'"), Nothing)

I know WMI and VBScript, and of course VB.NET is
significantly more complex, but I don't quite know what
the problem is. Are my parameters wrong? Is my path
wrong? I don't even fully understand what I should be
looking for-I want to basically be able to run any query
that WMI support. If anyone can help me or at least
point me in a direction, I'd appreciate it.

Thank you









Here is the code (sorry for making it so lengthy):

Imports System
Imports System.Management
Public Class Form1
Inherits System.Windows.Forms.Form
Dim IPAddress As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
Friend WithEvents Button1 As
System.Windows.Forms.Button
Friend WithEvents ListBox1 As
System.Windows.Forms.ListBox
Friend WithEvents ListBox2 As
System.Windows.Forms.ListBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents ListBox3 As
System.Windows.Forms.ListBox
Friend WithEvents ListBox4 As
System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.ListBox3 = New System.Windows.Forms.ListBox
Me.ListBox4 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point
(176, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(40, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point
(168, 80)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "START"
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point
(176, 136)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120,
17)
Me.ListBox1.TabIndex = 2
'
'ListBox2
'
Me.ListBox2.Location = New System.Drawing.Point
(16, 168)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(552,
17)
Me.ListBox2.TabIndex = 3
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point
(112, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(176, 16)
Me.Label1.TabIndex = 4
Me.Label1.Text = "Insert the 3rd IP address
number"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(64,
136)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.TabIndex = 5
Me.Label2.Text = "The IP address is:"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(8,
104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(120, 16)
Me.Label3.TabIndex = 6
Me.Label3.Text = "The Machine Name is:"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point
(128, 48)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(40, 16)
Me.Label4.TabIndex = 7
Me.Label4.Text = "10.33."
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point
(232, 48)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(16, 16)
Me.Label5.TabIndex = 8
Me.Label5.Text = ".X"
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(56,
224)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(104, 16)
Me.Label6.TabIndex = 9
Me.Label6.Text = "The CPU Speed is:"
'
'Label7
'
Me.Label7.Location = New System.Drawing.Point(48,
264)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(112, 16)
Me.Label7.TabIndex = 10
Me.Label7.Text = "The vendor name is:"
'
'ListBox3
'
Me.ListBox3.Location = New System.Drawing.Point
(176, 224)
Me.ListBox3.Name = "ListBox3"
Me.ListBox3.Size = New System.Drawing.Size(392,
17)
Me.ListBox3.TabIndex = 11
'
'ListBox4
'
Me.ListBox4.Location = New System.Drawing.Point
(176, 264)
Me.ListBox4.Name = "ListBox4"
Me.ListBox4.Size = New System.Drawing.Size(120,
17)
Me.ListBox4.TabIndex = 12
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(592, 302)
Me.Controls.Add(Me.ListBox4)
Me.Controls.Add(Me.ListBox3)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Vital Information Collected
Electronically"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
For IPAddress = 123 To 125
MsgBox("Now testing " & IPAddress)
Dim TheMachine As String = "10.33." &
TextBox1.Text & "." & IPAddress
Dim Options As New ConnectionOptions
Dim oProperty As PropertyData
Dim oTest As PropertyDataCollection
Options.Username = "TheUsersName"
Options.Password = "TheUsersPassword"
Dim myScope As New ManagementScope("\\" &
TheMachine & "\root\cimv2", Options)
ListBox1.Items.Add("10.33." & TextBox1.Text
& "." & IPAddress)
Try
myScope.Connect()
Dim disk As New ManagementObject(myScope,
New ManagementPath("win32_operatingsystem='TheMachine'"),
Nothing)
'Dim disk As New ManagementObject
(myScope, New ManagementPath("win32_logicaldisk='c:'"),
Nothing)
disk.Get()
For Each oProperty In disk.Properties
MsgBox(oProperty.Name)
Next
Catch ex As Exception
ListBox2.Items.Add("Machine at " &
TheMachine & " has failed with " & ex.Message)
End Try
Dim msg As New RemoteMessage(Me)
msg.Broadcast()
Next
MsgBox("Finished")
End Sub
Function GetTheText() As String
Return TextBox1.Text
End Function
Sub AddTheItem(ByVal s As String)
'ListBox2.Items.Add(s)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Public Class RemoteMessage
Dim theform As Form1
Sub New(ByRef owner As Form1)
theform = owner
End Sub
Sub Broadcast()
Dim s As String
Dim s2 As String
s = theform.GetTheText()
s2 = s.ToUpper()
theform.AddTheItem(s2)
End Sub
End Class
 
K

Ken Tucker [MVP]

Hi,

Try something like this


Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_LogicalDisk where Name = 'C:'")

moReturn = moSearch.Get

For Each mo In moReturn

Dim VolumeName As String = mo("Volumename")

Dim SerialNumber As String = mo("Volumeserialnumber")

Dim strOut As String = String.Format("{0} - {1}", VolumeName, SerialNumber)

Debug.WriteLine(strOut)

Next

Ken

-------------------

Hi there-I'm an AMATEUR VB.NET programmer, who has hacked
together a small VB.NET application that can get
information from remote machines using the
System.Management namespace. MY APPLICATION DOES WORK,
but not quite in the way I want to. My management object
is defined like this:

Dim disk As New ManagementObject(myScope, New
ManagementPath("win32_operatingsystem='TheMachine'"),
Nothing)

However, it does not work correctly-the properties are
not retrieved for reasons I don't know. The original
code that I...stole from had a different ManagementPath,
but it does work.

Dim disk As New ManagementObject(myScope, New
ManagementPath("win32_logicaldisk='c:'"), Nothing)

I know WMI and VBScript, and of course VB.NET is
significantly more complex, but I don't quite know what
the problem is. Are my parameters wrong? Is my path
wrong? I don't even fully understand what I should be
looking for-I want to basically be able to run any query
that WMI support. If anyone can help me or at least
point me in a direction, I'd appreciate it.

Thank you









Here is the code (sorry for making it so lengthy):

Imports System
Imports System.Management
Public Class Form1
Inherits System.Windows.Forms.Form
Dim IPAddress As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
Friend WithEvents Button1 As
System.Windows.Forms.Button
Friend WithEvents ListBox1 As
System.Windows.Forms.ListBox
Friend WithEvents ListBox2 As
System.Windows.Forms.ListBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents ListBox3 As
System.Windows.Forms.ListBox
Friend WithEvents ListBox4 As
System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.ListBox3 = New System.Windows.Forms.ListBox
Me.ListBox4 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point
(176, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(40, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point
(168, 80)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "START"
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point
(176, 136)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120,
17)
Me.ListBox1.TabIndex = 2
'
'ListBox2
'
Me.ListBox2.Location = New System.Drawing.Point
(16, 168)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(552,
17)
Me.ListBox2.TabIndex = 3
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point
(112, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(176, 16)
Me.Label1.TabIndex = 4
Me.Label1.Text = "Insert the 3rd IP address
number"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(64,
136)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.TabIndex = 5
Me.Label2.Text = "The IP address is:"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(8,
104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(120, 16)
Me.Label3.TabIndex = 6
Me.Label3.Text = "The Machine Name is:"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point
(128, 48)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(40, 16)
Me.Label4.TabIndex = 7
Me.Label4.Text = "10.33."
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point
(232, 48)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(16, 16)
Me.Label5.TabIndex = 8
Me.Label5.Text = ".X"
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(56,
224)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(104, 16)
Me.Label6.TabIndex = 9
Me.Label6.Text = "The CPU Speed is:"
'
'Label7
'
Me.Label7.Location = New System.Drawing.Point(48,
264)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(112, 16)
Me.Label7.TabIndex = 10
Me.Label7.Text = "The vendor name is:"
'
'ListBox3
'
Me.ListBox3.Location = New System.Drawing.Point
(176, 224)
Me.ListBox3.Name = "ListBox3"
Me.ListBox3.Size = New System.Drawing.Size(392,
17)
Me.ListBox3.TabIndex = 11
'
'ListBox4
'
Me.ListBox4.Location = New System.Drawing.Point
(176, 264)
Me.ListBox4.Name = "ListBox4"
Me.ListBox4.Size = New System.Drawing.Size(120,
17)
Me.ListBox4.TabIndex = 12
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(592, 302)
Me.Controls.Add(Me.ListBox4)
Me.Controls.Add(Me.ListBox3)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Vital Information Collected
Electronically"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
For IPAddress = 123 To 125
MsgBox("Now testing " & IPAddress)
Dim TheMachine As String = "10.33." &
TextBox1.Text & "." & IPAddress
Dim Options As New ConnectionOptions
Dim oProperty As PropertyData
Dim oTest As PropertyDataCollection
Options.Username = "TheUsersName"
Options.Password = "TheUsersPassword"
Dim myScope As New ManagementScope("\\" &
TheMachine & "\root\cimv2", Options)
ListBox1.Items.Add("10.33." & TextBox1.Text
& "." & IPAddress)
Try
myScope.Connect()
Dim disk As New ManagementObject(myScope,
New ManagementPath("win32_operatingsystem='TheMachine'"),
Nothing)
'Dim disk As New ManagementObject
(myScope, New ManagementPath("win32_logicaldisk='c:'"),
Nothing)
disk.Get()
For Each oProperty In disk.Properties
MsgBox(oProperty.Name)
Next
Catch ex As Exception
ListBox2.Items.Add("Machine at " &
TheMachine & " has failed with " & ex.Message)
End Try
Dim msg As New RemoteMessage(Me)
msg.Broadcast()
Next
MsgBox("Finished")
End Sub
Function GetTheText() As String
Return TextBox1.Text
End Function
Sub AddTheItem(ByVal s As String)
'ListBox2.Items.Add(s)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Public Class RemoteMessage
Dim theform As Form1
Sub New(ByRef owner As Form1)
theform = owner
End Sub
Sub Broadcast()
Dim s As String
Dim s2 As String
s = theform.GetTheText()
s2 = s.ToUpper()
theform.AddTheItem(s2)
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top