![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Script Parameters Is there a method to create script command line parameters similar to cmdlet parameters that are not positional? I would like to create a script containing options/parameters that require arguments. Example: scriptname -Filter *.ps1 -path ./ parses the same as scriptname -path ./ -Filter *.ps1 |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Script Parameters To specify parameters in a script use the param statement at the beginning of the script. Script parameters could be pass in any order as long as the parameter name --or its first chars-- is placed before its value. They are consider positional when no parameter name is defined, any extra values are picked up by $args. @' # beginning of scriptX param($filter, $path) "`$filter is $filter" "`$path is $path" if ($args) {"`$args is $args"} # end of scriptX '@ > scriptX.ps1 # either way works properly: ../scriptX -Filter *.ps1 -path ./ ../scriptX -path ./ -Filter *.ps1 # positional assignment ../scriptX *.ps1 ./ # positional incorrect assignment ../scriptX ./ *.ps1 # extra values picked up by $args ../scriptX -path ./ -Filter *.ps1 1 abc 009 yyz -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Script Parameters Thank You Kiron, My mistake was having filter and path as [switch] types in my param declaration. Doh ![]() "Kiron" wrote: > param($filter, $path) |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Script Parameters Dear Kiron, When I tried to run the script, I got this error message: "The term 'param' is not recognized as a codlet, function, operable program, or script file.". Kindly help. Thanks. Paul Cheuk. "Kiron" wrote: > To specify parameters in a script use the param statement at the beginning > of the script. Script parameters could be pass in any order as long as the > parameter name --or its first chars-- is placed before its value. They are > consider positional when no parameter name is defined, any extra values are > picked up by $args. > > @' > # beginning of scriptX > param($filter, $path) > "`$filter is $filter" > "`$path is $path" > if ($args) {"`$args is $args"} > # end of scriptX > '@ > scriptX.ps1 > > > # either way works properly: > ./scriptX -Filter *.ps1 -path ./ > ./scriptX -path ./ -Filter *.ps1 > > # positional assignment > ./scriptX *.ps1 ./ > > # positional incorrect assignment > ./scriptX ./ *.ps1 > > # extra values picked up by $args > ./scriptX -path ./ -Filter *.ps1 1 abc 009 yyz > > > -- > Kiron > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Script Parameters The 'param' keyword is used to declare parameters for script files, functions and filters. Parameters are declared inside parenthesis. The 'param' keyword is not used when the parameters are declared before the script block of a function or filter. Place the following in a file named scriptX.ps1 # beginning of scriptX param($filter, $path) "`$filter is $filter" "`$path is $path" if ($args) {"`$args is $args"} # end of scriptX Then you can test it. -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Passing parameters to a PS script | PowerShell | |||
| Startup Script Via GPO... Where Are Parameters Stored? | VB Script | |||
| passing parameters to script (ps1) | PowerShell | |||
| How to run PS script with parameters from a batch file | PowerShell | |||
| How to run PS script with parameters from cmd batch file? | PowerShell | |||