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. In python, there is the functionality of sys.argv
that stores all arguments into an array. I could then do a try-except to check if the arguments exist. Can I do something similar in 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.
Turns out I was wrong about param
! Came across this which was very helpful.
param($one, $two)
if ($one -And $two)
{
$argOne = $one
$argTwo = $two
}
else
{
$argOne = "One"
$argTwo = "Two"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.