1

team!

I have validation script for parameter $Data. It fail when it get $null.

whats wrong?

[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"
5
  • Your validation logic doesn't make too much sense - you want to [AllowEmptyString] but fail on input validation whenever any [string] is passed? Commented Nov 3, 2021 at 16:33
  • You are right. But the result is the same. Commented Nov 3, 2021 at 16:41
  • Also: If you parameter ($Data) is untyped (implies [object]), you don't need [AllowNull()], [AllowEmptyCollection()], and [AllowEmptyString()] - it'll work the same without them. Commented Nov 3, 2021 at 16:43
  • Corrected it. The same validation error. $data can be [string] or [psobject] or something else. It else, doesnt work without [AllowNull()], [AllowEmptyCollection()], [AllowEmptyString()]. Commented Nov 3, 2021 at 16:47
  • Yes - my comment was just an aside. The real problem is explained in Mathias' answer. Commented Nov 3, 2021 at 16:51

1 Answer 1

1

Most validation attributes are incompatible with [AllowNull()] because the first thing they all check - before your custom validation is invoked - is whether the input object is $null or not.

Move the validation logic inside the function body:

[CmdletBinding()]
Param (
    [Parameter(  HelpMessage = "PsObject data." )]
    [AllowNull()]
    [AllowEmptyCollection()]
    [AllowEmptyString()]
    $Data
)

# validate $Data here before the rest of the script/command
Sign up to request clarification or add additional context in comments.

4 Comments

It`s a good idea. My aim, to validate this, inside validatescript block.
@Alex, it's not just a good idea, it's your only option as of PowerShell 7.2, because, as the answer implies, the use of [ValidateScript()] alone - irrespective of the content of the script - unexpectedly prevents passing $null. This is arguably a bug, which was reported in May 2020 in GitHub issue #12557. A succinct repro: & { param([ValidateScript({ $true })] $Foo) } -Foo $null
@Mathias R. Jessen, I think, and for Powershell 7.1 and 5.1 also.
Indeed, this behavior stretches back to at least 5.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.