19

Is it possible to have ValidateScript generate a custom error message when a test fails, like say Test-Path?

Instead of this:

Test-Folder : Cannot validate argument on parameter 'Folder'. The "Test-Path $_ -Path Type Container" validation script for the argument with value "blabla" did not return a result of True. Determine why the validation script failed, and then try the comma and again.

It would be nice to have it report this instead in the $Error variable:

The 'Folder' is not found, maybe there are network issues?

Code:

Function Test-Folder {
    Param (
        [parameter(Mandatory=$true)]
        [ValidateScript({Test-Path $_ -PathType Container})]
        [String]$Folder
    )
    Write-Host "The folder is: $Folder"
}

Workaround 1:

I could remove the Mandatory=$true and change it as below. But this doesn't give me the correct Get-Help syntax and doesn't do the Test-Path validation, because it only checks if the parameter is present.

Function Test-Folder {
    Param (
        [parameter()]
        [String]$Folder = $(throw "The $_ is not found, maybe there are network issues?")
    )
    Write-Host "The folder is: $Folder"
}

Workaround 2:

I found this workaround on a blog post, but the problem is that it generates two errors instead of one.

Function Test-Folder {
    Param (
        [parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_ -PathType Container) {$true}
            else {Throw "The $_ is not found, maybe there are network issues?"}})]
        [String]$Folder
    )
    Write-Host "The folder is: $Folder"
}

Workaround 3:

I could also try to make it more clear by adding a comment section. However, this is still not the desired result as the error needs to be readable to end users.

Function Test-Folder {
    Param (
        [parameter(Mandatory=$true)]
        [ValidateScript({
        # The folder is not found, maybe there are network issues?
        Test-Path $_ -PathType Container})]
        [String]$Folder
    )
    Write-Host "The folder is: $Folder"
}
2
  • 1
    Had the same issue and found this post. I also found what you call workaround 2. Unlike you though I only got the one error. Perhaps PowerShell version plays a part in that. M:\Scripts\Move-MaintenanceData.ps1 : Cannot validate argument on parameter 'Source'. C:\werwer does not appear to be a valid folder.
    – Matt
    Oct 26, 2015 at 19:24
  • 1
    You are right, I don't have this problem anymore now. So it has to depend on the version of PowerShell one uses. Problem solved :)
    – DarkLite1
    Nov 4, 2015 at 7:21

4 Answers 4

17

Your ValidateScript should look something like this:

[ValidateScript({
    try {
        $Folder = Get-Item $_ -ErrorAction Stop
    } catch [System.Management.Automation.ItemNotFoundException] {
        Throw [System.Management.Automation.ItemNotFoundException] "${_} Maybe there are network issues?"
    }
    if ($Folder.PSIsContainer) {
        $True
    } else {
        Throw [System.Management.Automation.ValidationMetadataException] "The path '${_}' is not a container."
    }
})]

That will give you a message like this:

Test-Folder : Cannot validate argument on parameter 'Folder'. Cannot find path '\\server\Temp\asdf' because it does not exist. Maybe there are network issues?

Or:

Test-Folder : Cannot validate argument on parameter 'Folder'. The path '\\server\Temp\asdf' is not a container.

If the older versions of PowerShell are throwing a double error, you may need to test inside the function:

Function Test-Folder {
    Param (
        [parameter(Mandatory=$true)]
        [String]$Folder
    )

    try {
        $Folder = Get-Item $_ -ErrorAction Stop
    } catch [System.Management.Automation.ItemNotFoundException] {
        Throw [System.Management.Automation.ItemNotFoundException] "The '${Folder}' is not found, maybe there are network issues?"
    }

    if (-not $Folder.PSIsContainer) {
        Throw [System.Management.Automation.ApplicationFailedException] "The path '${_}' is not a container."
    }

    Write-Host "The folder is: ${Folder}"
}

The part that I always hated in PowerShell was trying to figure out what error to catch; without catching all. Since I finally figure it out, here's how:

PS > Resolve-Path 'asdf'
Resolve-Path : Cannot find path '.\asdf' because it does not exist.
At line:1 char:1
+ Resolve-Path 'asdf'
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (asdf:String) [Resolve-Path], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand

PS > $Error[0].Exception.GetType().FullName
System.Management.Automation.ItemNotFoundException
2
  • Glad you liked it, @DarkLite1 ... I just realized I didn't answer the question completely so I updated it. Hoping this revision is good enough to get marked as the answer. ;)
    – VertigoRay
    Apr 5, 2016 at 0:27
  • 1
    Note: This doesn't work as well with Write-Error, by default it ignores it altogether and if you use -ErrorAction you get "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: before the custom error. May 2, 2017 at 11:12
5

You can just do

ValidateScript({Test-Path $_ -PathType Container}, ErrorMessage="Must exist")]

That gives you error messages like:

Cannot validate argument on parameter 'Folder'. Must exist

At least in current powershell versions (7.x at the time of writing this).

Generally, for you powershell-only devs, when you use attributes (stuff like [ValidateScript), then you can sometimes also set additional properties with the syntax as above. To see which properties, you can just google for the name of the attribute postfixed with "Attribute", eg "ValidateScriptAttribute", and then look at the "Properties" section for all writeable properties.

1
2

I think you've found the straightforward workarounds.

The parameter validation logic is extensible, but requires some C#. If you implement the abstract class System.Management.Automation.ValidateArgumentsAttribute, your implementation can throw a System.Management.Automation.ValidationMetadtaException that PowerShell will use to report the error, and you can naturally use any message you like when creating that exception.

1
  • 2
    Thx for the feedback Jason. Can you give a small example on how to implement such an abstract class? i'm not really a programmer..
    – DarkLite1
    Oct 1, 2014 at 6:36
0

I am not sure.

A suggestion: maybe you want to just trap the error, and make your own message.

trap [Error.Type] { 
  @"
  The message you want displayed.
  With maybe some additional information.
  I.e.: The full message itself: {0}' -f  $_.Error.Message;
  continue;
}
1
  • Thx for the suggestion. Yes I would like to make my own message. If I use the trap suggestion, I need to do this outside my function I suppose? This would be a problem, because I have other error messages arriving in my script..
    – DarkLite1
    Oct 1, 2014 at 6:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.