Powershell 布尔NoteProperty变为数组
标题中说,当使用Add Member或splatting将所有单个布尔值分配给NoteProperty时,它们都将成为一个数组 PSVersion:5.0.1xx 我有一个奇怪的问题。我正在创建一个PSObject,其中一个NoteProperty成员是布尔值。该函数在列表中循环,调用函数执行求值,创建对象,然后将其添加到数组中。这似乎只发生在创建的第一个对象上,但我没有用创建的5个或更多对象来测试这一点 我已经验证了函数实际上返回bool,并且分配给属性的变量是bool 我的解决方法似乎很可靠,但我很好奇为什么会发生这种情况 以下是部分代码:Powershell 布尔NoteProperty变为数组,powershell,psobject,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
}在
Get Is Client Cert Required
函数的主体中,您可以执行以下操作:
[system.xml.xmlelement]$clientCertMappingNode
[system.xml.xmlelement]$iisClientCertMappingNode
这种模式:
[type]$nonExistingVariable
在PowerShell中是一个糟糕的想法-与C#不同,PowerShell没有裸变量声明的概念,上面的模式只是将$null
强制转换为指定的类型,如果成功,则会发出所述类型的新实例-这可能是导致函数输出数组的原因
如果确实需要将变量绑定到特定类型,请在赋值时强制转换:
[type]$Variable = Get-Stuff
额外提示:PowerShell函数和cmdlet的惯用命名约定是
名词动词
,只有一个连字符。更合适的函数名称是:
Test-ClientCertRequirement
您能告诉我们
Get Is Client Cert Required
的定义吗?该函数中的某个方法调用很可能发出value@MatthewWetmore[bool]$var=Get Stuff
是一个危险的“变通方法”-计数大于1的数组将始终导致$true
,即使预期值是$false
,这可能解释了我之前遇到的问题。:)更好的建议?我喜欢PowerShell中的一些东西,这些东西会让我已经稀疏的头发脱落。为你的函数编写纠缠测试,确保它们不会释放无用的垃圾:-)@MatthewWetmore是的,我的坏消息。这意味着使用模棱两可的表达式,如$r=[bool](foo)
,如果foo
返回>1项,则始终为$true