简体   繁体   English

验证参数 powershell 中没有使用空格

[英]Validate that no spaces have been used in parameter powershell

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 $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)"
}

Use a [ValidateScript()] parameter attribute to validate an argument as part of the parameter binding.使用[ValidateScript()]参数属性来验证参数作为参数绑定的一部分。 If the supplied script block returns $false , the invocation fails with a statement-terminating error.如果提供的脚本块返回$false ,则调用失败并出现语句终止错误。

# 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.需要注意的是,在Windows PowerShell中,您无法提供用户友好的错误消息- 用户将看到用于验证的脚本块( {... } ) 的源代码。

However, there is a workaround (works in both PowerShell editions): If validation fails, use throw with the desired error message inside the script block:但是,有一个解决方法(适用于 PowerShell 两个版本):如果验证失败,请在脚本块内使用throw和所需的错误消息:

[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.请注意,为概念清晰起见,参数类型为[string] ,并且在$DistributionGroupName的情况下,还允许使用[ValidateLength()]属性。

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.请注意,因为错误是语句终止错误,所以[1]默认情况下继续执行下一条语句。

To abort a script, either (possibly temporarily) set $ErrorActionPreference = 'Stop' first, or use a try / catch statement or trap statement from which to exit.中止脚本,要么(可能是暂时的)先设置$ErrorActionPreference = 'Stop' ,要么使用try / catch语句或trap语句退出。


[1] This applies even to the workaround that uses throw , even though throw normally create a script -terminating (fatal) error. [1] 这甚至适用于使用throw的解决方法,即使throw通常会产生脚本终止(致命)错误。 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.但是,在这种情况下,捕获底层 .NET 异常的是 PowerShell 本身(参数绑定器),这有效地将其转换为语句终止错误。
For a comprehensive overview of PowerShell's bewilderingly complex error handling, see GitHub docs issue #1583 .有关 PowerShell 令人眼花缭乱的复杂错误处理的全面概述,请参阅GitHub 文档问题 #1583

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM