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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Instnace of Custom WMI Class

Status
Not open for further replies.

mrmovie

Technical User
Oct 2, 2002
3,094
GB
Good afternoon All.
wondered if anyone could point me in the right direction?

1. have a .mof file of:
#pragma namespace("\\\\.\\Root\\Cimv2")

class MyClass_MachineProperties
{
string VMClusterName;
string VMHostName;
string Region;
string Version;
};

I import this and i can view my new class and its properties through:

strComputer = "."
strNameSpace = "root\cimv2"
strClass = "MyClass_MachineProperties"

Set objClass = GetObject("winmgmts:\\" & strComputer & _
"\" & strNameSpace & ":" & strClass)

WScript.Echo strClass & " Class Properties"
WScript.Echo "------------------------------"

For Each objClassProperty In objClass.Properties_
WScript.Echo objClassProperty.Name
Next



I now want to create an instance of this class through a vbscript and populate its properties with information...which is where i am a little stuck. have looked at Create methods but my class doesnt expose a create method explicitly??? any ideas? I found this post but the code doesnt work...

set NewObject = GetObject("winmgmts:root\cimv2").Get
NewObject.Path_.Class = "MyClass_MachineProperties"
NewObject.Properties_.Add "VMClusterName", "TestClusterName1"
 
...i think i have solved it

had to update the mof to include a [Key]

#pragma namespace("\\\\.\\Root\\Cimv2")

class MyClass_MachineProperties
{
[Key] uint32 KeyProperty;
string VMClusterName;
string VMHostName;
string Region;
string Version;
};

then the creation script is somthing like

Set refWMI = GetObject("winMgmts:")
Set newInstance = refWMI.Get("MyClass_MachineProperties").SpawnInstance_()
newInstance.VMClusterName = "test"
newInstance.KeyProperty = 1
newInstance.Put_()
 
I was unaware that custom objects could be created in vbscript. Where did you store your .mof file? Are custom object creations and utilizations resourceful in VBScript?

-Geates
 
the mof file was run using mofcomp.exe "name of mof file" against the client machine.
this is creating a static class (i.e. WMIProvider required cc/c# etc)

the usefulness?

i have lived in the desktop/server provisioning space for about 15 years and am used to implementing logging / auditing of 'package' installations by populating the registry HKLM\%CompnayName%\Packages\%PacakgeName%\..and then lots of registry strings etc for Duration, Result, Date, User, SourcePath, Features etc etc

you also end up with the HKLM\%CompnayName%\ key populated with other custom company specific information...things like Region, ServerRoles etc etc

The registry seems oldskool and with SCCM and SMS clients being 32bit and by default the above keys on a 64bit arent availalbe to the 32bit client one can get into a mess with regards reporting and inventory etc....the crux i guess is that the audit information is supposed to be stored in a location which is consistant, in the example of a 32bit and 64bit server then the default HKLM isnt? (because of the syswow thing)

so, one option i think is to create a custom class for bizness / environment specific information. ultimately WMI ties in better with SCCM and would be easier to query than some funny registry keys /values?

i have waffled
 
sorry, i have literally shot myself in the foot. my bulid methodologies would not suggest storing this information in WMI is a good idea, I would use XML for all this data. However, try telling XML to SMS\SCCM, the WMI thing for me purely servers a method of making the audit process easier.
 
I see, I think. Because you machine is 64-bit, new instances of objects are 64-bit by default - this is why you had to add uint32 KeyProperties.

Currently, my organization uses SMS with SCCM just down the road. I wrote a script to query remote WMI objects to return inventory result. It works better and is more reliable than SMS. The only downside is that the remote machine needs to be on! :)

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top