[英]Passing optional arguments in Powershell script
I'm trying to write a code that would check if arguments exist and take action accordingly. 我正在尝试编写代码来检查参数是否存在并采取相应的措施。 The code would need to be flexible to run with or without arguments when the script is called.
调用脚本时,代码需要灵活地运行,无论有无参数都可以运行。
For example: 例如:
if (arg[0] -And arg[1])
{
$argOne = arg[0]
$argTwo = arg[1]
}
else
{
$argOne = "One"
$argTwo = "Two"
}
This code is obviously wrong because I'm not sure how to check if arguments exist in Powershell. 这段代码显然是错误的,因为我不确定如何检查Powershell中是否存在参数。 In python, there is the functionality of
sys.argv
that stores all arguments into an array. 在python中,有
sys.argv
的功能,可将所有参数存储到数组中。 I could then do a try-except to check if the arguments exist. 然后,我可以做一个try-except来检查参数是否存在。 Can I do something similar in Powershell?
我可以在Powershell中做类似的事情吗?
I did some research and param
was advised to use to handle arguments but I don't think param
could handle no arguments being passed to the script. 我进行了一些研究,建议使用
param
处理参数,但是我认为param
无法处理传递给脚本的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.