Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 

AND

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 
like image 998
Aaron Wurthmann Avatar asked Jan 28 '10 20:01

Aaron Wurthmann


People also ask

How do you pass a command line argument in PowerShell?

ps1 and execute this script we need to open PowerShell and type the file name with the parameters. If you are using the command line then to execute the PowerShell script you could use the below format. powershell.exe -File "C:\example. ps1" arg1 arg2 arg3 # OR powershell.exe -File "C:\example.

How do you handle command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do I pass multiple command line arguments in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.


1 Answers

You are reinventing the wheel. Normal PowerShell scripts have parameters starting with -, like script.ps1 -server http://devserver

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" )  ) 

Inside the script you can simply

write-output $server 

since all parameters become variables available in script scope.

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.

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.

The keyword for that is [switch] parameter type:

 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 

Links to relevant Microsoft documentation (for PowerShell 5.0; tho versions 3.0 and 4.0 are also available at the links):

  • about_Scripts
  • about_Functions
  • about_Functions_Advanced_Parameters
like image 133
naivists Avatar answered Sep 30 '22 17:09

naivists