我有一个复杂的脚本来评估来自 Web 服务的“属性”。我在一个名为$global:attributes的列表中构建了所有属性和有关它们的信息的列表,并确定它们是否是单值/多值值并且是引用。
下面是一个片段:
#Build the object
$obj = new-object object
$obj | Add-Member -MemberType NoteProperty -Name AttributeName -Value "Applications"
$obj | Add-Member -MemberType NoteProperty -Name IsReference -Value $true
$obj | Add-Member -MemberType NoteProperty -Name IsMultiValued -Value $true
$global:attributes = $obj
现在以下命令返回“true”对吗?
(($global:attributes | where { $_.AttributeName -eq "Applications" }).IsReference -eq $true)
(($global:attributes | where { $_.AttributeName -eq "Applications" }).IsMultiValued -eq $true)
那么为什么下面给定的函数没有按预期进行评估?是因为我在嵌套的 if 语句中调用 where 子句吗?
Function doTest($key)
{
if (($global:attributes | where { $_.AttributeName -eq $key}).IsReference -eq $true)
{
write-host "$key is of type Reference"
if (($global:attributes | where { $_.AttributeName -eq $key}).IsMulitValued -eq $true)
{
write-host "$key is multivalued"
}
else {
write-host "$key is single value"
}
} else {
if (($global:attributes | where { $_.AttributeName -eq $key}).IsMultiValued -eq $true)
{
write-host "$key is Multivalue other"
} else {
write-host "$key is single value other"
}
}
}
该命令
doTest -key "Applications"
返回
Applications is of type Reference
Applications is single value
应该是多值的,对吧?