使用脚本和程序创建WMI类和实例来扩展hardware inventory

来源:互联网 发布:苹果解压软件下载 编辑:程序博客网 时间:2024/06/05 10:14

 

MOF和MIF一样,也是文本文件,用脚本和程序创建很容易。麻烦在于要mofcomp编译到WMI中去,这个动作通过package部署当然也能做,但是不如直接将MOF类和实例信息通过脚本和程序写入WMI.

 

SMS 2003 SDK 中包括一个 VB程序 "Asset wizard“,提示用户输入资产相关的各种信息,然后写入WMI来扩展hinv。以下是其中的主要代码(存到vbs中可以直接运行),演示了写数据到 WMI 中的步骤,但不包括收集数据(SMS_def.mof报告类).  这个例子中,数据就在脚本中,当然也可以使用数据源,例如数据库来提取需要的数据。脚本的工作流程一目了然,就不解释了。WMI的概念,工具和编程不是本文的范围,有机会再讨论。

 

 

Set loc = CreateObject("WbemScripting.SWbemLocator")

Set WbemServices = loc.ConnectServer(, "root/CIMv2")

 

On Error Resume Next

 

Set WbemObject = WbemServices.Get("SMS_AssetWizard_1")

 

'If this call failed, we need to make the SMS_AssetWizard_1 data class

If Err Then

'Retrieve blank class

Set WbemObject = WbemServices.Get

 

'Set class name

WbemObject.Path_.Class = "SMS_AssetWizard_1"

 

'Add Properties (8 = CIM_STRING, 11 = CIM_BOOLEAN)

 

WbemObject.Properties_.Add "Type", 19

WbemObject.Properties_.Add "ContactFullName", 8

WbemObject.Properties_.Add "ContactEmail", 8

WbemObject.Properties_.Add "ContactPhone", 8

WbemObject.Properties_.Add "ContactLocation", 8

WbemObject.Properties_.Add "SysLocationSite", 8

WbemObject.Properties_.Add "SysLocationBuilding", 8

WbemObject.Properties_.Add "SysLocationRoom", 8

WbemObject.Properties_.Add "SysUnitManufacturer", 8

WbemObject.Properties_.Add "SysUnitModel", 8

WbemObject.Properties_.Add "SysUnitAssetNumber", 8

WbemObject.Properties_.Add "SysUnitIsLaptop", 11

 

'Add key qualifier to Type property

 

WbemObject.Properties_("Type").Qualifiers_.Add "key", True

WbemObject.Put_

End if

 

On Error Goto 0

 

Set WbemServices = loc.ConnectServer(, "root/CIMv2")

Set WbemObject = WbemServices.Get("SMS_AssetWizard_1").SpawnInstance_

 

' Store property values (the data!)

 

WbemObject.Type = 0

WbemObject.ContactFullName = "John Smith"

WbemObject.ContactEmail = "JSmith"

WbemObject.ContactPhone = "(425) 707-9791"

WbemObject.ContactLocation = "Redmond"

WbemObject.SysLocationSite = "Campus"

WbemObject.SysLocationBuilding = "24"
WbemObject.SysLocationRoom = "1168"
WbemObject.SysUnitManufacturer = "Dell"

WbemObject.SysUnitModel = "GX1"

WbemObject.SysUnitAssetNumber = "357701"

WbemObject.SysUnitIsLaptop = False

'WMI will overwrite the existing instance

WbemObject.Put_

 

 

下面是应该添加到 SMS_def.mof 中的报告类,用来hinv以上的数据。

 

 

 

#pragma namespace("////.//ROOT//CIMV2//sms")

[SMS_ReporT(TRUE),

SMS_Group_Name("Asset Wizard Results"),

SMS_Class_ID("MICROSOFT|ASSETWIZARD|1.0")]

 

class SMS_AssetWizard_1 : SMS_Class_Template

{

[SMS_Report(TRUE),key] uint32 Type;

[SMS_Report(TRUE)] string ContactFullName;

[SMS_Report(TRUE)] string ContactEmail;

[SMS_Report(TRUE)] string ContactPhone;

[SMS_Report(TRUE)] string ContactLocation;

[SMS_Report(TRUE)] string SysLocationSite;

[SMS_Report(TRUE)] string SysLocationBuilding;

[SMS_Report(TRUE)] string SysLocationRoom;

[SMS_Report(TRUE)] string SysUnitManufacturer;

[SMS_Report(TRUE)] string SysUnitModel;

[SMS_Report(TRUE)] string SysUnitAssetNumber;

[SMS_Report(TRUE)] boolean SysUnitIsLaptop;

};

 

原创粉丝点击