0

タイトルは、Add-Member またはスプラッティングを使用して NoteProperty に割り当てられると、すべての単一のブール値が配列になることを示しています。

PSバージョン: 5.0.1xx

私は奇妙な問題だと思うものを持っています。NoteProperty メンバーの 1 つをブール値として PSObject を作成しています。関数はリストをループし、関数を呼び出して評価を実行し、オブジェクトを作成してから配列に追加します。これは、作成された最初のオブジェクトでのみ発生するようですが、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

}

4

1 に答える 1

0

Get-Is-Client-Cert-Required関数の本体では、次のことを行います。

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

このパターン:

[type]$nonExistingVariable

PowerShell ではひどい考えです - C# とは異なり、PowerShell には裸の変数宣言の概念がなく、上記のパターンは単に$null指定された型にキャストし、成功した場合はその型の新しいインスタンスを発行します - これが関数の原因である可能性があります配列を出力します。


変数を特定の型にバインドする必要がある場合は、割り当て時にキャストします。

[type]$Variable = Get-Stuff

おまけのヒント: 関数とコマンドレットの PowerShell 慣用的な命名規則はNoun-Verb、ハイフン 1 つだけの です。関数のより適切な名前は次のようになります。

Test-ClientCertRequirement
于 2017-01-09T02:29:10.123 に答える