Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Strategy for handling parameters to a script

Closed Thread
 
Thread Tools Display Modes
Old 06-27-2008   #1 (permalink)
Jacob Saaby Nielsen
Guest


 

Strategy for handling parameters to a script

Hey guys,

anyone got any good way to handle parameters to a script ?

E.g. the script takes certain parameters.
What's the best way to check if they're set, and if they are to use them,
if they're not, how to not use them.

Anybody did something they wanna share in that regard ?

I'm thinking something that builds a command, but there might be a much better
way.

Please share how you accomplish this

Best Regards,
Jacob Saaby Nielsen

http://www.ijacob.info
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail


Old 06-27-2008   #2 (permalink)
Jacob Saaby Nielsen
Guest


 

Re: Strategy for handling parameters to a script

Hey,

perhaps I should supply an example.

Let's say TestScript had 10 possible parameters you could pass to it.

E.g. "TestScript /whatever:0 /morewhatever:4 /evenmorewhatever:hellno" and
so forth.

How do you:

1. Best make the script do what it should do, based on the values in those
parameters
2. Any difference to using /command, -command or something else ? Does PS
require something particular here ?
3. How do you best make the script avoid the parameters NOT set ? E.g. in
the above example, 7 are missing of the 10 - how do you stick to the script
JUST using the 3 that are used ?

I'm looking for any and all ways to do it, since it's simply for inspiration
for finding a way that works (for me), and I was kinda wondering

Best Regards,
Jacob Saaby Nielsen

http://www.ijacob.info
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail
Quote:

> Hey guys,
>
> anyone got any good way to handle parameters to a script ?
>
> E.g. the script takes certain parameters.
> What's the best way to check if they're set, and if they are to use
> them,
> if they're not, how to not use them.
> Anybody did something they wanna share in that regard ?
>
> I'm thinking something that builds a command, but there might be a
> much better way.
>
> Please share how you accomplish this
>
> Best Regards,
> Jacob Saaby Nielsen
> http://www.ijacob.info
> gmail: jacob DOT saaby
> hotmail (IM/LinkedIN/Facebook): same as gmail

Old 06-27-2008   #3 (permalink)
RickB
Guest


 

Re: Strategy for handling parameters to a script

On Jun 27, 7:27*am, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
wrote:
Quote:

> Hey,
>
> perhaps I should supply an example.
>
> Let's say TestScript had 10 possible parameters you could pass to it.
>
> E.g. "TestScript /whatever:0 /morewhatever:4 /evenmorewhatever:hellno" and
> so forth.
>
> How do you:
>
> 1. Best make the script do what it should do, based on the values in those
> parameters
> 2. Any difference to using /command, -command or something else ? Does PS
> require something particular here ?
> 3. How do you best make the script avoid the parameters NOT set ? E.g. in
> the above example, 7 are missing of the 10 - how do you stick to the script
> JUST using the 3 that are used ?
>
> I'm looking for any and all ways to do it, since it's simply for inspiration
> for finding a way that works (for me), and I was kinda wondering
>
> Best Regards,
> Jacob Saaby Nielsen
>
> http://www.ijacob.info
> gmail: jacob DOT saaby
> hotmail (IM/LinkedIN/Facebook): same as gmail
>
>
>
Quote:

> > Hey guys,
>
Quote:

> > anyone got any good way to handle parameters to a script ?
>
Quote:

> > E.g. the script takes certain parameters.
> > What's the best way to check if they're set, and if they are to use
> > them,
> > if they're not, how to not use them.
> > Anybody did something they wanna share in that regard ?
>
Quote:

> > I'm thinking something that builds a command, but there might be a
> > much better way.
>
Quote:

> > Please share how you accomplish this
>
Quote:

> > Best Regards,
> > Jacob Saaby Nielsen
> >http://www.ijacob.info
> > gmail: jacob DOT saaby
> > hotmail (IM/LinkedIN/Facebook): same as gmail- Hide quoted text -
>
> - Show quoted text -
The example you give shows 'executable' style parameters
rather than PowerShell style. I'm not sure it's clear if you
intend PowerShell to interpret the parameter syntax you've
presented or need it to built the parameter string for an
executable.

PowerShell doesn't deal with parameters the same way typical
executables do. If, as I suspect, you are looking to interpret
stuff like this being passed to your routine you really need to
have a good reason because it won't be easy. If you can pass
your parameters in the PowerShell supported way it's very easy.

TestScript -whatever 0 -morewhatever 4 -evenmorewhatever hellno

function TestScript ([int]$whatever = -1, [int]$morewhatever = -1
[string]$evenmorewhatever = '', $anything) {
if ($anything -eq $null){"anything not passed"}
...}
Old 06-27-2008   #4 (permalink)
Shay Levi
Guest


 

Re: Strategy for handling parameters to a script

Hi Jacob,


For Powerhell parameters use '-parameterName value' instead of '/parameterName:value'

There are several ways to define/validate a parameter inside a script/function
and . Basically to find out if a parameter has a value:

param ($foo)
if($foo) { 'param $foo has a value' } else {'no value found for $foo'}



You can initialize the parameter with a default value (if no value supplied
by the caller):

param ( $foo = "defaultFooValue")



The default value for switch parameter is $false. When the caller specifies
the parameter then PowerShell sets the value to $true.

param ( [switch]$foo )
if($foo) { 'the caller specified $foo, do something...'}


Make $foo mandatory (throw error if no value has been specified):
param ( $foo = $(throw 'must have a value')


Using parameter types, convert the parameter to a type of your choice. If
possible, PowerShell convert the value to the specific type otherwise the
user gets an error.
param ( [string]$foo)


Must be a number and have a value (an error is thrown if the value is not
a number):
param ( [int]$foo = $(throw 'must be a number')



Handling parameters is a mixture of all above methods.





---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

JN> Hey,
JN>
JN> perhaps I should supply an example.
JN>
JN> Let's say TestScript had 10 possible parameters you could pass to
JN> it.
JN>
JN> E.g. "TestScript /whatever:0 /morewhatever:4
JN> /evenmorewhatever:hellno" and so forth.
JN>
JN> How do you:
JN>
JN> 1. Best make the script do what it should do, based on the values in
JN> those
JN> parameters
JN> 2. Any difference to using /command, -command or something else ?
JN> Does PS
JN> require something particular here ?
JN> 3. How do you best make the script avoid the parameters NOT set ?
JN> E.g. in
JN> the above example, 7 are missing of the 10 - how do you stick to the
JN> script
JN> JUST using the 3 that are used ?
JN> I'm looking for any and all ways to do it, since it's simply for
JN> inspiration for finding a way that works (for me), and I was kinda
JN> wondering
JN>
JN> Best Regards,
JN> Jacob Saaby Nielsen
JN> http://www.ijacob.info
JN> gmail: jacob DOT saaby
JN> hotmail (IM/LinkedIN/Facebook): same as gmail
Quote:
Quote:

>> Hey guys,
>>
>> anyone got any good way to handle parameters to a script ?
>>
>> E.g. the script takes certain parameters.
>> What's the best way to check if they're set, and if they are to use
>> them,
>> if they're not, how to not use them.
>> Anybody did something they wanna share in that regard ?
>> I'm thinking something that builds a command, but there might be a
>> much better way.
>>
>> Please share how you accomplish this
>>
>> Best Regards,
>> Jacob Saaby Nielsen
>> http://www.ijacob.info
>> gmail: jacob DOT saaby
>> hotmail (IM/LinkedIN/Facebook): same as gmail

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
handling errors in a script Darren Mar-Elia PowerShell 4 11-29-2007 07:56 PM
Passing parameters to a PS script Dan PowerShell 9 07-17-2007 05:05 PM
Script Parameters David PowerShell 4 07-17-2007 02:26 AM
passing parameters to script (ps1) IT Staff PowerShell 1 10-19-2006 09:43 AM
How to run PS script with parameters from a batch file =?Utf-8?B?Um9tYW4gS3V6bWlu?= PowerShell 1 09-09-2006 01:35 PM








Vistax64.com 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 2005-2008

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 47 48 49 50