Boolean NoteProperty成为一个数组

时间:2017-01-08 22:14:43

标签: powershell psobject

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

PSVersion:5.0.1xx

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

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

我的解决方案看起来很稳固,但我很好奇为什么会这样。

以下是代码的一部分:

$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

}

1 个答案:

答案 0 :(得分:0)

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
相关问题