【问题标题】:Boolean NoteProperty becomes an ArrayBoolean NoteProperty 变成一个数组
【发布时间】:2026-01-16 02:25:02
【问题描述】:

标题说,当使用 Add-Member 或使用 splatting 将单个布尔值分配给 NoteProperty 时,所有单个布尔值都会变成一个数组。

PSVersion:5.0.1xx

我有一个我认为奇怪的问题。我正在创建一个带有 NoteProperty 成员之一的 PSObject 作为布尔值。该函数遍历一个列表,调用一个函数来执行评估,创建一个对象,然后将其添加到一个数组中。这似乎只发生在创建的第一个对象上,但我没有在创建 5 个或更多对象时对此进行测试。

我已经验证了函数实际上返回的是布尔值,并且分配给属性的变量是布尔值。

我的解决方法似乎很可靠,但很好奇为什么会发生这种情况。

下面是部分代码:

$clientCertsRequired = Get-Is-Client-Cert-Required -configFile $configFile -siteName $siteName

$httpsStatus = "Https is not enabled"
$clientCertStatus = "Client certs are not required"

if ($httpsEnabled -eq $true) {
    $httpsStatus = "Https is enabled"
}

if ($clientCertsRequired -eq $true){
    $clientCertStatus = "Client certs are required"
} 

$sc = New-Object PSObject -Property @{
    SiteName = $siteName;
    ConfigFilePath = $path;
    HttpsEnabled = $httpsStatus;
    ClientCertStatus =$clientCertStatus; 
    ClientCertRequired = $clientCertsRequired;
}

# clean up of some inexplicable problem where assignment to property 
# produces array with actual value in the last element.
if ($sc.ClientCertRequired.GetType().Name -eq "Object[]"){
    $sc.ClientCertRequired = $sc.ClientCertRequired[-1]
}

$si += $sc

Function Get-Is-Client-Cert-Required{
param(
    [xml]$configFile, 
    [string]$siteName
)
$functionName = $MyInvocation.MyCommand.Name  

$clientCertRequired = $false
try{
    # then read locations section (this will often not have any pages 
    $locationPath = "//configuration/location[@path='$siteName']"
    [system.xml.xmlelement]$location = $configFile.DocumentElement.SelectSingleNode($locationPath)

    if($location -ne $null){
        [system.xml.xmlelement]$accessNode = $location.SelectSingleNode("system.webServer/security/access")
        [system.xml.xmlelement]$authenticationNode = $location.SelectSingleNode("system.webServer/security/authentication")
        [system.xml.xmlelement]$clientCertMappingNode
        [system.xml.xmlelement]$iisClientCertMappingNode

        [int]$sslFlagMask = 0
        if($accessNode -ne $null){
            $sslFlags =  $accessNode.Attributes.GetNamedItem("sslFlags")
            # $sslFlags = $accessNode.Attributes["sslFlags"].Value
            if($sslFlagMask -ne $null){
                $sslFlagMask = Convert-Ssl-Flag-String-To-Int-Flag -sslFlag $sslFlags.Value
            }
        }

        if($authenticationNode -ne $null){
            [system.xml.xmlelement]$clientCertMappingNode = $authenticationNode.SelectSingleNode("clientCertificateMappingAuthentication[@enabled='true']")
            [system.xml.xmlelement]$iisClientCertMappingNode = $authenticationNode.SelectSingleNode("iisClientCertificateMappingAuthentication[@enabled='true']")
        }

        $clientCertAccepted = ($sslFlagMask -band $certAccepted) -eq $certAccepted
        $clientCertRequired = Check-IIS-Express-SSL-Config $sslFlagMask

        if($clientCertRequired -eq $false){
            if($clientCertAccepted -and ($clientCertMappingNode -ne $null -or $iisClientCertMappingNode -ne $null)){
                $clientCertRequired = $true
            }
        }
    }
}catch{
    $exceptionMessage = Get-Formatted-Exception-String -exceptionObject $_
    $message = "$functionName - Exception`: $exceptionMessage"
    Add-Exception -exception $message
    Log-Error -message $message 
}

$clientCertRequired

}

【问题讨论】:

  • 你能告诉我们Get-Is-Client-Cert-Required的定义吗?该函数中的某些方法调用很可能会发出一个值
  • @MatthewWetmore [bool]$var = Get-Stuff 是一种危险的“解决方法” - count > 1 的数组将总是导致$true,即使预期值是$false
  • 你知道,这可能解释了我不久前遇到的一个问题。 :) 更好的建议?我喜欢 PowerShell 的一些地方,还有一些让我已经稀疏的头发掉光的地方。
  • 为你的函数编写 Pester 测试,确保它们不会发出无用的垃圾:-)
  • @MatthewWetmore 是的,我的错。意味着转换模棱两可的表达式,如$r = [bool](foo),如果foo 返回> 1 项,则它总是$true

标签: powershell psobject


【解决方案1】:

Get-Is-Client-Cert-Required 函数的主体中,您可以:

[system.xml.xmlelement]$clientCertMappingNode
[system.xml.xmlelement]$iisClientCertMappingNode

这种模式:

[type]$nonExistingVariable

在 PowerShell 中是一个糟糕的想法 - 与 C# 不同,PowerShell 没有裸变量声明的概念,并且上述模式只是将 $null 转换为指定类型,如果成功则发出所述类型的新实例 - 这个可能是导致函数输出数组的原因。


如果您确实需要将变量绑定到特定类型,请在赋值时强制转换:

[type]$Variable = Get-Stuff

额外提示:函数和 cmdlet 的 PowerShell 惯用命名约定是 Noun-Verb,只有一个连字符。该函数的更合适的名称是:

Test-ClientCertRequirement

【讨论】:

    最近更新 更多