團隊!
我有引數 $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"
uj5u.com熱心網友回復:
大多數驗證屬性是不兼容的,[AllowNull()]因為它們首先要檢查 - 在呼叫您的自定義驗證之前 - 是輸入物件是否$null存在。
在函式體內移動驗證邏輯:
[CmdletBinding()]
Param (
[Parameter( HelpMessage = "PsObject data." )]
[AllowNull()]
[AllowEmptyCollection()]
[AllowEmptyString()]
$Data
)
# validate $Data here before the rest of the script/command
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347462.html
上一篇:多階段表單驗證角度
