Home > Mobile >  Validate that no spaces have been used in parameter powershell
Validate that no spaces have been used in parameter powershell

Time:03-10

So i am pulling my hair out of my head trying to find a solution for this. i am working on some different functions and are implementing some validation steps.

What i am looking for is a solution so that i can validate that no spaces have been used in 2 of my parameters, as the underlying property does not accept it.

I know that i can just check for and remove spaces, but would like for it to drop an error and inform the user that spaces are not allowed and to change the value. The values i am trying to do this to is:

$GroupMailSuffix $ValidatedDomainName

function Add-DistributionGroup {  
 param (

        [Parameter(Mandatory=$true)]
        [ValidateLength(5,256)]
        $DistributionGroupName,

        [Parameter(Mandatory=$true)]
        $GroupMailSuffix,

        [Parameter(Mandatory=$true)]
        $ValidatedDomainName

    )
    New-DistributionGroup -Name $DistributionGroupName -PrimarySmtpAddress "$GroupMailSuffix@$($ValidatedDomainName)"
}

CodePudding user response:

Use a [ValidateScript()] parameter attribute to validate an argument as part of the parameter binding. If the supplied script block returns $false, the invocation fails with a statement-terminating error.

# IMPORTANT: ErrorMessage only supported in PowerShell (Core) 7 
[ValidateScript({ $_ -notmatch ' ' }, ErrorMessage = 'Value must not contain spaces.')]

The caveat is that in Windows PowerShell you cannot supply a user-friendly error message - the user will see the source code of the script block ({ ... }) used for validation.

However, there is a workaround (works in both PowerShell editions): If validation fails, use throw with the desired error message inside the script block:

[ValidateScript({ if ($_ -notmatch ' ') { return $true }; throw 'Value must not contain spaces.' })]

To put it all together:

# NOTE: PowerShell (Core) 7 , due to use of the ErrorMessage property
#       See Windows PowerShell alternative above.
function Add-DistributionGroup {  
  param (
         [Parameter(Mandatory)]
         [ValidateLength(5,256)]
         [string] $DistributionGroupName,
 
         [Parameter(Mandatory)]
         [ValidateScript({ $_ -notmatch ' ' }, ErrorMessage = 'Value must not contain spaces.')]
         [string] $GroupMailSuffix,
         
         [Parameter(Mandatory)]
         [ValidateScript({ $_ -notmatch ' ' }, ErrorMessage = 'Value must not contain spaces.')]
         [string] $ValidatedDomainName
 
     )
  New-DistributionGroup -Name $DistributionGroupName -PrimarySmtpAddress "$GroupMailSuffix@$($ValidatedDomainName)"
 }

Note that the parameters were typed [string] for conceptual clarity and, in the case of $DistributionGroupName, also to allow the [ValidateLength()] property to be used.

In case the user passes a value with spaces, they'll see an error such as the following:

Add-DistributionGroup: Cannot validate argument on parameter 'ValidatedDomainName'. 
Value must not contain spaces

Note that, because the error is a statement-terminating error,[1] execution continues with the next statement by default.

To abort a script, either (possibly temporarily) set $ErrorActionPreference = 'Stop' first, or use a try / catch statement or trap statement from which to exit.


[1] This applies even to the workaround that uses throw, even though throw normally create a script-terminating (fatal) error. However, in this case it is PowerShell itself (the parameter binder) that catches the underlying .NET exception, which effectively converts it to a statement-terminating error.
For a comprehensive overview of PowerShell's bewilderingly complex error handling, see GitHub docs issue #1583.

  • Related