如何在 PowerShell 中强制设置参数?

发布于 2024-12-01 02:54:56 字数 30 浏览 0 评论 0原文

如何在 PowerShell 中强制设置参数?

How do I make parameters mandatory in PowerShell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜夜流光相皎洁 2024-12-08 02:54:56

您可以在每个参数上方的属性中指定它,如下所示:

function Do-Something{
    [CmdletBinding()]
    param(
        [Parameter(Position=0,mandatory=$true)]
        [string] $aMandatoryParam,
        [Parameter(Position=1,mandatory=$true)]
        [string] $anotherMandatoryParam)

    process{
       ...
    }
}

You specify it in an attribute above each parameter like this:

function Do-Something{
    [CmdletBinding()]
    param(
        [Parameter(Position=0,mandatory=$true)]
        [string] $aMandatoryParam,
        [Parameter(Position=1,mandatory=$true)]
        [string] $anotherMandatoryParam)

    process{
       ...
    }
}
冷︶言冷语的世界 2024-12-08 02:54:56

要使参数成为强制参数,请在参数描述中添加“Mandatory=$true”。要使参数可选,只需保留“强制”语句即可。

此代码适用于脚本和函数参数:

[CmdletBinding()]
param(
  [Parameter(Mandatory=$true)]
  [String]$aMandatoryParameter,

  [String]$nonMandatoryParameter,

  [Parameter(Mandatory=$true)]
  [String]$anotherMandatoryParameter
)

确保“param”语句是脚本或函数中的第一个语句(注释和空行除外)。

您可以使用“Get-Help”cmdlet 来验证参数是否已正确定义:

PS C:\> get-help Script.ps1 -full
[...]
PARAMETERS
    -aMandatoryParameter <String>

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -NonMandatoryParameter <String>

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -anotherMandatoryParameter <String>

        Required?                    true
        Position?                    3
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

To make a parameter mandatory add a "Mandatory=$true" to the parameter description. To make a parameter optional just leave the "Mandatory" statement out.

This code works for both script and function parameters:

[CmdletBinding()]
param(
  [Parameter(Mandatory=$true)]
  [String]$aMandatoryParameter,

  [String]$nonMandatoryParameter,

  [Parameter(Mandatory=$true)]
  [String]$anotherMandatoryParameter
)

Make sure the "param" statement is the first one (except for comments and blank lines) in either the script or the function.

You can use the "Get-Help" cmdlet to verify the parameters have been defined correctly:

PS C:\> get-help Script.ps1 -full
[...]
PARAMETERS
    -aMandatoryParameter <String>

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -NonMandatoryParameter <String>

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -anotherMandatoryParameter <String>

        Required?                    true
        Position?                    3
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?
泡沫很甜 2024-12-08 02:54:56

只是想发布另一个解决方案,因为我发现 param(...) 块有点难看。
看起来像这样的代码:

function do-something {
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [string] $First,

        [Parameter(Position=1, Mandatory=$true)]
        [string] $Second
    )
    ...
}

也可以像这样写得更简洁:

function do-something (
        [Parameter(Mandatory)] [string] $First,
        [Parameter(Mandatory)] [string] $Second
    ) {
    ...
}

看起来好多了! =$true 可以省略,因为 mandatory 是一个开关参数。

just wanted to post another solution, since i find param(...) blocks kind of ugly.
looks like this code:

function do-something {
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [string] $First,

        [Parameter(Position=1, Mandatory=$true)]
        [string] $Second
    )
    ...
}

can also be written more concisely like this:

function do-something (
        [Parameter(Mandatory)] [string] $First,
        [Parameter(Mandatory)] [string] $Second
    ) {
    ...
}

which looks much nicer! the =$true can be omitted because mandatory is a switch parameter.

迟月 2024-12-08 02:54:56

您不必指定 Mandatory=trueMandatory 就足够了。

简单示例:

function New-File
{
    param(
        [Parameter(Mandatory)][string]$FileName
    )

    New-Item -ItemType File ".\$FileName"
}

You don't have to specify Mandatory=true, Mandatory is enough.

Simple Example:

function New-File
{
    param(
        [Parameter(Mandatory)][string]$FileName
    )

    New-Item -ItemType File ".\$FileName"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文