创建WMI类并添加静态属性或默认值

时间:2018-10-03 07:21:53

标签: windows powershell wmi

我正在尝试创建WMI类并添加静态属性,或设置属性的默认值

$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

已创建该类,但$ ver为空,有什么主意吗?

1 个答案:

答案 0 :(得分:2)

将代码更改为:

$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