Understanding $args in PowerShell
-
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.
-
@chutestrate said:
If it is automatic how do you use it later on?
No idea what you mean. It's an array. Use it whenever you want. It isn't like arrays go bad after a period of time or something. It's just an array. As long as you know what an array is and that this is an array, that should explain everything. It's just an array. It's not required, it's not magic, it doesn't time out. It is absolutely exactly like every other array.
-
I think you are right. I'm stuck between $args and arrays. Fine, $args is an array to use or not use. Then $args[0] and $args[1] are different arrays?
-
nm, i think i get it now