Search code examples
windowspowershellwmi

Create WMI class and add static property or default value


I'm trying to create a WMI class and add a static property, or set the default value of the property

$WMI_Class = New-Object System.Management.ManagementClass("root\default", $null, $null)
$WMI_Class.Qualifiers.Add("Static", $true)
$WMI_Class.Properties.Add("ver", [System.Management.CimType]::String, "myDefaultValue")
$WMI_Class.name = "MyCoreClass"
$WMI_Class.Put()

$obj = ([WmiClass] 'root\default:MyCoreClass')
$ver = $obj.Properties['ver'].Value
$ver

The class is created but the $ver is empty, any ideas?


Solution

  • Change your code to :

    $WMI_Class = New-Object System.Management.ManagementClass("root\default", [String]::Empty, $null)
    $WMI_Class.name = "MyCoreClass"
    $WMI_Class.Properties.Add("ver", "Hi, This is sample static value")
    $WMI_Class.Put()
    
    $obj = ([WmiClass] 'root\default:MyCoreClass')
    $ver = $obj.Properties['ver'].Value
    $ver