I am passing $args to my script.
I want to know the $args syntax that can take in "any" number of arguments.
If i put $args[1], it means 2 arguments.
I want to take in *any* number of arguments ..what is the syntax ? Is it
$args[] or $args() ?
I am passing $args to my script.
I want to know the $args syntax that can take in "any" number of arguments.
If i put $args[1], it means 2 arguments.
I want to take in *any* number of arguments ..what is the syntax ? Is it
$args[] or $args() ?
On Jan 18, 10:44 am, "IT Staff" <jkk...@xxxxxx> wrote:Once inside a script, all of the parameters passed to the script are
> I am passing $args to my script.
>
> I want to know the $args syntax that can take in "any" number of arguments.
>
> If i put $args[1], it means 2 arguments.
>
> I want to take in *any* number of arguments ..what is the syntax ? Is it
> $args[] or $args() ?
contained in $args, whatever the number. You access each one using
array subscripting, like you have shown above:
# argument one
$arg[ 0 ]
#argument two
$arg[ 1 ]
If you use a param statement at the top of your script (which I highly
recommend, as it makes scripts much easier to maintain), $args holds
any additional arguments.
# test-param.ps1
param( $first, $second )
$first
$second
for ( $i = 0; $i -lt $args.count; $i++ )
{
$args[ $i ]
}
# test-param.ps1
PS$ .\test-param.ps1 "first" "second" "third" "fourth"
first
second
third
fourth
I hope this clears things up a bit.
Jeff
Hey Jeff,
Shouldn't that be $args[0] and $args[1] ?
> # argument one
> $arg[ 0 ]
> #argument two
> $arg[ 1 ]
Anyway, IT Staff, try this code in a script to get an idea of how $args work,
save it to argstest.ps1:
-----------
for ($i=0; $i -lt $args.length; $i++)
{ Write-Host 'This is $args'[$i]':' $args[$i] }
Write-Host "`n"'And this is outputting $args by itself'":`n"
$args
-----------
Now run it like this, in PowerShell: .\argstest.ps1 one two three four five
six seven eight nine ten
As long as you use $args, you can use any number of arguments. Because $args
is an array that takes in
any number of arguments. Feed it 1, it has 1 element. Feed it 1000, it has
1000 elements.
And to answer your other question, because it is an array, you use the square
brackets [] after the array-
name. Or simply $args if you want to output the whole array.
However, like Jeff, I'd recomment using the Param functionality, something
I only just began to do myself.
Best Regards,
Jacob Saaby Nielsen
http://www.pipforhelvede.net
gmail: jacob DOT saaby
hotmail (IM): same as gmail
> On Jan 18, 10:44 am, "IT Staff" <jkk...@xxxxxx> wrote:
>> Once inside a script, all of the parameters passed to the script are
>> I am passing $args to my script.
>>
>> I want to know the $args syntax that can take in "any" number of
>> arguments.
>>
>> If i put $args[1], it means 2 arguments.
>>
>> I want to take in *any* number of arguments ..what is the syntax ? Is
>> it $args[] or $args() ?
>>
> contained in $args, whatever the number. You access each one using
> array subscripting, like you have shown above:
>
> # argument one
> $arg[ 0 ]
> #argument two
> $arg[ 1 ]
> If you use a param statement at the top of your script (which I highly
> recommend, as it makes scripts much easier to maintain), $args holds
> any additional arguments.
>
> # test-param.ps1
> param( $first, $second )
> $first
> $second
> for ( $i = 0; $i -lt $args.count; $i++ )
> {
> $args[ $i ]
> }
> # test-param.ps1
>
> PS$ .\test-param.ps1 "first" "second" "third" "fourth"
> first
> second
> third
> fourth
> I hope this clears things up a bit.
>
> Jeff
>
On Jan 17, 9:44*pm, "IT Staff" <jkk...@xxxxxx> wrote:If I read your question correctly, I'm working on a similiar problem
> I am passing $args to my script.
>
> I want to know the $args syntax that can take in "any" number of arguments..
>
> If i put $args[1], it means 2 arguments.
>
> I want to take in *any* number of arguments ..what is the syntax ? Is it
> $args[] or $args() ?
causing me to make a post.
http://groups.google.com/group/micro...af8c8c7c79a401
$script = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "X:
$x Y:$y"; $x+$y}
$parms = 4,5
$script.InvokeReturnAsIs($parms)
In this example, $parms can have any number of entries and accessed
through the $Args
On Jan 18, 8:56*am, cashfo...@xxxxxx wrote:If the script is an existing function it can be invoked as in this
>
> If I read your question correctly, I'm working on a similiar problem
> causing me to make a post.
>
> http://groups.google.com/group/micro....powershell/br...
>
> $script = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "X:
> $x Y:$y"; $x+$y}
> $parms = 4,5
> $script.InvokeReturnAsIs($parms)
>
> In this example, $parms can have any number of entries and accessed
> through the $Args
example:
function test
{
$x = $Args[0]
$y = $Args[1]
Write-Host "X: $x Y:$y"
$x+$y
}
$testdef = Get-ChildItem function:test
$parms = 4,5
$testdef.ScriptBlock.InvokeReturnAsIs($parms)
"Jeff" <jeff.hillman@xxxxxx> wrote in message
news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxxExcept for any arguments that have been bound to defined parameters
> Once inside a script, all of the parameters passed to the script are
> contained in $args, whatever the number.
(function($a,$b) or use of param($a,$b)). In this regard, PowerShell $args
behaves like Rest parameters in Lisp.
--
Keith
On Jan 19, 7:27 am, "Keith Hill [MVP]"
<r_keith_h...@xxxxxx_no_spam_I> wrote:Keith,
> "Jeff" <jeff.hill...@xxxxxx> wrote in message
>
> news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx
>>
> > Once inside a script, all of the parameters passed to the script are
> > contained in $args, whatever the number.
> Except for any arguments that have been bound to defined parameters
> (function($a,$b) or use of param($a,$b)). In this regard, PowerShell $args
> behaves like Rest parameters in Lisp.
>
> --
> Keith
This is what I meant to point out with my second example with the
param statement, but I guess I chose my words poorly. Thanks for
clearing it up.
Jeff
"Jeff" <jeff.hillman@xxxxxx> wrote in message
news:a46e0f04-5c2f-42e5-8497-16837e278dca@xxxxxxOops. Indeed you did point that out. Sorry 'bout that. :-)
> On Jan 19, 7:27 am, "Keith Hill [MVP]"
> <r_keith_h...@xxxxxx_no_spam_I> wrote:>
>> "Jeff" <jeff.hill...@xxxxxx> wrote in message
>>
>> news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx
>>>>
>> > Once inside a script, all of the parameters passed to the script are
>> > contained in $args, whatever the number.
>> Except for any arguments that have been bound to defined parameters
>> (function($a,$b) or use of param($a,$b)). In this regard, PowerShell
>> $args
>> behaves like Rest parameters in Lisp.
>>
>> --
>> Keith
> Keith,
>
> This is what I meant to point out with my second example with the
> param statement, but I guess I chose my words poorly. Thanks for
> clearing it up.
--
Keith
| Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What's wrong with args[0] | Curious | .NET General | 5 | 27 May 2009 |
| @args isn't working here | RickB | PowerShell | 3 | 18 Sep 2008 |
| Is this a bug with @args | RickB | PowerShell | 4 | 09 Apr 2008 |
| Help with $Args in script | David Kriz | PowerShell | 2 | 12 Feb 2008 |
| $args.count | IT Staff | PowerShell | 0 | 27 Dec 2007 |