Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Script Parameters

Reply
 
Old 06-20-2007   #1 (permalink)
David


 
 

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 SpecsSystem Spec
Old 06-20-2007   #2 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 06-21-2007   #3 (permalink)
David


 
 

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 SpecsSystem Spec
Old 07-16-2007   #4 (permalink)
Paul Cheuk


 
 

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 SpecsSystem Spec
Old 07-17-2007   #5 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46