【发布时间】:2018-01-16 05:27:45
【问题描述】:
我有在 WMI 中更新对象的函数。我希望用户能够在参数中仅指定他想要更新的值。我该怎么做?
function UpdateObject ([bool] $b1, [bool] $b2, [int] $n1, [string] $s1)
{
$myObject = GetObjectFromWmi #(...)
#(...)
#This is bad. As it overrides all the properties.
$myObject.b1 = $b1
$myObject.b2 = $b2
$myObject.n1 = $n1
$myObject.s1 = $s1
#This is what I was thinking but don't kwow how to do
if(IsSet($b1)) { $myObject.b1 = $b1 }
if(IsSet($b2)) { $myObject.b2 = $b2 }
if(IsSet($n1)) { $myObject.n1 = $n1 }
if(IsSet($s1)) { $myObject.s1 = $s1 }
#(...) Store myObject in WMI.
}
我尝试将$null 作为参数传递,但它会自动转换为$false 用于bool,0 用于int 和empty string 用于string
你有什么建议?
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0 powershell-4.0