为什么在powershell函数中$Null验证失败



团队!

我有参数$Data的验证脚本。当它获得$null时,它将失败。

怎么了?

[CmdletBinding()]
Param (
[Parameter(  HelpMessage = "PsObject data." )]
[AllowNull()]
[AllowEmptyCollection()]
[AllowEmptyString()]
[ValidateScript({
if ( ( $_ -eq $null ) -or ( $_ -eq '' ) ){
$true
}
else {
(( !( $_.GetType().IsValueType ) ) -and ( $_ -isnot [string] ))
}
})]        
$Data,
...
$UserObject = Show-ColoredTable -Data $null -View 'SamAccountName', 'Name', 'DistinguishedName', 'Enabled'  -Title "AD User list" -SelectMessage "Select AD user(s) to disable VPN access: " -AddRowNumbers -AddNewLine -SelectField "SamAccountName"

大多数验证属性与[AllowNull()]不兼容,因为在调用自定义验证之前,它们首先要检查的是输入对象是否为$null

在函数体内移动验证逻辑:

[CmdletBinding()]
Param (
[Parameter(  HelpMessage = "PsObject data." )]
[AllowNull()]
[AllowEmptyCollection()]
[AllowEmptyString()]
$Data
)
# validate $Data here before the rest of the script/command

相关内容

  • 没有找到相关文章

最新更新