none
Powershell: Creating a WMI class and instances problem RRS feed

  • Question

  • Hi all,

    I am struggling with creating instances for properties in Powershell. Here is my current code:

    $newClass = New-Object System.Management.ManagementClass ("root\cimv2", [String]::Empty, $null); 
    $newClass["__CLASS"] = "Win32_Test"; 
    $newClass.Qualifiers.Add("Static", $true)
    $newClass.Properties.Add("Property1",[System.Management.CimType]:: UINT16, $false)
    $newClass.Properties["Property1"].Qualifiers.Add("Key", $true)
    $newClass.Put()
    Set-WmiInstance -Class Win32_Test -Argument @{Property1=1} 
    

    This little code piece creates the WMI class Win32_Test with a property Property1 and also an instance where Property is set to 1. Works fine.

    But when I am adding a second property like this:

    $newClass = New-Object System.Management.ManagementClass ("root\cimv2", [String]::Empty, $null); 
    $newClass["__CLASS"] = "Win32_Test"; 
    $newClass.Qualifiers.Add("Static", $true)
    $newClass.Properties.Add("Property1",[System.Management.CimType]:: UINT16, $false)
    $newClass.Properties["Property1"].Qualifiers.Add("Key", $true)
    $newClass.Properties.Add("Property2",[System.Management.CimType]:: UINT16, $false)
    $newClass.Properties["Property2"].Qualifiers.Add("Key", $true)
    $newClass.Put()
    Set-WmiInstance -Class Win32_Test -Argument @{Property1=1} 

    Then both properties are created, but the instance is not created. Instead I get the following error:

    Set-WmiInstance : Illegal null value 
    At line:9 char:1
    + Set-WmiInstance -Class Win32_Test -Argument @{Property1=1}
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Set-WmiInstance], ManagementException
        + FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance

    Can anyone help me out here? Thanks in advance :-)

    Thursday, April 4, 2019 7:18 PM

Answers

  • Just figured it out!

    One key property needs to be defined as string (I am using a property that i dont use) then it works:

    $newClass = New-Object System.Management.ManagementClass ("root\cimv2", [String]::Empty, $null); 
    $newClass["__CLASS"] = "Win32_Test"; 
    $newClass.Qualifiers.Add("Static", $true)
    $newClass.Properties.Add("KeyProperty",[System.Management.CimType]::String, $false)
    $newClass.Properties["KeyProperty"].Qualifiers.Add("Key", $true)
    $newClass.Properties.Add("Property1",[System.Management.CimType]:: UINT16, $false)
    $newClass.Properties.Add("Property2",[System.Management.CimType]:: UINT16, $false)
    $newClass.Put()
    Set-WmiInstance -Class Win32_Test -Argument @{Property1=1}
    Set-WmiInstance -Class Win32_Test -Argument @{Property2=2}
    Will mark this one as answered so others might be able to learn from it :-)


    • Marked as answer by Schinken77 Thursday, April 4, 2019 8:02 PM
    Thursday, April 4, 2019 8:02 PM

All replies

  • I just figured changing UINT16 to String fixes the issue, but unfortunately the tool that I use (and cannot change) requires the property to be UINT16.
    Thursday, April 4, 2019 7:48 PM
  • Just figured it out!

    One key property needs to be defined as string (I am using a property that i dont use) then it works:

    $newClass = New-Object System.Management.ManagementClass ("root\cimv2", [String]::Empty, $null); 
    $newClass["__CLASS"] = "Win32_Test"; 
    $newClass.Qualifiers.Add("Static", $true)
    $newClass.Properties.Add("KeyProperty",[System.Management.CimType]::String, $false)
    $newClass.Properties["KeyProperty"].Qualifiers.Add("Key", $true)
    $newClass.Properties.Add("Property1",[System.Management.CimType]:: UINT16, $false)
    $newClass.Properties.Add("Property2",[System.Management.CimType]:: UINT16, $false)
    $newClass.Put()
    Set-WmiInstance -Class Win32_Test -Argument @{Property1=1}
    Set-WmiInstance -Class Win32_Test -Argument @{Property2=2}
    Will mark this one as answered so others might be able to learn from it :-)


    • Marked as answer by Schinken77 Thursday, April 4, 2019 8:02 PM
    Thursday, April 4, 2019 8:02 PM