WMI Manipulations and Manifestations

Patch My Pc | install & update thousands of apps

As a follow-on to my System Center Universe 2014 session, I thought I’d put together the various ways to create WMI “objects” so that ConfigMgr can later pick them up using Hardware Inventory.

The below examples do not include any error checking and simply contain the core code snippets needed to perform the actions; variations are also possible, these are simply one way to do get the job done. The following example snippets are provided:

1. Create a namespace named ConfigMgrFTW in the root namespace.

2. Create a class named Bazinga within the ConfigMgrFTW namespace.

3. Create a static instance of the class Bazinga.

4. Delete the Bazinga class (this also deletes all instances of the class).

5. Delete the ConfigMgrFTW namespace (this also deletes all classed within the namespace as well as all instances of those classes).

[ms_panel title=”Notes” title_color=”#f5f5f5″ border_color=”#00274c” title_background_color=”#00274c” border_radius=”2″ class=”” id=””]

  • For the VBScript and PowerShell examples, local admin permissions are required.
  • configuration.mof is only compiled on client systems when it is changed and thus is not a good candidate for creating class instances unless they are dynamic and based on a provider like the registry provider.[/ms_panel]
    configuration.mof
    1
    1
    2
    3
    4
    5
    #pragma namespace("\\\\.\\root")
    instance of __Namespace
    {
    Name = "ConfigMgrFTW";
    };
    2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #pragma namespace("\\\\.\\root\\ConfigMgrFTW")
    class Bazinga
    {
    [Key]
       string Eat;
       uint32 Sleep;
       string Rave;
       uint32 Repeat;
    }
    3
    1
    2
    3
    4
    5
    6
    7
    8
    #pragma namespace("\\\\.\\root\\ConfigMgrFTW")
    Instance of ConfigMgrFTW
    {
       Eat = "Pizza";
       Sleep = 1;
       Rave = "Always"
       Repeat = 365;
    }
    4
    1
    2
    #pragma namespace("\\\\.\\root\\ConfigMgrFTW")
    #pragma deleteclass("Bazinga", NOFAIL)
    VBScript
    1
    1
    2
    3
    4
    5
    6
    Set locator = CreateObject("WbemScripting.SWbemLocator")
    Set rootNamespace = locator.ConnectServer(".", "root")
    Set emptyClass = rootNamespace.Get("__namespace")
    Set newNamespace = emptyClass.SpawnInstance_
    newNamespace.name = "ConfigMgrFTW"
    newNamespace.Put_()
    2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Set locator = CreateObject("WbemScripting.SWbemLocator")
    Set ns = locator.ConnectServer(".", "root\ConfigMgrFTW")
    Set newClass = ns.Get()
    newClass.Path_.Class = "Bazinga"
    newClass.Properties_.Add "Eat", wbemCimtypeString
    newClass.Properties_.Add "Sleep", wbemCimtypeReal32
    newClass.Properties_.Add "Rave", wbemCimtypeString
    newClass.Properties_.Add "Repeat", wbemCimtypeReal32
    newClass.Properties_("Eat").Qualifiers_.add "key", True
    newClass.Put_()
    3
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Set locator = CreateObject("WbemScripting.SWbemLocator")
    Set ns = locator.ConnectServer(".", "root\ConfigMgrFTW")
    Set bazingaClass = ns.Get("Bazinga")
    Set newInstance = bazingaClass.SpawnInstance_
    newInstance.Eat = "Pizza"
    newInstance.Sleep = 1
    newInstance.Rave = "Always"
    newInstance.Repeat = 365
    newInstance.Put_()
    4
    1
    2
    3
    Set locator = CreateObject("WbemScripting.SWbemLocator")
    Set ns = locator.ConnectServer(".", "root\ConfigMgrFTW")
    ns.Delete("Bazinga")
    5
    1
    2
    3
    4
    Set locator = CreateObject("WbemScripting.SWbemLocator")
    Set rootNS = locator.ConnectServer(".", "root")
    Set ns = rootNS.Get("__Namespace.Name='ConfigMgrFTW'")
    ns.Delete_
    PowerShell
    1
    1
    2
    3
    4
    $ns = [wmiclass]'root:__namespace'
    $ftwns = $ns.CreateInstance()
    $ftwns.Name = 'ConfigMgrFTW'
    $ftwns.Put()
    2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $newClass = New-Object System.Management.ManagementClass("root\ConfigMgrFTW",
       [string]::Empty, $null)
    $newClass["__CLASS"] = "Bazinga"
    $newClass.Properties.Add("Eat"[System.Management.CimType]::String,
       $false)
    $newClass.Properties["Eat"].Qualifiers.Add("Key", $true)
    $newClass.Properties.Add("Sleep"[System.Management.CimType]::Uint32,
       $false)
    $newClass.Properties.Add("Rave"[System.Management.CimType]::String,
       $false)
    $newClass.Properties.Add("Repeat"[System.Management.CimType]::Uint32,
       $false)
    $newClass.Put()
    3
    1
    2
    3
    4
    5
    6
    7
    $data = [wmiclass]'root\ConfigMgrFTW:Bazinga'
    $obj = $data.CreateInstance()
    $obj.Eat = "Pizza"
    $obj.Sleep = 1
    $obj.Rave = "Always"
    $obj.Repeat = 365
    $obj.Put()
    4
    1
    2
    [wmiclass]'root\ConfigMgrFTW:Bazinga' |
       Remove-WMIObject
    5
    1
    2
    gwmi -namespace "root" -class "__namespace" |
       where {$_.Name -eq 'ConfigMgrFTW'} | Remove-WMIObject

Leave a Reply

Your email address will not be published. Required fields are marked *

  +  44  =  48

Proudly powered by WordPress | Theme: Wanderz Blog by Crimson Themes.