![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | $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() ? |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: $args On Jan 18, 10:44 am, "IT Staff" <jkk...@xxxxxx> wrote:
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 | ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: $args Hey Jeff,
![]() 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
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: $args On Jan 17, 9:44*pm, "IT Staff" <jkk...@xxxxxx> wrote:
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 | ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: $args On Jan 18, 8:56*am, cashfo...@xxxxxx wrote:
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) | ||||||||||||
My System Specs![]() | |||||||||||||
| | #6 (permalink) | ||||||||||||
| Guest | Re: $args "Jeff" <jeff.hillman@xxxxxx> wrote in message news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx
(function($a,$b) or use of param($a,$b)). In this regard, PowerShell $args behaves like Rest parameters in Lisp. -- Keith | ||||||||||||
My System Specs![]() | |||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||
| Guest | Re: $args On Jan 19, 7:27 am, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_no_spam_I> wrote:
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 | ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: $args "Jeff" <jeff.hillman@xxxxxx> wrote in message news:a46e0f04-5c2f-42e5-8497-16837e278dca@xxxxxx
-- Keith | ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is this a bug with @args | RickB | PowerShell | 4 | 04-09-2008 07:58 AM |
| Help with $Args in script | David Kriz | PowerShell | 2 | 02-12-2008 09:00 AM |
| $args.count | IT Staff | PowerShell | 0 | 12-27-2007 08:53 PM |
| $args in runspace invoke | William Stacey [C# MVP] | PowerShell | 0 | 05-09-2007 11:10 AM |
| -match within where-object against $args[0] | Joe D | PowerShell | 2 | 12-04-2006 11:23 AM |