如何在 PowerShell 中处理命令行参数 [英] How to handle command-line arguments in PowerShell
问题描述
处理命令行参数的最佳"方式是什么?
What is the "best" way to handle command-line arguments?
关于什么是最佳"方式似乎有几个答案,因此我被困在如何处理这样简单的事情上:
It seems like there are several answers on what the "best" way is and as a result I am stuck on how to handle something as simple as:
script.ps1 /n name /d domain
和
script.ps1 /d domain /n name.
有没有插件可以更好地处理这个问题?我知道我在这里重新发明轮子.
Is there a plugin that can handle this better? I know I am reinventing the wheel here.
显然,我已经拥有的东西并不漂亮,也肯定不是最好的",但它确实有效……而且很丑.
Obviously what I have already isn't pretty and surely isn't the "best", but it works.. and it is UGLY.
for ( $i = 0; $i -lt $args.count; $i++ ) {
if ($args[ $i ] -eq "/n"){ $strName=$args[ $i+1 ]}
if ($args[ $i ] -eq "-n"){ $strName=$args[ $i+1 ]}
if ($args[ $i ] -eq "/d"){ $strDomain=$args[ $i+1 ]}
if ($args[ $i ] -eq "-d"){ $strDomain=$args[ $i+1 ]}
}
Write-Host $strName
Write-Host $strDomain
推荐答案
您正在重新发明轮子.普通 PowerShell 脚本的参数以 -
开头,例如 script.ps1 -server http://devserver
You are reinventing the wheel. Normal PowerShell scripts have parameters starting with -
, like script.ps1 -server http://devserver
然后在文件开头的 param
部分处理它们.
Then you handle them in param
section in the beginning of the file.
您还可以为参数分配默认值,如果不可用则从控制台读取它们或停止脚本执行:
You can also assign default values to your params, read them from console if not available or stop script execution:
param (
[string]$server = "http://defaultserver",
[Parameter(Mandatory=$true)][string]$username,
[string]$password = $( Read-Host "Input password, please" )
)
在脚本中你可以简单地
write-output $server
因为所有参数都成为脚本范围内可用的变量.
since all parameters become variables available in script scope.
在此示例中,如果在没有它的情况下调用脚本,$server
将获得默认值,如果省略 -username
参数并要求终端输入,脚本将停止如果省略 -password
.
In this example, the $server
gets a default value if the script is called without it, script stops if you omit the -username
parameter and asks for terminal input if -password
is omitted.
更新:您可能还想将标志"(布尔值真/假参数)传递给 PowerShell 脚本.例如,您的脚本可能会接受强制",即在不使用强制时脚本以更谨慎的模式运行.
Update: You might also want to pass a "flag" (a boolean true/false parameter) to a PowerShell script. For instance, your script may accept a "force" where the script runs in a more careful mode when force is not used.
关键字是[switch]
参数类型:
param (
[string]$server = "http://defaultserver",
[string]$password = $( Read-Host "Input password, please" ),
[switch]$force = $false
)
在脚本内部,您可以像这样使用它:
Inside the script then you would work with it like this:
if ($force) {
//deletes a file or does something "bad"
}
现在,在调用脚本时,您可以像这样设置开关/标志参数:
Now, when calling the script you'd set the switch/flag parameter like this:
.yourscript.ps1 -server "http://otherserver" -force
如果您明确想声明未设置标志,则有一个特殊的语法
If you explicitly want to state that the flag is not set, there is a special syntax for that
.yourscript.ps1 -server "http://otherserver" -force:$false
相关 Microsoft 文档的链接(适用于 PowerShell 5.0;3.0 和 4.0 版也可在链接中找到):
Links to relevant Microsoft documentation (for PowerShell 5.0; tho versions 3.0 and 4.0 are also available at the links):
这篇关于如何在 PowerShell 中处理命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!