简体   繁体   English

如何在PowerShell中强制执行命令行参数检查

[英]How do I enforce command line argument checking in PowerShell

Here's my script (test.ps1): 这是我的脚本(test.ps1):

[CmdLetBinding()]

Param
(
    [parameter(Mandatory=$true)][string]$environment,
    [switch][bool]$continue=$true
)

Write-Host $environment
Write-Host $continue

Question: 题:
If I invoke this script by giving an argument which is a substring of the parameter I specified in the script like this: PS> .\\test.ps1 -envi:blah, PowerShell doesn't seem to check the argument name. 如果我通过提供一个参数来调用此脚本,该参数是我在脚本中指定的参数的子字符串,如下所示:PS>。\\ test.ps1 -envi:blah,PowerShell似乎不会检查参数名称。 I want PowerShell to enforce parameter spelling, ie, it should only accept -environment which matches the parameter name in the script. 我希望PowerShell强制执行参数拼写,即它只应接受与脚本中的参数名称匹配的-environment。 For anything else, it should raise an exception. 除此之外,它应该引发一个例外。 Is that doable? 那可行吗? How do I do that? 我怎么做?
Thanks. 谢谢。

It's not pretty, but it will keep you from using anything except -environment as a parameter name. 它不是很漂亮,但是它将阻止您使用-environment作为参数名称。

Param
(
    [parameter(Mandatory=$true)][string]$environment,
    [parameter()]
     [ValidateScript({throw "Invalid parameter. 'environment' required."})]
     [string]$environmen,
    [switch][bool]$continue=$true
)


Write-Host $environment
Write-Host $continue
}

Edit: As Matt noted in his comment the automatic disambiguation will force you to specify enough of the parameter name to find a unique substring match. 编辑:正如Matt在评论中指出的那样,自动消除歧义将迫使您指定足够的参数名称以查找唯一的子字符串匹配项。 What I'm doing here is basically giving it a parameter that satisfies all but the last character to prevent using any substring up to the last character (because it's ambiguous), and throwing an error to prevent you from using that. 我在这里所做的基本上是为它提供一个参数,该参数可以满足除最后一个字符以外的所有条件,以防止使用任何子字符串直到最后一个字符(因为其模棱两可),并抛出错误以阻止您使用它。

And, FWIW, that could well be the ugliest parameter validation I've ever done but I don't have any better ideas right now. 而且,FWIW,这很可能是我做过的最丑陋的参数验证,但是我现在没有更好的主意。

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

相关问题 如何将powershell参数传递给非powershell命令? - How do I pass a powershell argument to a non powershell command? 如何从批处理文件启动Powershell命令行并强制其保持打开状态? - How I can I start a powershell command line from a batch file and enforce it to stay open? 如何从命令行运行管道PowerShell命令? - How do I run a piped powershell command from command line? 如果命令行参数在Powershell中“存在”,如何设置变量? - How do I set a variable if a command line parameter is “present” in powershell? 如何使用 PowerShell 中的函数从命令行执行? - How do I use Function in PowerShell to execute from Command Line? Powershell:在命令行调用powershell时,如何将变量传递给切换参数? - Powershell: How do I pass variables to switch parameters when invoking powershell at the command line? 如何使用PowerShell脚本运行命令行参数 - How to run command line argument using PowerShell script 如何在Alias的profile.ps1中向powershell添加命令行参数? - How to add a command line argument to powershell in profile.ps1 for Alias? 带有空格和大括号的Powershell命令行参数? - Powershell command line argument with spaces and curly brackets? 将参数从命令行传递到 PowerShell 脚本 - Passing an argument from command line to PowerShell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM