问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/52621784/create-wmi-class-and-add-static-property-or-default-value