Pass Params to Scripts (Powershell)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys tonight we're gonna talk about passing parameters to powershell scripts now I hadn't really planned on doing a video tonight but I was on somebody's blog who said that I don't release enough video so I couldn't recommend this site as a good training site so it kind of got me all riled up so I'm gonna make a couple videos tonight anyway just to spite him so tonight we're gonna talk about like I said what did I say again oh yeah passing parameters to power shows to powershell files okay so there are a couple different ways that you can pass a parameter when you call a powershell script and i've got the first way right here and i recently used this in a class so I'm not gonna rewrite it those of you who were in the class will recognize this so what we've got here is we've got arguments right and arguments or the args parameter is a is not only one fine parrot but it's a it's a zero-based array so you can see here that I'm taking the zero-based array and I'm assigning it to a local variable called server name then I'm taking the I'm taking args one and typing it to to the local variable location sorry my my mind just went blank there for a second and then all I'm going to do is print these out to the screen no you don't have to assign these to local variables but like it says up here in the notes if you don't then it can get pretty confusing if you've got a really really long script and you've got like 17 arguments and you're sitting and you know you've got different paths and different file names with you know Arg 15 and Arg 12 and Arg 2 and arg0 and Arg 17 and armed 9 you have no idea what those things are right so it's always best when you when you use this method to go ahead and assign them to local variables so that when you're actually using them in your script you know what the hell you're doing ok here okay so all I've got to do is call this script and I think I'm gonna have to get there first actually I still think I have let me see something real quick okay so I'll get us I gotta change directories - nice path I'm glad I still have that in my profile okay so if I do it Durham that you see here I've got args that ps1 remember you always have to call a you always have to call a script with the location so it's either you can either hard-code the location or it can be a relative path but you always have to use the location of some kind you have to qualify it args dot ps1 and you don't even have to have the ps1 if it's the only args in there and here I'm gonna say my server and my location just because I'm so imaginative and there you go I got exactly what I was looking for server name is my server location is my location I can call those again and we'll say Shawn's server Shawn's location and you see I get the same thing I'm just passing those parameters right so one more time here what I did was I used the args our favorite pirate variable I know that's stupid what do you expect from me it's late and we used the first one is zero and the second one is one and then they just go up from there right so it's a zero-based array then I just assigned them to local variables which I certainly didn't have to write I could easily say server name and args zero and args one down here but for for documentation purposes it's a lot easier to do with this one and also if I wanted to document it out here right the server name is cool and the location is cool but you can you see you can you know you can document these guys and if you know if you get it from someplace special or if something in there needs some kind of documentation so if you if you not only set this to args one but if you set it to artist one and then do some math around it before you present it to the rest of the script then you'll want to put that up here you know put your reasoning and and you know why the math is done and so on so that's how you do it with args close that no I don't want to save the changes and you can also just flat-out pass it parameters right so there's a per M you can say per M and then in the parentheses just separate your parameters by with commas and it's the exact same thing you notice how this is the same so all we're really doing is by passing the arguments right now what this does for you or what the other one does for you is it allows you to document a lot a lot easier and you notice how here I've got the server name and location here so it's a it's gonna be a lot longer and a little bit Messier if you've got a lot of parameters or if you've got some math to do on these parameters before you presume the rest of the script it's gonna be a lot Messier and you may end up and you may actually end up presenting it to local variables anyway but you know if I wanted to comment this one a little bit it would be the same thing it would look like that right um except I would want to say something like that right you you want to document what this what this parameter is all about so but anyway you'll see here that it works the exact same if I come here params you notice I get the exact same thing right here I'll even clear the screen so it's easier for you to see so I got params ps1 Shawn server Shawn's location and it just prints those back to the screen right here the server name here and the location name there it works the exact same there's really there there's no difference whatsoever and how these things get handled I just think that for small scripts that you're gonna pass things and this one will work just fine but if you're gonna pass in a lot of if you're gonna if you're gonna pass in a lot of parameters and and or do some math on the parameters before you pass them to the rest of the script then I would personally use the args method instead but you know here you would just the same thing I mean you would take location or you could say location or Ridge and then say location equals location or Ridge and then do your math there that's kind of doubling up on the work but anyway that's how you pass parameters to powershell files and good luck
Info
Channel: Jen and Sean McCown - MinionWare
Views: 17,454
Rating: undefined out of 5
Keywords: SQL Server
Id: HucGlAftCLo
Channel Id: undefined
Length: 7min 46sec (466 seconds)
Published: Mon Oct 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.