Understanding $args in PowerShell
-
@chutestrate said:
There is a special variable $Args which contains an array of the parameters passed to a function. Let’s use this variable to show how we can pass an argument using string expantion.
Function HAL {“What are you doing $args ?”}<enter>
Type in the FunctionName and an Argument:
HAL DaveYou can use multiple $Args variables to pass more than one parameter.
Oh my, you've gone down the rabbit hole. I see that you are reading this page which is horribly written for teaching:
http://www.powershellpro.com/powershell-tutorial-introduction/powershell-functions-filters/This is super confusing because of calling functions directly from the command line. Stop trying to use this, this is very hard to read and understand for no good reason. This is accurate, but much more complex and way past the point where you are now. Right now you are just trying to learn the most basic data structures. This is beyond what we've been talking about here (but doesn't conflict in any way.)
-
Nothing in there is wrong or different, he's just adding a complex example of creating a function directly from the command line rather than making a program and calling it that way. There is absolutely no reason to be adding that confusion here. Let's learn this material first then learn how to do that and how that is just another way of looking at what we have already covered.
-
Yeah, I'm kinda good like that, not in a good way. Ok, done with that then. Well, that is the source of my confusion of the "args is an array" now that I'm re reading it.
ok
-
I think the complexity there is that he is calling things in "yet another way." It's good to learn that you can do that (calling a function directly loaded into memory rather than running a full program that you saves) but it is just introducing too many concepts at once rather than one at a time and giving each a chance to sink in before providing the next one.
I find the new system of showing people live shells before teaching them full programs to not be a good one for understanding what programs are doing. But that might just be my old school years talking. I started programming on BASIC in 1985 so my perspective on learning languages is rather tainted by the era in which I did it.
-
Well for whatever reason I just lost power so maybe it's a good reason to look things over again tomorrow. Plus trying to type on phone is tough. I'll try to make the connection of $args again.
-
Fun. Lots of snow there in PA? I hope that the weather is good, I have to fly to San Francisco in the morning and I'm not far from PA.
-
No snow. Just my part of the block. Have a safe trip
-
Thanks. Lifting off about noon and heading south first thing, so probably safe. Nothing is delayed so far.
-
This has been a great discussion. I'm learning a bit about PowerShell just from this.
-
Yes, very good discussion. Did I just see @Rob-Dunn pop in too?
-
I was on my phone; Martin sent me a PM over at SW
I had responded to this question over there in the Powershell forum, but chutestrate's account is in moderation for some reason.
Anyway - I'll try to take a stab at this.
As mentioned, when you run a script or a function, powershell has a built-in mechanism that will interpret anything passed after that script or function (that are not named parameters) and places them into an $arg *object.' It's kind of like a basket that catches the other stuff passed that hasn't been explicitly handled in the script/function. Since someone could feasibly pass more than one argument to the script, Powershell needs to have a way to handle more than one argument...just in case. With that being said, there's always an $arg object, whether you've passed arguments or not. It's just there to handle anything that fall outside named parameters during execution.
Remember, everything is an object in Powershell. It can represent one thing or many things. In this case, $arg is an object that represents many things; i.e an array. $arg is a representation of that array.
So, you might run a script like this:
test.ps1 -ComputerName <COMPUTER_NAME> -ProcessName <PROCESS_NAME> more arguments
or even like this:
test.ps1 -ComputerName <COMPUTER_NAME> -ProcessName more <PROCESS_NAME> arguments
Since we've specified -ComputerName and -ProcessName, those are named parameters (as denoted by the dash '-' followed by the name of the parameter you are passing.), so anything that is passed outside of those named parameters will be placed into the $arg object. In either of the above examples, $arg[0] would be 'more' and $arg[1] would be 'arguments.' Like Scott and Martin said, $arg is an array, and in the script/function, the [0] and [1] just helps identify which item in the array we want to return.
-
-
Rob thank you for doing that. I've watched it a few times, and I'm not catching what you are trying to demonstrate. I'm sorry.
-
I need to correct my last post. I understand the named parameter demo, but not the $args piece of it.
-
Are you able to describe in any way how you are finding $args confusing? I realize that when you don't understand something that there is really little way to describe how it is being misunderstood. If you knew what you didn't understand... you'd understand it. But I'm trying to come up with some way to figure out what is being missed. If you know what an array is, you know $args. So I'm wondering if arrays aren't understood or if you just aren't accepting that $args is just the name of an array or what.
-
Ok, I'm kinda with you at this point. Correct me if anything I'm thinking is incorrect. I'm using english, not the correct terms.
I understand that $args is automatic. I believe the mental block is the where it is an array, or is it creating and naming an array [$args[0], $args[1], etc.) Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?
I believe I'm ok with the concept of explicitly creating and naming an array.
What I'm having the heartburn over is how $args is an array, or creates an array, or was just put there to drive me a little crazy. Do you have to put it in the code you are developing? I've been given some code examples that included it. Were these just examples of what would look like or is it necessary to be used if not creating/naming an array. If it is automatic how do you use it later on?
Hopefully, these questions give some kind of hint to my confusion.
Thanks again for all the support.
-
sigh. I just don't think this is going to happen. I think it's been explained as many ways as possible, and I keep missing the message.
I think it's time to let this go.
The support has been terrific thank you.
-
@chutestrate said:
Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?There is an array. That array's name is $args. That is all.
Yes, each program gets one $args and it is always an array.
-
@chutestrate said:
What I'm having the heartburn over is how $args is an array.
This is where you are stuck. $args is an array. That is all that there is to it. It just is.
-
@chutestrate said:
Do you have to put it in the code you are developing?
No, not if you don't want to use it. You never have to use anything that you don't want to.