Create WMI class and add static property or default value

喜欢而已 提交于 2021-02-05 08:29:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!