Groups > Microsoft > Windows PowerShell > Is there a better way how to get command-line params like WSH ?




Re: Is there a better way how to get command-line params
like WSH ?

Re: Is there a better way how to get command-line params like WSH ?
Tue, 8 Apr 2008 07:59:26 -0700
On Apr 8, 9:37 pm, David Kriz <david.k...@ccv.cz> wrote:
> I can write scripts in WSH.
> WSH supports (in WSF-files) two types of command-line params:
"named" and
> "unamed"
(Seehttp://msdn2.microsoft.com/en-us/library/y2d0z4f9(VS.85).aspx).
> I like "named".
> But PowerShell probably don't support "named", and because I must
repeatedly
> write this code:
>
> /---------------------------------------------------------------\
>
> ...
>
> $I = [int32]
> $InParam = [string]
> $InParamValue = [string]
> $InputFile = [string]
> $InputDate = [DateTime]
> $S = [string]
>
> foreach ($InParam in $Args) {
>    $I = $InParam.indexof(":")
>    $I++
>    $S = $InParam.substring(0,$I).ToUpper()
>    $InParamValue = $InParam.substring($I,($InParam.length - $I))
>    switch ($S)
>      {
>        "/I:" {$InputFile = $InParamValue }
>        "/D:" {$InputDate = [DateTime] $InParamValue }
>      }}
>
> if ($InputFile.length -lt 1) {
>    $InputFile = "some default value"
>
> }
>
> ...
>
> \_______________________________________________________________/
>
> Is there a better way how to do in PowerShell?

David,

You can do this quite easily with the param keyword at the top of your
script:

### Show-Param.ps1
param( [string] $file, [datetime] $date )

"File: $file"
"Date: $date"
###

The usage on the command line is a little different:

# positional
PSH$ .\Show-Param.ps1 "jeff" "2008, 4, 8"
File: jeff
Date: 04/08/2008 00:00:00

# named
PSH$ .\Show-Param.ps1 -date "2008, 4, 8" -file "file.txt"
File: file.txt
Date: 04/08/2008 00:00:00

# you only need enough of the name to unambiguously identify it
PSH$ .\Show-Param.ps1 -d "2008, 4, 8" -f "file.txt"
File: file.txt
Date: 04/08/2008 00:00:00

I hope this helps.

Post Reply
Re: Is there a better way how to get command-line params like WSH ?
Tue, 8 Apr 2008 09:55:28 -0600
"David Kriz" <david.kriz@ccv.cz> wrote in message 
news:uATNJYYmIHA.5956@TK2MSFTNGP03.phx.gbl...
> I can write scripts in WSH.
> WSH supports (in WSF-files) two types of command-line params:
"named" and 
> "unamed" (See 
> http://msdn2.microsoft.com/en-us/library/y2d0z4f9(VS.85).aspx).
> I like "named".

In addition to the positional and named parameters that Jeff and Steven have 
mentioned, PowerShell also supports what is called Rest parameters in Lisp. 
That is, any parameter that isn't bound by name or position is available in 
$args.  IOW the "rest" of the parameters can be found in $arg e.g.:

PS> function sum($a, $b) { $tot = 0; $tot += $a + $b; foreach ($arg in 
$args) { $tot += $arg }; $tot }
PS> sum 1 2
3
PS> sum 1 -b 2
3
PS> sum 1 2 3 4 5 6
21

--
Keith 
Post Reply
Re: Is there a better way how to get command-line params like WSH
Tue, 08 Apr 2008 10:05:51 -050
David Kriz wrote:
> I can write scripts in WSH.
> WSH supports (in WSF-files) two types of command-line params:
"named" 
> and "unamed" (See 
> http://msdn2.microsoft.com/en-us/library/y2d0z4f9(VS.85).aspx).
> I like "named".
> But PowerShell probably don't support "named", and because I must

> repeatedly write this code:
> 
> /---------------------------------------------------------------\
> 
> ...
> 
> $I = [int32]
> $InParam = [string]
> $InParamValue = [string]
> $InputFile = [string]
> $InputDate = [DateTime]
> $S = [string]
> 
> foreach ($InParam in $Args) {
>   $I = $InParam.indexof(":")
>   $I++
>   $S = $InParam.substring(0,$I).ToUpper()
>   $InParamValue = $InParam.substring($I,($InParam.length - $I))
>   switch ($S)
>     {
>       "/I:" {$InputFile = $InParamValue }
>       "/D:" {$InputDate = [DateTime] $InParamValue }
>     }
> }
> if ($InputFile.length -lt 1) {
>   $InputFile = "some default value"
> }
> 
> ...
> 
> \_______________________________________________________________/
> 
> 
> Is there a better way how to do in PowerShell?
At the start of your script, you can use

param([string]$InputFile, [DateTime]$InputDate)

When you run your script you can then pass it parameters either 
positionally or via names.

Examples:

./myscript.ps1 'C:\somefile.txt' '04/08/08'
./myscript.ps1 -InputFile 'C:\somefile.txt' -InputDate '04/08/08'
./myscript.ps1 -InputF 'C:\somefile.txt' -InputD '04/08/08'

Parameter binding in PowerShell starts with named parameters.  After 
named parameters, it fills positionally.  You only have to specify 
enough of the named parameter to uniquely identify it to the script, 
function, or cmdlet.  I used your variable names, but if you wanted to 
use $File and $Date, you could use -f and -d to identify your arguments.

Hope this helps...

Steven Murawski
Co-Host - Mind of Root
http://www.mindofroot.com
Host - PowerShell Basics
Post Reply
Re: Is there a better way how to get command-line params like WSH ?
Tue, 8 Apr 2008 11:43:44 -0600
"Keith Hill [MVP]" <r_keith_hill@mailhot.moc_no_spam_I> wrote in
message 
news:421DDFFD-6103-44E7-948D-4F74F6F8C622@microsoft.com...
> "David Kriz" <david.kriz@ccv.cz> wrote in message 
> news:uATNJYYmIHA.5956@TK2MSFTNGP03.phx.gbl...
>> I can write scripts in WSH.
>> WSH supports (in WSF-files) two types of command-line params:
"named" and 
>> "unamed" (See 
>> http://msdn2.microsoft.com/en-us/library/y2d0z4f9(VS.85).aspx).
>> I like "named".
>
> In addition to the positional and named parameters that Jeff and Steven 
> have mentioned, PowerShell also supports what is called Rest parameters in

> Lisp. That is, any parameter that isn't bound by name or position is 
> available in $args.  IOW the "rest" of the parameters can be
found in $arg 
> e.g.:

Oops that should be:

... the "rest" of the parameters can be found in $args ...

--
Keith
 
Post Reply
Is there a better way how to get command-line params like WSH ?
Tue, 08 Apr 2008 16:37:09 +020
I can write scripts in WSH.
WSH supports (in WSF-files) two types of command-line params: "named"
and 
"unamed" (See
http://msdn2.microsoft.com/en-us/library/y2d0z4f9(VS.85).aspx).
I like "named".
But PowerShell probably don't support "named", and because I must
repeatedly 
write this code:

/---------------------------------------------------------------\

...

$I = [int32]
$InParam = [string]
$InParamValue = [string]
$InputFile = [string]
$InputDate = [DateTime]
$S = [string]

foreach ($InParam in $Args) {
   $I = $InParam.indexof(":")
   $I++
   $S = $InParam.substring(0,$I).ToUpper()
   $InParamValue = $InParam.substring($I,($InParam.length - $I))
   switch ($S)
     {
       "/I:" {$InputFile = $InParamValue }
       "/D:" {$InputDate = [DateTime] $InParamValue }
     }
}
if ($InputFile.length -lt 1) {
   $InputFile = "some default value"
}

...

\_______________________________________________________________/


Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact