Powershell Advanced Tools and Scripting Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay yeah how's mine look is it looking okay almost okay a little bit a little bit more okay oh hey hello everyone welcome to the microsoft virtual academy and this is behind the scenes and this jump start is going to be about scripting and tool making and actually we call it advanced tools and scripting and we'll talk more about what this jumpstart is going to be my name is jason helmick and to my left the inventor of powershell most of you already know the distinguished engineer jeffrey snover jeffrey how you doing this morning i'm doing great thanks oh awesome we had such a great feedback from the first one yeah of this that we did now the first one we did almost universally great almost universally yeah highest numbers highest attendance right um what are the other things it was the highest attendance um there was a whole bunch of records that we brought with the class beat us on two points on satisfaction so there were some unsatisfied people out there that so hopefully they have not dialed in tonight when we get to the end we need we need we need to beat rick claus by two points two so yeah well he beat us by two should we beat him or if we beat him by four then we own it so yeah do us a favor and and rick's a friend he'll he'll appreciate it yeah i don't care if he appreciates it or not anyways guys if you joined us for the first one we were using powershell interactively we were using it to get real work done and taking you through how powershell and the console works how to use get help and how to figure out how the pipeline works in this one and we'll get into some more details here this one we're going to shift a little bit instead of spending our time in the console doing things in real time we're going to move to scripting ooh ah and we're going to show you how to start getting started yeah um using uh scripting and we're going to show you some of the scripting language how to put the scripting language together but most importantly what we're going to be doing today is showing you how to take solutions that you're making with powershell and turn them into actual tools and to build tools and make like a toolbox and so that they can share them with other people and things like that um before we get started let's get started with some introductions first of all jeffrey let me bring up the slide here why don't you introduce yourself a little bit so i'm jeffrey snover a distinguished engineer and uh there you go so now i'm also the there you go says it all i'm the lead architect for windows server 2000 or what was the i was the lead architect for windows server 2012. i'm now the lead architect for windows server and for the system center data center products because we kind of have a unified organization now we're all very excited about that by the way yeah have you seen a recent blog posts been rolling out the blogs about windows server 2012 r2 and uh they're really good really clear about hey here's what we delivered in 2012. here's what you told us and here's how we responded in terms of uh the next set of features so really exciting stuff yeah very excited about it and now let me go ahead and introduce myself uh i'm jason helmick uh jason the jason homick if you notice the twitter there's a long story behind that no we're not gonna go there uh a senior technologist for concentrated technology and um i'm just a powershell affection auto i just love powershell so that's all you need to know about me so here's what we're gonna be doing first of all if you wanna play along and follow along today with us you might want to grab hold of the scripts now i've put together a package of scripts that there's the url and you're probably going oh crap man did you really expect us to type in that url no hang on just watch oh oops wait a minute hang on hang on let me get my need some help there i think i do i'm going to take you out to you're going to be learning from this guy all day what do you think about that oh here we go oh wow that just went so wrong in so many ways i don't even know what happened there it's being all over the place let me just see if i can get one of these up guys the slides are right here there go out to powershell.org www.powersh and i put them into a blog post it's a couple of posts down because i put them up a couple of days ago so he said don jones just can't shut that guy up you can't shut don jones up scroll past it all so right down here is you'll see jumpstart two and if you click in there i have a download for the script so that you guys can follow along with us and you have the scripts too i'm gonna try to minimize my hand typing today because we've got a lot of scripting that we want to take a look at so go ahead and grab those when you get a chance and we'll be using those today okay let me see if i can bring my slide deck back up for some reason things are moving a little bit slow but we'll figure this out come on you can do it you're a big computer now [Laughter] i swear to god i'll get a slide here so here's what we're going to do today um if you take a look at our at our agenda what we're going to do is we're going to get you started with scripting now there's going to be some review if you were with us in the the first jump start one of the things uh we towards the end is we got you kind of started on the direction that we were using for this one so this might be a little bit of review for some of you but i know we've got a lot of new people that have joined a lot of new people yeah lots have joined us today so this will be cool even higher attendance than the last time or at least registered attendance so yeah as a matter of fact guys they're really concerned that you might overload the circuits if everybody keeps joining in so keep joining in yeah yeah yeah that'll be great that's the goal crash the servers that'll be great yeah uh and we're then we're going to go into powershell scripting language now we're going to take you through the basics of the scripting language and some important constructs that you'll need that you'll start working with right away and then we're going to go into starting to build scripts and functions now let me just tell you that our focus today is taking a solution that you've already created at console and turning it into a script but more importantly starting to turn it into a useful tool that you can share with other people and a tool that you can use so we're going to be taking you into what's called advanced functions and don't let the word advance scare you it's just kind of a fun word to let you know that you're going to do something really cool now and you're going to be able to create awesome tools that look and feel well just like commandlets right yeah absolutely that's the whole point and and i have a a quick question you know there everybody's going to see this we have the ability to in powershell make something that looks smells and tastes like a commandlet exactly and i don't have to be a c-sharp developer using visual studio to do this why'd you guys think of this i mean where did this come from yeah so this is a great piece of uh uh background by the way uh turns out we're not crashing the mva but we are crashingpowershell.org so yeah nice uh yeah so uh hold off try it again uh try it again we'll we'll get there yeah that's awesome okay background so here's a kind of fundamental thinking around uh uh powershell and and uh scenarios so at some point there was this like why are you building this powershell thing why not just go figure out what the scenarios are and then implement the scenarios yeah and so at some point we referred to this as you know scripting scripting is the anti-scenario scenario right which is to say so here's basically the problem right because i've done this a long time i'm gonna go i'm gonna you're you're uh my target audience so i'm gonna spend all day like shadowing you that's a term shadowing i'm gonna shadow you i'm going to see what you do i'm going to write it all down i'm going to figure it out at the end day i know exactly what your scenarios are and so i go back to the office and i spend the next three years implementing a solution to your scenarios that said half an hour after i left all your scenarios changed changed you got a new boss he says no we're not doing it that way anymore or you got some new technology or there's just a change in philosophy whatever scenarios are constantly changing so if you had something that said you know do my scenario click this button it's not going to work okay and so that's what the whole compositional management model is all about to give you and by the way so everybody's scenarios are different because you have different politics you have different philosophies you have different technologies that you integrate in when you add a user do you just get to add the user or do you get to add a user but then joe over here has to add them to the hr system and oh before they get access to this you know somebody else has to give authorization or okay it etc everything's always different and so the model of the philosophy around compositional management and powershell was to give you a toolbox a toolbox of tools that then you can then go quickly produce a solution to scenarios as they pop up and the whole point about this was again what we said in the first session was we want to create a world where you think about what you want you type it and you get it okay so here's where you come in literally i talked to a website i won't tell you which one but the big big big internet presence running everything on powershell and at some point they're the guys they were super excited about this and they went to their operators and they said okay hey we're gonna you know manage everything with powershell and they said okay great i said how do you want to manage the system and they said what does that question mean he said well go up to the whiteboard and type down the things you want to be able to do and like okay and they said well you know every monday we want to find all the front-end servers where this is the case and then we want to restart them he says great he said what do you mean great he said well that's what we'll do what do you mean the tool to do he says well that's what you'll type he says look here all we'll do is you put a dash between the where and you put a squiggle bracket and the stylus and underbar and uh and that's and you're going to type it like that and they're like really yeah really so that's the point you want to start with thinking about what things you want to think type do and here's the key point each customer is going to want to think type and do something differently so today is all about how you take the basic components of powershell you use the scripting language to produce your specific things to think type and do and what's yeah and what's really cool about this is that um the idea of you already doing something and then turning that into a tool that somebody else can use so we're gonna show you the scripting how to make the tool and build the tool and all the options you have with making a tool and you're going to find that because this is built into the language and because this is written using powershell in powershell's part of the scripting language it's pretty straightforward it's pretty easy and it actually becomes fun to do this you'll type something out in real time and then you'll just immediately turn it into a tool it'll be that easy so if you take a look again at our course topics we're going to be starting with building scripts and functions and then getting into advanced functions and you'll get to see how those work one of the characteristics of a tool is that they have parameters so we're going to show you some of the options this time you can do with parameters and we're going to go back and talk about making help because help is one of the most important things when you're making a tool and we'll also get into things like error handling and tools that make changes how to get the dash confirm and what-if options and then at the very end we're going to show you how to bundle all of this properly so that you too can have your own modules of your own tools your own little toolbox if you will by the way stay on this slide a second so it's very interesting to note what we're talking about here is what we call the admin development model right and we're very explicit about this and the idea is i think i mentioned the first session you know bruce payette superstar bruce bad said hey the average lifetime or the lifetime of 99 of scripts uh start at the command prompt and terminate with carriage return right okay so that is to say a lot of things are done very ad hoc now then you say well geez i do this and i do this and i do this i do this a lot and i'm getting a repetitive stress injury and i don't want a repetitive stress injury because muffy and buffy need to go to college so i need this job so what you do is you say okay well hey put that in a file and instead of typing it out each time you run the file oh great and then you say well but it's almost right except it's not quite because sometimes i have to go and i open up that file and i change the the name of the computer or the name of something over and over again well then you parameterize it and okay well that's great so now it's parameterized and then you might have it more and more levels of parameterization to make it wider and wider useful and then at some point you say hey you know jason i heard about the script you're working on and uh can i see it and you say oh no it's embarrassing you know just give me give me a couple minutes and i'll give it to you and at that point you're going to get share it with somebody so you make it a little bit more formal you know kind of clean it up use proper verbs maybe even document something and then you share with somebody and you're like hey that's pretty cool and then we decide hey we're going to run this on our production servers so this is no longer just this little private thing you're going to run it on a production server so at that point you want to say well okay wait i need a little bit be more formal i need to have documentation i need to have help they say hey this is quite useful we're going to make it available to other people in forms of modules so if you think about it that's what we're doing here we're going to walk you through step by step this progression of of formality you know it's perfectly fine to have like i'll tell you what the number of times that i write scripts called t because they're all called t t tap it's just t it's really easy to type to t and i go and then at some points okay well i'm gonna do something with it and you just walk through each one of those steps and so that's what we're going to do with you today and then along these lines just to set some expectations for you if you take a look at our expectations slide look this is designed for the i t pro but if you're a developer thanks for joining us because this is a great opportunity to see the cool things that you can do with powershell and there's all kinds of interesting ways that you might find fascinating with powershell as well this is kind of high speed but today is going to be scripting and you've got all the examples that we're going to be using i just want to point out a couple of things for you as some supporting material first of all i've mentioned this in the last one and i'll mention it again now powershell.org as long as it doesn't crash today has a great forum site where you can go out and ask questions at the forums there's wonderful people that are answering questions there and you can do this at any time also coming soon to the microsoft marketplace there is a microsoft course called 55038 that is a full five-day version of going through this step by step by step plus more than what we're able to do today all on tool making which also ties into some of you may already have this learn powershell toolmaking in a month of lunches by don jones and jeff hicks which is a great book that also goes through all of this on making tools yeah by the way that's a that is a great book it really captures that spirit of translating things from instead of just focusing on doing getting the job done so here's the thing each of us you know every everybody's got a job in reality you don't you have two jobs you have two jobs so the first job is the one that you know about you're supposed to get something done oh the server has to have this installed on it i need to do this i need to do that we all have that job but there's a second job and that second job is to do the first job in a way that maximizes your integrated lifetime earnings which is to say we all took calculus right from most of us to guys you know earnings over time he'll maximize that area under the curve which is to say that there are some ways of doing your job which finally gets you through the year but then next year what are you going to get paid or if you need another job what kind of job are you going to get and what are you going to get paid and there's some that minimize that okay that minimizes your pay and then there's those that maximize your pay which is to say hey i'm going to do this job and i'm going to accomplish it but i'm going to do it with a script and i'm going to do with a script that means i can do it across a large set of systems and i can do this and i can be more productive guess what if you're more productive you get more money it's pretty simple okay and then by the way and if your employer doesn't get that like hello mcfly i'm more productive than these guys i should get this year's big bonus you know big chunk of the bonus queue like if they don't get that you put your paper out on the street and other people will say oh this guy's really productive we'll hire him and we'll pay him that much more so anyway so those are your two jobs get the job done and get the job done in a way that maximizes your integrated lifetime earnings don jones book it's like super clear on this point it's all about that become a tool maker not just a solve your today's problem yeah and your job role has definitely changed in this respect is we're not just doing real-time administration now now you're the one solving real business problems and turning them into tools for others and so that's what our focus is going to be today so join the mva community those of you that are watching this either you're live or you're excuse me you're watching the recording you've already started to join the mva community by the way if you notice towards the bottom here here's the special code if you want to get some extra points for this event it expires 9-2 so make sure you tell all your friends about this as the video comes up online questions or comments you guys are have access to the chat room and we are very fortunate to have wonderful mvps sharing their time again to help answer questions in the chat room so please feel free to ask your questions and hopefully we'll get answers out there for you now let's get started into our first module get started scripting yeah i know it's i was trying to make music out of it so here's what we're going to do that almost worked it almost worked here's here's what we're going to do we're going to talk about the purpose of powershell and the purpose uh wait a minute i missed a slide the purpose of powershell we'll take a look at some reminders on getting powershell v3 installed launching powershell and some of the security aspects so that we can run scripts in here so let's get started with and this might be familiar if you saw the previous show the purpose to powershell and actually we've already been talking about this it's its purpose is so that you can do quick real time management in large scale and its purpose also is so that once you've done it well then automate it stop typing it you know i type in something once that's going to be the last time i type that thing in because if i i know if i'm going to need to do it again i'm going to want to automate it so i'm going to want to put it into a script and if i want to help my my fellow admins out i'm going to make a tool out of it so um we're going to be the tool makers this time and by the way that that picture there that is me with hair no it's not me with hair um we'll see that's just it everybody gets confused between the right or the left is which one is so using powershell we're going to make our lives much easier in tool making so some notes on installing powershell we went through this the in the previous episode but it's worth talking about again powershell v3 currently runs on windows 8 and server 2012 and also on windows 7 sp1 and server 2008 sp2 or server 2000 r2 sp1 boy that's a mouthful that's why i had to write it all out now you can download this this is version 3 is referred to as the windows management framework and i have a link there for you where you can download it now guys take a moment and read the instructions for this couple of things for you first of all you're going to need the full version of the net 40 framework not the client install so make sure you install the full version of it so that um the update can run and you can get powershell installed and something else i want to point out to you is is that if you're installing v3 understand that we're working on windows 8 boxes and working with server 2012 because windows 8 and server 2012 comes with more modules and more commandlets there are some commandlets that we have that if you're on a windows 7 box they just weren't available then just sorry they're not there today you're going to see that we're going to be focused on scripting and it's going to be the same across all your different versions of windows even if you are and you should not be but if you are running windows xp and you have powershell v2 it's all going to be pretty much the same there too so you're going to be fine but well you're not fine you need to get off of xp but besides that we'll okay so mcfly please get off of xp for crying out loud um so launching powershell now guys um let me just do a quick demonstration here with the magic computer and launching powershell if you're on windows 8 hit that cool little windows key and start typing the word powershell and you'll find there's powershell now i always pin it to the taskbar so right click and pin that bad boy to the taskbar once it's pinned let me get back out of here don't do this don't and i left click on it we had this conversation in the first one if it comes up it says windows powershell you got the wrong thing what you want is let me close this right click and say run as administrator there's your powershell console for you and that'll bring it up notice it says administrator up here that gives me all the authority that i need to be able to work with powershell something else to keep in mind is we're going to be working with the ise and we're going to talk about the isc in just a few minutes but i just want to point out if you right click here you can also say run the ise as administrator and so we'll be spending a lot of time in that tool today now i have to show you in the slide if you're using an older os then you may have to click on start and go navigate to accessories and then you'll find windows powershell in there but if you're hip and cool and on windows 8 hit and cool then you get to hit the windows key and get the tile and life is all good now security jeffrey yeah we need to be able to run scripts and we talked about this in the last show but we need to talk about this again um we need to be able to run scripts and there has been a lot of times in the past where some scripting languages have been very dangerous in the way that they've been implemented yes script no um and bad things have happened yep now you guys put in some additional controls they know they're they're not so much you know hardcore security goals but they're kind of to prevent somebody from making foolish mistakes and things like that so talk about a couple of these like that um powershell scripts don't run by default exactly so the first thing to be aware of is that we developed uh started working on powershell after bill gates's famous trustworthy computing memo right so we had seen uh you know lots of bad stuff going on in particular the use of scripting languages i love you to vbs i love you too man no i'm sorry um later i love uwwbs and uh caused the problem so we knew we had to pay special attention to this so uh that's why uh when you double click on a powershell file it does not run the script what it does is it brings it up in notepad or an editor of your choice okay so too we have this execution policy now the original execution policy was restricted what restricted meant was that you can do interactive sessions but you can't run scripts okay and the whole notion the whole philosophy around powershell security model is to make you aware and to make explicit decisions about things and so whenever there's something that that has a security ramification in general we tell you about it what the ramification is and then ask you to confirm like are you sure you want to do that so when you change the security policy to say oh i'd actually like to run scripts we tell you okay here's the risks associated with that and we have different ways you can run scripts one is to say free for all wherever it comes from doesn't run it you know drunken sailor mode we should have renamed that drunken sailor mode matter of fact i'm going to bring that slide up and i i should change it to say drunken sailor mode and the one he's talking about here is uh is is unrestricted exactly brought up the ether and having some fun yeah okay the mezcal yeah they're messed up we had a conversation last night about mezcal anyway so um so that's that or remote signed and associated with each one in the help the help's actually quite good on this topic we tell you what it does and what the risks are and what the benefits are and then you make the uh the appropriate decision now the interesting thing about this is um we've been changing things so as we have more and more coverage and it becomes more and more an integral part of managing windows server what we first did was turned out remoting was off by default we turned it on by default for server because when you manage lots of servers we want to minimize the effort to do what we now consider to be a mainstream scenario which is to script and automate lots of computers the second thing we did in this is in windows server 2012 r2 is we changed the execution policy so now the default execution policy is remote signed which i think is is spectacular um and so and guys let me show you why this is kind of spectacular first of all keep in mind that if when you make a script it's just a text file and what's going to happen is you double click it it launches in notepad could you change that file association could you make it launch in something else or could you make it actually just run powershell and run the script you can but you shouldn't you should just leave that alone that protects you in case you or one of your friends in the office accidentally downloads the script that could be bad so if you ever see somebody change that association to run it with doubleclick give them a dope slap like shun them because it's stupid stupid stupid yeah so by default you're not even allowed to run scripts well and that's what we have these execution policies for and i'm going to show you setting my execution policy here in just a second but as jeffrey mentioned restricted is what's on by default on everything but the new server 2012 r2 because quite honestly when you're managing servers one of the first things you have to do is turn on an execution policy for scripting and to have the new default changed for server 2012 r2 makes life a lot easier you know it is basically we just constantly evaluating that risk reward ratio and there are so many things powershell is becoming such a mainstream way of doing things uh the benefit is very high and frankly we just have now years and years and years of experience with powershell um and uh basically we haven't been having any security issues so the we're getting more and more comfortable uh with the risks associated well when you announced it at tech ed i just was beside myself because that's one of the first things that you okay you want to manage your environment first of all we have to turn on the execution policy on your servers so now guys if you're not at 2012 r2 yet and you're not so no you're gonna you're gonna need to set this policy let's take a look at a couple of these unrestricted we've already talked about bad bad bad this means you can run anything that you write or that you download from the internet folks be really careful about what you download from the internet it's can be dangerous in fact it could be quite dangerous so it's good to always know what you're going to run before you run it however they put in some helpful things such as all signed and remote signed and what the sign means and we actually talked about this in the last episode signed means that the script has been signed with a digital signature in other words there was a certificate that was used to sign it this certificate identifies who this person is when they made this script and so what happens is it if they wrote bad stuff in there we know who you are and we can hunt you down so a sign script gives you a better level of confidence with where the script's coming from now in this case the execution policy is stating for remote signed you can write anything you want and it doesn't have to be signed but if you download it if it gets tagged from the internet and as we said in the last one you do know that we know everything you download right we do we've been there a long time even you xp people we know it gets tagged and we yeah i know i know you now you're scared um remote sign means anything you download we're not going to let you run just automatically you're going to get prompted you're going to say hey look this has not been signed this is no no so that's probably the best policy for you today hey jason you know what that was a that was an awesome explanation yeah but it was wrong don't what do you mean it was wrong you just described the uh vbscript uh execution policy so vbscript says vbscript has a mode they don't have the notion of remoting or not but they have this mode that says you can only run sign scripts and you can set that and it'll only run signed scripts and so what it does is it says hey is this script signed powershell is not that powershell's remote execution script assigned mode says i'll only run scripts that are signed by people i trust well there's a huge difference so yeah you're absolutely right you're absolutely right so by people that you trust means what that means that uh not just someone did it so in vbscript you can run some evil thing but you can find the evil person that destroyed all your systems and and say oh some fair sign knows who it is what we say is no no first it has to be someone that you can identify and then it comes up and says hey currently you don't trust things from evil corp like evilcorp.com can be known by verisign that doesn't mean you don't want to be running their code right right so you say hey no i want to trust everything from microsoft and i want to trust everything from my internal i.t department they'll sign things but then if i see some script coming from evilcore.com even though it's signed don't run it which is an important distinction because you have a much better control over this thank you thank you very much so let me show you now guys you can set an execution policy by using a group policy let me show you how to do it on a standalone we have a couple of commandlets for this mine's already been set for today but let me just kind of show you i'm going to go out here to run as administrator and i'm going to type get execution policy mine is now set for is currently set at remote sign so i can run some scripts if you check yours and yours says restricted well you can change that with set execution policy and put in remote signed now when you make a script you too will be able to use it now it's going to come up and ask you mate to make sure because it is a security change go ahead and say yes and then you're ready to rock and roll and we can start scripting now and actually running them so now that we have an execution policy and we can run some scripts let's talk about running scripts there's all kinds of ways to run scripts and what's funny is i don't have a test script so i'm going to make a quick test script and show you a couple of ways to run them and we did this also in the first show but let's see here i'm going to do let's do this uh let's see notepad because we'll talk about the isc in a second and i'm going to make a simple script called get service dash name bits it'll get i'm going to do file save as and i'm going to go out here to i've got this scripts folder and i'm going to save it as test dot and the extension for scripts is ps1 hit save now i'd sure like to run this well again powershell has some built-in security around this and jeffrey help me out with this it's for um i have to do one of two things i have to either type the full path yep to the script and no tab completion will help me with this why is it again that i have to type this full path even though you're in that direction even though i'm there all right but we have shortcuts for this but why do i have to put in this full path uh we don't put in the full path but you have to put in an explicit path in an explicit path yes so concretely you cannot execute things in your current directory um by just giving their name and this prevents what's called a lurking attack this has been known in unix forever where an evil person would put like a direct a file an executable file in a directory called print working directory or cd and if you went to that directory and then you tried to say where am i print working directory or cd you would first run their executable so this is called the lurking attack and uh so we fixed that lurking attack you can't do that and so here's what's going to happen guys if you try to run the script like i just made see it's sitting out here see i just made test ps1 if you try to run that script by typing test ps1 um sorry uh no the term test ps1 is not recognized as the name of a command a function or script file and this is confusing to a lot of folks a lot of people kind of go well wait a minute i just typed it in and it's it's not running well they tell you here at the bottom how to fix your problem they also give you a reference uh for your your get help with the about underscore help but let me show you there's two ways you can do this you can type the full path if you're not currently sitting in the folder of the script you can type the full path and it's scripts and run it this way or if you are in that location there you go and tab completion again will help you out and you can run the script this way but you should remember to move your uh screen down so the video oh yeah yeah yeah i got to make sure that i have my screen down somewhere so you can learn my shiny head and all this so i'll just see the text i'll make sure i'm not typing over here so you guys don't don't freak out now guys i don't have a function in here yet but we will do this later on we also have a way of running scripts that will you'll see today that'll keep the functions as we build them keep them loaded into memory so that we can work with them we don't have to do this this much today because we're going to be using the isc and the isc well let's talk about the ise because that's next on our agenda anyways we're not going to use notepad to write our scripts in now if you want to use notepad i feel sorry for you but um we want to use something better than notepad and something better than notepad hmm i wonder if the powershell team helped us out with this too and yes they did watch i'm going to right click and say run ise as administrator so jeffrey we got we've got four easy payments of absolutely free scripting tool here yep and this scripting tool is this is more than notepad this is a lot more than notepad so let's talk about this a little bit some of the features that we have here now it is when it comes up by default it shows um i've got a scripting pane up here where i can start typing and i've got a console down here that i can test stuff in and what's all this over here for the commands so this is a new feature called show command and it allows you to explore the commands and you can type in things look for things look by things by module look for name like well let's look for one of my favorite ones get service right and then when you click on it we show you these parameters right and so notice that they're tabs those are the parameter sets and by the way this is just a wonderful tool to really go explore things now go to the second one this display name display you notice the star next to the display name that says it's mandatory and if you hover over it tells you it tells you it's mandatory so then you can start filling things in and then when you do that let's do this because i i um i don't i don't can i put a star in there yeah and then put a star in there and then you can either run it insert it or copy it well i like this let's try running it oh looky looky looky and i didn't get a result because guys i don't remember what the name of the service is but that's or what the beginning of it oh you think it's background or something like that background back let me do that and we'll run it and you can see it run in the console oh look you ran the a but here insert oops let me click up here you can copy this and paste it or just insert it watch i'm going to hit ctrl v look at that it took it built it took made my commandlet for me and put it right up here so actually this is pretty cool to explore things you know i've you obviously know i've been working with powershell for a while now just a while and yet i use this thing all the time to go explore and i'm constantly finding new things well and so guys go ahead and use this today but for uh screen size and stuff i'm going to close this pane today but i want to show you some other things first of all you probably noticed now watch this i'm going to go up here and i'm going to start typing get dash this is really cool this is much better than notepad at this point because look i'm getting this intellisense that's helping me find this and i can tab complete and you see what started happening there it's helping me remember what the parameters are how to use them i can go in and start typing and you'll also notice that with the intellisense we also get color syntax now one of the things that has just really impressed me and i remember you had made mention of this just as uh the beta had just come out and i hadn't played with the ise yet and you and maine mentioned we were talking about the console at some conference and i said you know i just i love the console but the console you know it it feels it's old you can't modify it you can't use control c and control v to copy stuff out of it wouldn't it be cool to have a can't put unicode characters in it yeah but utica wouldn't it be cool to have a cooler console than than than the current console and you said well have you launched the ise and this was for version three or version two i'm trying to remember now and some version some version and guys you you've noticed this watch it this console that's here looking i love this it also has the the the intellisense and it has the color syntaxing and it's beautiful and you can test things here and then go oh this is awesome i want this watch ctrl c and then ctrl v cut and paste it's beautiful yeah it's absolutely beautiful so do you think that most admins will be using this console more than the older one absolutely yeah definitely more than the older one i mean the the the cases where you still use the older one is either if you've are you know you're sitting at a a server core box and it has the console um although frankly the better way to do that is to get on a client box and use remote powershell to access it yeah that's probably the only case you'll use it by the way you shouldn't note that powershell ise is just one of a number of great ises out there a number of the third party products have fantastic tools that go well beyond the capabilities of that which we ship in powershell isa and what i like to tell a lot of admins is there are some great third-party tools i'm a huge pro proponent of sapiens tools their primal script and their powershell studio because they also solve particular business problems but when you're just getting started this is the tool to start with and then when you start to run into those business problems that these great ids and these great ises solve that's a great time to move to those and there's plenty of information out there on when and why you'd want to move to them you know think of it as the ise is is you know much better than notepad but think of some of these third-party tools as the visual studios of of powershell as well there's quite a few out there and if you just go to thepowershell.org powershell.org has a bunch of them listed you can also just ask questions and people can help you check out and find all the different ones that are available to you yep now a couple of other things about the isc before we uh take a break um there's a couple of options up here i want to i want to show you guys this gives you your script pane and a console and this is probably the view that i'm going to be in most of today however you can also have it go side by side and a lot of people like this because if you have two monitors yeah put that line right in between them and then you get power shell on one side and a script pane on the other which is really cool because as you're gonna see the best thing to do is you're gonna use your powershell kung fu skills and you're going to get event log you're going to solve the problem first log name system give me newest three and when you've solved the problem now you're going to oops i mean grimace you're going to copy this over and you're going to paste it in here and you're going to save it as a script so that you don't have to type it all again and we're going to do that and show you how to make that even cooler by turning it into a tool now the other one that i usually use on my desktop is is uh where they're maximized this is where you get a full screen for it and i also like this view um to have the command pane up because it gives me a nice view and i can explore with the command pane up the other cool thing that i like about this is control r and i'm going to use ctrl r there's also a button over here that will do it as well but ctrl r will flip between the two and this makes it so that you can you have a full size console you can work here you can cut and paste from here and then paste back over into your full-size scripting pane however i'm going to go back to this one anything else we should show them before uh we uh take a break and get into the hardcore stuff and i think this is the good stuff this is good yeah the basics yeah you'll see more we'll get into debugging i'm sure at some point that's cool stuff yeah yeah so basically powershell ise is your kind of uh bare bones uh environment they're third-party ones with much richer capabilities but it's a great environment to do your own uh you know scripting and debugging and exploration of the environment that's really what it was meant for is to just you know increase the volume of things get people off notepad we found just a shocking number of people writing scripts in notepad and honestly we were just embarrassed yeah notepad is is not the way to go and since this is four easy payments of free get started with this and this is all you'll need um today as we work through getting with uh uh starting all of our scripting stuff so what we're going to do here is in a moment we're going to take a break but when we come back when we come back we're going to start getting into the scripting language itself and so we're going to take you through some of the basic parts of the scripting language that you're going to need these are also some of the most powerful parts and you can see us use it all day long if statements and loops and for each and all kinds of stuff as we start building tools so take 10 and come back and see us in yeah because when we come back we're going to get right into the into the meat of it so make sure you download those scripts because you're going to need them it'll be helpful [Music] i don't know who's funny are these guys back in production or what they they just did this we're gonna start in three two oops wait a minute it's like all right so now we're going to get started with the scripting language and taking people through the basics that they need to know the scripting language and powershell but okay so we just told everybody hey go to powershell.org crash that server and when it comes back up download the scripts because you're going to use these scripts and we talked about the execution policy and said best practices remote signed so let's put those pieces together you just downloaded a bunch of scraps and your execution policy is remote signed and so you've gotten a demonstration of how it works which is to say you're not allowed to run any of those scripts we intended that yes to make a point and thank you for pointing out in the in the chat room now there's two things you can do first is you can change your execution policy you can change it to um unrestrict it for just this though for justice bad idea here's a better idea so let's go let me show you how this works so here in on ise i can say i i dot i i is short for invoke item and dot brings it up and so what you can do is you can right click things go to properties and there's something i've already fixed this so uh sorry yeah it's right it'll show you if it was blocked yep and then you can unblock it however i already fixed this um there is a version of a new commandlet called unblock file and so what you do is uh i'll cd here you see i'm in jumpstart i i put everything there and what you can do is you can say unblock file star star and i do verbose and what it does is it unblocks all those files you know okay so here's the model for unblock unblock is for when you have looked at the file you you brought it down low download it from somewhere and you trust it now and so what you do then is to say i i it i unblock it and uh we're telling you by the way is it safe well i marathon man is it safe yeah it's safe okay i didn't put anything nasty in there guys yeah so you get we're telling you jason is telling you that makes me clear about that that there's nothing evil in those files i'm not telling you that jason that it's safe to unblock so go ahead and unblock those files and then we'll use them for today's sessions and um by the way thanks for showing me the unblock because i didn't even know that that existed see i'm telling you it's this exploration and community right you try something and other people say why didn't you do it this way yeah i'd love that so boys and girls here's where we go we're going to start off with talking about variables how you make variables how you can use variables some fun tricks with quotation marks and how they work with variables and also if you watch the first episode this might be a little bit of a review but we want to talk about object members and using them with variables in other words everything's an object in this in powershell and we're going to send it to get member and there's all kinds of interesting things that we can do both with methods and properties when we have them in variables also i want to review how to utilize parentheses in powershell to call up some files like if you have a server list and a text file or a csv or something like that is one of the things that people were asking us about in the last episode so we'll show you some more on that then we're going to dive into what you're going to be using quite a bit some logical constructs which means if statements and how they work and then some looping constructs which are the loops that we can use in powershell i'm going to show you several different loops but there's really one or two that we're going to be focused on today that are the most useful ones that we'll be using throughout this so let's get started with variables now i've got all this on the slide for you but i'm going to open up my scripting pane here and i'm if you've noticed it guys let me just show you in this is module two i put in a startup script that will just launch the ise and put all of the demo scripts in this module already as tabs right across the top in the ise so i'm going to be using these i'm going to try to minimize my hand typing because as you can tell i can't type very well first of all variables place in memory to store stuff now variables in powershell um can store anything i mean they can store strings integers they can store objects objects and we don't really have to think about that as administrators we just do it and it just happens and that's it's very fascinating about variables that we don't have to think about it most of the time when we're interactive at the console but now when we start working with variables we're probably going to want to start thinking about what type of data we want in those variables and that kind of thing so guys let's take a look at a couple of examples here first of all one of my favorite examples setting a value to a variable and guys um one of the neat things about the ise is if i want this line to execute i just press f8 and you'll see it in the screen down here so that line executed i want to show you this is one of jeffrey's favorites he loved to show this one in the last one i want you to notice the use of the squigglies you can put spaces in your variables crazy but you can do it um and i'll show you that um i'll execute this one as well by pressing f8 when you want to get the output of a variable there's a couple ways you can do it you guys if you've worked at the console you know you can just type the variable and you see there's the value that's in there i'm going to hand type this down here though i want you to see what intellisense does with this guy for me because here's the problem you run into you you put a space in there and you kind of forget that you need squigglies around it look at the intellisense see how it found my space var well when i hit tab it put the squigglies in for me so if you if you do have to have a space in your bar you shouldn't at least intellisense will help you use it and you can get the value out of it now the key there was he did dollar sign and then m and did intellisense if you did dollar signs squiggly it's not going to work that's interesting yeah i would have had to hand type it all out yeah yeah okay you're on your own i'm on my own now guys this is how you can work with variables in the console interactively but when we start to script we want to be a little bit more structured about how we do things so the correct way at least for now to output a variable is to use the command right output now we're going to have several different versions of write where we can do warnings and all kinds of things but right output myvar says take this bar and put it out to whatever it is i want it to go out to later you'll see that this could be pipeline or something like that by default it's going to go out to the host or to the screen for us also in scripting we want to start to be specific about our variables and we want to start to tell our variables what kind of data they're going to hold rather than just letting the variable figure it out we're going to start to want to control this first of all it's much better script documentation in other words you have specified exactly what you want take a look at the example here i have in line 12. notice i'm going to say string my name is jason and i'm going to run that and so you can imagine that well we'll just type it out down here dollar sign my name it's got my name in it because this is string data and i have strongly typed what that variable is allowed to hold take a look at this though line 13. here i'm saying that the variable needs to hold an integer and look at the kind of data that i'm giving it oh let's see what happens when i try to run this oh i get an error message it's telling me that i'm not i'm trying to input the wrong data type into this var this is why you want to start to do this in your scripts because it can get confusing sometimes what kind of data is going in yeah this is a so this is a benefit of types types allow you you allows the system to do work for you right and it brings in a whole set of features so let me just draw one key distinction and that is where you use the type so here i'll show you i'll say dollar sign x equals integer one okay and so we'll execute that and indeed dollar sign x is 1. i can style sign x equals test that's an x okay so notice when i put the uh type that's bracket a name bracket when i put that on the right side that is what we call casting uh the value to that type but we didn't apply it we applied it to the top to the value and not to the variable and that's why we were able to reuse this and set it to a string okay so now imagine i do this imagine i say integer dollar sign x equals 1. okay same thing happened but now it gives an error so that's the distinction if you want the variable at this point in time to have an integer you do it this way if you want to say this variable can only contain integers you do it this way and what this is is let me give you another example of this as well here's you're going to see this a lot today a lot of times we're going to want to be able to do things like store a computer name or something into a variable so that we can use it later on here i'm using the commandlet oops i don't want to move it uh which i just screwed it all up control z control c control z says undo something read dash host this will prompt you to enter in a computer name and i want to store that in here well this is always going to need to be a string so i'm going to make it a string i don't want people i don't want this stored as an integer um and let me just go ahead and run this as an example and you'll see what it does here so you prompts me for my computer name i'll put in a computer name called dc and then we can of course print it back out now i know that working with variables doesn't seem like that big of a deal but making sure that you have cast them correctly that you've typed them correctly is going to become very important to us as we start working with parameters you're going to see this as we start to look at how the syntax gets built and how the parameters get built so it's good practice to start doing this now with your vars which is not what you're accustomed to doing when you're interactively working in the shell because the shell just handles it all for you yeah let me show one more point about the benefit of types and how they do work for you so here i'm going to show you i set a dollar sign d equal to some date right so this is christmas right my daughter's fascinated with christmas she always asks me for christmas right we're all fascinated you know there's the string okay so at this point it's a string and if i say dollar sign d dot you'll see i can do string things to it right copy things like normalize pad left pad write so that's pretty useful stuff right i don't know if you've seen that pad pad left pad right let's see pad left one hundred okay there you go okay so things like that okay however notice none of those things were anything to do with the date yeah so what i can do here is i can say hey let's let's do it this way it'll say hey this is a date time okay and now when i say dollar sign d dot look at this oh there's all the date stuff i have a whole set of functions okay so this is the power of types i can say well hey give me the day oh the 25th wait day day what was that day of the week so what what day is christmas going to be this year i have no idea well let's ask powershell it's going to be on a wednesday party they say okay well wait a second day dot you know i always like to say that my my in-laws live in in the uk so if we wanted to send them gifts you want to do that like a couple weeks before so what you can do is you can add days and if you use a negative number so let's say uh well let's see six seven days times let's say six weeks a negative number that tells you i have to ship them wednesday november the 13th to get there so types are your friend types allow you to do really crazy powerful stuff like if you just had a string and you tried to do that in c sharp or in python or ruby or whatever it's like best of luck my friend you're in for some heavy lifting some heavy lifting so and let me just show you guys since you have the slides i just want to point out in the slides that i've listed some of the uh types for you over here we're going to be spending most of our times with int and string and date time that kind of stuff but i did list them all for you in the slides and also as a reminder you do have commandlets to work with variables we're just not going to use them that often in our scripting we're going to be writing it all out and as you'll see it's pretty straightforward the way that we're doing it and just be clear this type this list here is a small representative sample it turns out you have the full uh net access you can do anything with that that's one of the interesting things is that people ask me all the time and and correct me if i'm i'm sure you will correct me if i write is that um there's no help file that lists the types right it is oh correct okay good because it's all of these types come from.net so this is something you could find on msdnsdn yep is a great example of where you can find all the different types that you can have and explanations of what they are yeah so awesome okay back to the did i close it oh goody i didn't but wait can we do a short little uh trip can you say types are like variables like these look like your regular old variables like anybody who's done any kind of programming or any scripting they're they're variables right and you you think you know what they are but trust me they aren't this is powershell okay so it's different these are powershell so uh concretely let me show you something here so so imagine i say well i'm gonna have some variable by the way i'm kind of foreshadowing something you're going to see when we get into scripting and scripting you can define parameters and then you can put certain what we call validation attributes yeah right which is to say hey uh this value it's a string but it can only contain these values okay so imagine i say i am here and i say i'm going to have some string i'll assign x equals a okay i run that happiness dollar sign x is a and if i want to say does an x equals test yeah that works too that's a dos and x is equal to test okay but what i can do here is i can go and i can use one of the things you're going to see a lot of later in the day called the validation string and what i can say is i can say validate set and i can say hey this thing can only contain values a b or c this is awesome okay so now i run this it works delta sign x is that like status and x equals b happy happy that's an x but then if i say dollar sign x equals test error okay so this is powershell my friends so very very powerful so the point is just because something looks familiar doesn't mean you've explored it so you want to explore this there's a very rich set of things now again that's just kind of a foreshadow you're going to see more of it later on in the day but but i think that's an important demonstration that it's not just what it's not your your your daddy's programming language they put a lot of stuff in here to make your life easier and better and so the validation is actually awesomely cool you're going to see us use it with parameters but as you just show you there's a lot of useful ways you can use it anywhere in your script so totally awesome and guys i'm going to go to this this fun with quotes which is example number two and i want to show you this and you saw jeffrey just now he was putting values in quotes and sometimes double quotes and sometimes single quotes let's talk about this a little bit the quotes do matter in powershell and something kind of happens depending upon your quotes take a look at line four here i've got a variable and i'm just going to set it to powershell so i'm going to run this not a big deal take a look at my first line here notice that it's between double quotes and it says well this is the variable dollar sign i and dollar sign i rocks well here's the interesting thing between double quotes the variables get resolved right yes this is this is actually kind of cool so i'm going to run this and take a look what my screen says this is the variable power shell and powershell rocks well powershell does rock but it resolved this one too hmm well when you use single quotes the variables don't get resolved oh so single quotes the variables won't get resolved so let's try this one and see what it prints to the screen ah this is the variable dollar sign i and well dollar sign irocs well that's not exactly what i want but first of all the important distinction when you put a variable between double quotes you're turning on a special feature that powershell then resolves it when you use single quotes powershell ignores that and doesn't turn on that special feature so you will see us use very distinctly sometimes single quotes when we don't expect a variable there or we don't want the variable resolved and double quotes when we do and you'll see that throughout today now in this case let me show you how to solve this particular problem because you can see my problem is is that i wanted to say this is the variable dollar sign i and powershell rocks well there's an interesting little character here notice you have to put it in double quotes because i want a variable to resolve and let me take it away and add it back in to the left of your number one key is a backtick that's an escape character and watch what happens when i have escaped that now powershell will ink this is the variable dollar sign i am powershell rocks so the escape character lets me control that inside of the double quotes like i said we're going to be using lots of quotes today and everybody keeps asking what's the difference well that's what the difference is and it's an important difference when we start to script an example is right here i'm going to set a computer name of client and then i'd like to use get service name bits and for computer name i want to use that variable notice i put it inside of double quotes because i want that variable to get resolved when this executes now i'm going to select this so you can see this and it works now i want you to notice hey jeffrey how come i can take out the quotes and it still works i was going to say why are you putting the quotes well i know because it in in in this is the thing you're going to see us use quotes a lot and with variables but sometimes you see us use command let's and we've got a variable there but we don't have we don't put quotes there usually yeah so um okay ready for the oh god here we go the real answer yeah the real answer back to the trustworthy computing memo okay trustworthy computing memo we did design this for security by default and there's a classic thing called a sql injection attack sql injection attack says oh um i get some input from the user and i place it here and i run the sql query and what happens is that the sql injection text says oh i get the value you might say you know select star from table where name equals grab this value put it in here right and then you say jeffrey snover and says get you that but instead of putting jeffrey snover what they do is they'll say you know jeffrey snover you know uh statement terminator and then something else like a completely different table like go gather this information credit cards or some credit cards or delete the database or something and because you take that string and you just concatenate it and then execute it all sorts of bad things happen i cannot tell you how expensive sql injection attacks have been well powershell has no such thing our parser was designed specifically to address these sql injection type attacks and so it's a completely safe parser and so what you're doing is you're not concatenating a bunch of strings together when you give an object that object's going directly to that parameter and so we don't have to put the quotes because it knows how to handle it and particularly because you're not creative the command line isn't a big string is it one big string absolutely so guys you'll see this a couple of different ways sometimes you'll see people using the quotes um in their scripts but most of the time with commandlets when you're putting in values you won't see it you'll see me not use it a lot today as well but at least now you know the difference between the two different types but wait so let me show them a trick because i don't think you showed this later okay so uh show my screen so imagine this okay so imagine i'm going to get process so let's show you what that gives you so if i get process right and so then what i'm going to want to do is i'm going to want to say hey i want to get the your process id equals and then show the value right i want to show this 800 and 800 800. so i assign it to a variable yeah and then i just go ahead and execute this and notice that didn't quite work out yeah so look here i got system diagnostics process lsass dot id okay so by the way so this is interesting right so you might have guessed this and if you did you would write dollar sign p dot two string ah okay so that is that so what's happening is is when you do this it's turning it's evolving that to a string it's evaluating that and then it's concatenating data id so that's clearly what you're not you don't want to do right so at this point you just throw it away and pick a pipe it's all c-sharp yeah because it's broke and it doesn't work oh wait no we didn't do that that's right that turns out this guy bruce is actually pretty smart guys smart guy so here's the deal and this is important guys this is actually really cool i'm saying it all right [Laughter] confuse me there for a second so here's the deal in when you do the string expansion one of the things you can do is you just put things in parentheses okay so you say dollar sign parentheses and what happens is we will execute anything in that in that and we'll take it we'll execute it and we'll run it so here what we're going to execute is dollar sign pid yes okay so we do this oh what did they do wrong that's right sorry uh they do yeah oh wait a minute is it is it id dollar sign p dot id oh yeah yeah there oh there it goes okay so voila now notice i said anything you want is is in here right so you can run anything okay so concretely i said read host minus prompt what should i give them okay so again what we do when we expand the string is we we see this dollar sign parentheses we execute anything anything anything can be can be a simple expression it can be a multi-line script it can be a 10-page script uh anything you want we execute it and we take the value and we plug it in here so when we run this it's asking me what value should i give them oh give them a day day day date and it just takes that value and expresses it so that's one of the key things you want to do about learn about it by the way and if i had done single quotes single quotes of course it would say does no right no expression evaluation now that's actually referred that that that dollar sign parentheses and i'll do whatever you want is is so very important because it's confusing sometimes when you're putting things into strings and you're getting back you know the type name and all that stuff that has a particular name it's it's called a sub expression right exactly like like a periscope um that's my sub expression joke is up it's gonna be a long day yup all right so can we can we party can we have it can we just yeah okay so here's the thing you might say oh why am i ever going to do with that stuff like this distinction like when am i ever going to want to have a dollar sign in stuff like that makes no sense well let me show you cool thing so first off we showed you before this notion of snippets okay so let's hear so snippets okay cool stuff okay now did you realize i now show you commands and i say snip oh i already did the snip oh you always clean that out pay no attention to this pay no attention the man behind the curtain okay so notice you can get snippets you can import snippets and you can create new snippets you can create new snippets okay so here is the description uh i'm gonna say underbar my var test right and that the title i don't know what the title is so i'm gonna test test title author of course jeffrey and text now this is the text that you're going to replace and obviously you don't want to type that all here so what i'm going to do is i'm going to uh what i don't know if you knew this in show command i'm going to say dollar sign script okay and before i run it now i have to put a script into dollar sign script and my script is going to be this whole thing here this is i'm going to put the whole thing now the problem is i've got all these dollar signs in here and i've got to preserve it exactly as is so if you haven't seen this there's something called hear strings hear strings hair strings are awesome so hear string is wizzy wig at and then quote okay notice everything is there here string is a multi-line string okay so it can contain carriage returns and if i do the quote it's going to take all those dollar signs and expand them and so i don't do that i use a single quote i come down here to the end do it again sorry that wrong what am i doing oh am i doing this wrong uh it um is it quote there we go given enough time and resources so now that that is a string okay notice it's exactly what's up there because that's the whole point and now what i do is i say okay i'm going to assign that to a variable dollar sign remember i mentioned the first thing powershell is a powerful language because i'm deeply flawed script equals so now i run this and so now i have this dollar sign script and so that's here and so now what i'm going to do is i'm going to run new ise scriptlet i give it a description oh that's the description i want that that's its name that's its title this is a test which shows stuff anyway so we run it here okay great so now i've created a new stip snippet i come here and i hit ctrl j and somewhere here there it is there it is there's my commandlet and when i type it in voila i get exactly what i got before so you can make your own snippets and when you create your own snippets you want to have exactly you're going to have dollar signs and all this funky stuff and you don't want any of that stuff expanded otherwise it's going to come out all wonky so what you do is you use a hear string which is a multi-line comment and you use the single quotes so you don't expand any of it so we're tying it all together so it ties together so back to that tool making back to that whole tool making learn the concepts put the concepts together party now powershell does everything as objects and so another concept we have to add in here before we start making scripts take a look at my screen i'm looking at the third example here is if you joined us in the last presentation this will look familiar what i'm going to do is i'm going to put an object into a variable and you can see i'm just going to do get service name bits and the interesting thing that happens is i'm going to pipe this to get member now pipe gm is is is the short way of saying i'm going to pipe it to get member so we can take a look at what's in there well what's in there is the actual service object and we can go up here and look at the name it's a service controller object whoops for bits and look at all the methods and the properties well this is kind of cool because now that i have that object stored in that variable i can now call or work with those properties in those methods one of those is down here called status it will give me the status of the service so look what i'm doing dollar sign service dot status see you can see that it's stopped and if i go back up here you'll see that there's a method which is something that the object can do called where is it start well since the service is currently stopped let me change this to to execute the method as start and guys if this is really unfamiliar we did this in the first episode this is kind of a reminder and as you can see when i execute this it's actually going to start the service notice here see how jeffrey showed us the sub-expression of periscope um the sub by the way i got that from ed wilson so blame him um uh up periscope look i've got a message that i want to print out to screen service name is if i didn't put it inside of this sub-expression that jeffrey showed us this would come out with a weird name you know dot service controller and just it's not what you want to see so inside the sub expression i'm saying show me dollar sign service dot name and i'm using another method to upper to make it all uppercase so i just wanted you to be able to see this i'm going to put it in there and i'll print the message out service name is bets so again putting objects into variables you can then pipe it to gm to see what methods and properties are available you can execute and look at those methods and properties and you can also then start using them in messages and again that uh sub-expression becomes very important that dollar sign parenthetical now working with multiple objects variables are very variable uh come on that was okay and so it wasn't even the guys in back are going please so just look at this instead of doing a single object i'm going to execute and do notice that i'm using get service that's going to be multiple objects well the beautiful thing about this is and i'm going to do this down here in the lower pane is that variables are arrays and if i want the first element in that array and again we talked about this in the first episode i can put in the element number and we start counting from zero and so the very first service in this list i can also go through and look because we're going to use this quite a bit i want that first service but i want to see its status so i'm going to dot status and notice the ise is helping me and that'll get the status of the service so you can call individual objects in a collection and then check their properties and execute their methods as well and so then you can uh dash one get you the last one and here's a couple more examples that i just put in here for for you to be able to play with of using a sub expression with a particular object in a collection where i'm doing dash display name or changing the name to upper so you can see that working with variables especially a collection of variables is very important to us for our our scripting one last thing before we get into the actual script language itself well this is the script language but parentheses everybody's been asking about these parentheses um how can i you know get you know a list of computers out of a text or and how do i work with that so i wanted to make sure you can see that can i show them something sure okay so notice we're having fun with arrays okay oh yeah yeah yeah do we talk about that later well we go ahead and talk about it so you notice he did this we did you know service equals this and you have dollar sign services and you see them all okay happy happy but are you aware of the range operator right right the range operator so the range operator is dot dot so one dot dot three four okay it generates things right so you can say you know uh 10.15 okay now check this out services dollar sign services 1.5 i like that i like that a lot that's awesome so you think that's call yeah you think that's cool well yeah i think that's totally cool 5.1 backwards also did it backwards that's awesome that's awesome if you want to there's minus one right so you can go you know uh let's see i think there's that count yeah anyway so you can you minus one goes to the end of the array goes to the end of the array so we're going to be working a lot with these arrays and the looping functions that'll uh the looping constructs that'll let us work with them let's take a look at these parentheses here real quick and i'm just going to give you a couple of examples here i'm just going to make a couple of text files real quick i'm going to make a text file and i'm going to make a csv so you can see how i'm going to use these parentheses and all i'm doing is putting in some computer names and sending them out file the first one will just make a text file of two computer names for me and now take a look at the next one now you guys know that csvs they have a column header separated by commas so i want to put in computer name comma ip address so i'm going to do that and that'll create the file computers.csv but i want to add some things to it so i've got dc and its ip address and i'll just append it and i'm going to add client to it now here's the cool part yes i could call these up in notepad and you could see them real quick you know so if i oops not that's not what i wanted to do um so i could go out here and and show you that they exist uh dir so you see computers text and computer csv and i can notepad on the computer's text oops wrong one there we go and you can see there it is nice little text file and powershell we have an import csv and i'm going to import that uh csv so you can see that it's well formed it's the way it's supposed to be so notice i get my columns on my headers so whatever file type you have you can now use that with your commandlets and look at the examples here that i have for you on the using it with the command let's get service computer name see the parentheses the parentheses mean do what's ever inside there first what's going to happen is is that this result and i'm moving it again stop doing that this result and i'm moving in again so i'll stop doing it deja vu all over again this result will be added as the arguments on the parameter computer name so if i hit f8 here it's going to read the text file get content will open up and bring out those those values and notice it just ran get service and it ran it twice on both those computers and the same thing if you wanted to import it from a csv so here's an example of actually clearer is just give like bits get service bits and these yeah you know what it is a matter of fact what a great idea so name bits and boop see i got two of them and here's an example of working with the csv now now guys this was from our first lesson when we were talking about um understanding the pipeline it was we had a four-step process and this was part of step number uh three and part of step four actually when i imported 3.7 so it's like 3.7 yeah so when i import this csv and this confuses a lot of folks yes because it has two p's it's confusing everyone well right there that's not even gonna look like it works so i import this this isn't exactly what you think this is well it is exactly what you think it is i'm going to pipe the two one of those two one of those two kind of sort of maybe i'm going to pipe it to get member and i want you to see that this is actually an object this is coming across as an object well this parameter has an argument called string it wants a string it doesn't want this object so notice i can't just import csv like i did with the text file i have to extract out the column or property that i want which if you look down here lookie i've got i've got computer name is the column that i want i have to extract the values out of that and make sure that they're strings so watch i'm going to do this again and we'll do select object and if i just did dash property and said computer name it almost looks like it worked except it doesn't except it doesn't let me pipe it to get member again this is still an object i need to turn it into a string i need to extract extract extract no it's expand property and when i pipe it to gm you'll see that now i have a string so and this is how it normally would look that's the data that i'm going to put in there so notice that this it's a little bit more complicated but when i run it oh this should do dash name bits yeah bits just to make it easy when i run it lo and behold it works this is the hard part to it and so go back and check that our first video series on working with that but it's important concept for those parentheses and that's what you had to do if you had powershell version two and that's what you had to do if you had powershell version two but but go for it well no it's next doesn't it i don't have this i don't have it show them how to do a powershell version three how do i do it what do you want me to show them in powershell version three dude dude oh well just dot dot computer name parentheses dot computer name oh so yeah um up arrow up arrow yeah and just don't know no which one oh because you did f8 oh because they did f8 oh this guy yeah yeah so yeah so do the parentheses import csv dot computer name the dot operator this is like one of the most important man did you have coffee this morning yeah yeah so no not no import csv the computer name oh man oh and the file name okay so um oh it's c colon computer okay uh csv yeah and paren dot computer name no no end end parentheses end parentheses oh oh i'm an idiot i see what you're doing yeah yeah yeah yeah this is another way that you can computer name yeah yeah yeah so can we get rid of this yes because we don't need this yes and sure you see look at what's happening we're going to import that csv and then grab the computer name from it so i think you need a dollar sign in front of this as a i think i think it might not be able to instance just find out yeah this is how you run powershell that's perfect it works perfect i learned something that's the best part when when when the inventor is sitting here learning something that is that is constantly i told you flawed human great product i can't remember any of this so guys this might be a little bit easier to remember um than doing the select however i want to point out that that select and that expand property you see that a lot on the internet so keep them both in mind as you're working with it and as a last example i just wanted to show you that so that you had a copy of it a lot of people asked about this what if i wanted to grab a certain computers from active directory well if you have the active directory module here's where i'm going to use invoke command so i'm going to use powershell remoting and inside of the parentheses i'm going to do get ad computer and i'm going to use the filter name light that has a c in it so that'll get me my dc and my client computer and i did the expand property again and this time for the the script block because we're using powershell remoting i just did get service name bits and let's see how this guy works as we'll run him right at least i think the computers are up oh there we go we had to load the module and lo and behold it worked just fine so we should explain that why did that take so long oh that's a great it actually a lot of you guys that saw the other show should know the answer to this why did that take so long you may have even noticed it on my screen one of the coolest features in powershell v3 in my opinion notice i didn't have to do any kind of import of an active directory module i just had the command and powershell dynamically imported it for me so the first time it took a few seconds and you probably saw the screen flash but every time i use it now it's already there no i just think that we should call that virtual modules virtual modules right because you have virtual memory virtual memory you get it looks like you have the memory and when you use it page faults it in this is like a virtual module by making this up right now yeah i've never had this thought in my life so don't write this down that they're called virtual modules yet because that's the idea that's a great call like nobody called that it is when you use it it's not there and then the system brings it in and use it just like virtual memory virtual models go change the documentation yeah we'll go back and change it but the moral of that story also that's important is that when you're scripting if you're version three and up you don't have to worry about importing all these modules of all these commandments you just use what you need in the script when you need it and it'll work because virtual modules because okay twitter right now is alive with the new definition of virtual modules i like that i like that too i like that too all right folks let's go on to a couple of other magical tools that you're going to need first of all my fifth example the if statement if it's kind of iffy it's kind of iffy isn't it if this equals that notice we're using comparison operators that we talked about in the first version so i'm comparing in this case two variables and this is just kind of pseudo code if this equals that then do some stuff in other words if the condition is true i want you to do some stuff we use this all the time so we can check for files we can check for a path we can check you'll see us do it with error handling later on also the if construct has some additional things to it you can also do else so take a look if this and that do some commands however if that's not the case else do these so you got if and you've got else if this is true do this if not else do that but we also have else f now else if confuses the begins well because it's if this do these else if this other condition do this else if some other condition i i get i start getting confused with the the the conditions yeah here's the way to think about it if imagine you wanted to do this some other way what you'd end up having to do is in the else you have to move everything in a block and then else and so it just like goes out there and so elsif just says yeah we're not going to do that we're not going to make it complicated now guys i do want to show you that and i have an example down here for you to check out if you want to check out the if and else you're going to see us use it a ton today so you can run that example but i want you to kind of notice since i'm one of these guys that get confused by this there is another way of doing an if statement now it's okay to do multiple apps if this do this if this then do that if the else if starts to kind of confuse you there's also something called a switch and and actually if i have to do multiple ifs i kind of am a switch kind of guy yeah i'm a happy switch kind of guy so take a look at this calling dr freud hey you know whatever it takes you know 110 220. so um here's the uh the uh uh keyword for it is called switch and i'm going to switch on a variable so i'm going to check this variable this is and notice i've got multiple sets here that i can do this is a little bit easier for a lot of folks mentally when they they're thinking about what i'm checking so in this case i'm checking status and if the status is okay that's value zero and value one would be if there's an error so let's try this let me uh do uh i guess it would be ridiculous we need status equals um three just so we can see the switch run so i've got multiple conditions in here but it's a little bit easier to read and so let me run this [Music] and nothing happened wait a minute nothing happened why did nothing oh ctrl z clear that out yeah i gotta clear this out because you said status text and you didn't show status text so set down do dollar sign to status text at the bottom ah i am such a bad person you know you should uh dial into the advanced no no no status text status text text under bar text hmm okay great i've screwed up my demo now yeah so status is three so we should get stat oh status underscore text if you replay the tape from about 30 seconds ago i know it's it's sorry guys so i'm going to actually display this so you can see what's going to happen status 3 this is the one that's going to get selected and so let's see what happens here yep overheated overheated now there's two notes on this if you're just getting started with scripting there isn't about underscore to help explain switch and i do have to say for people that are just getting started sometimes switch is more confusing than the if statement so while i've given you example for switch sometimes just doing a simple if as a comparison and then doing something is going to be the easiest way to go and you're going to see us use lots and lots of examples of this but we can show them an alternative way to do it show them the status the switch switch okay so you notice here you see dollar sign status under bar text equals this dollar sign status i hate stuff like that visually i'm a person that likes things really clean nice and pithy pithy because too many characters confuse me so here's the way i do this because not many people recognize this but many of the statements in powershell are assignable which is to say now show my screen so here i say dollar sign status equals three yeah but then check this out what i did is i changed it i said dollar sign status text equals switch so do you know what so you can take this whole thing oh yeah unless that much cleaner this outputs something and what you can do is you can take the output and you can assign it to a variable so notice how clean this is it just has the value just has the values so then i like that a lot overheated yeah so then your scripts can be cleaner in my view no and actually that's why we can do the same thing with ifs with the if statements yeah i think to delete it so um let's see do you have something here anyway so here what you could have said is you know um dollar sign x equals oh and then get the result out of x yeah so here uh so doesn't x equals uh if dollar sign false one else two yep two so here's the deal remember i uh i did that script right before and screwed it up well it turned out weird like what happened there right and so if you go to my monitor you'll see this is what had happened right so i tried to run this and said can't be validated but my earlier demo i had added a validation requirement remember i said dollar sign x and i could say it could only be a b or c so that worked but if i did this it didn't work so that was going what's going on there so basically here dollar sign y plus sign y execute those and it works okay so that's that's the case of the unexplained what the heck yeah actually got explained and somebody actually in the chat rooms also remembered it was you because you had done the validate set so good job pretty smart group guys i want to catch you up in the slides because we're going to do loops and then we're going to start scripting some cool stuff so just if you take a look at mine we've done variables quotation marks uh object members and variables but you have all the scripts for parentheses so you have all of that and we've done if statements and switches now i want to do something more than once therefore i want to loop and i'm a loopy kind of guy so let's do a little loopy loopy loopiness first of all some of the basic loops jeffrey i'm a do-loop kind of guy yeah oh good i don't use it that often but guys take a look we have a keyword called do and what's ever inside of the brackets we're gonna do while some condition is met now i want you to notice that i'm starting up here before the do loop dollar sign i equals one and then i'm going to do stuff and then as long as dollar sign i is less than five so this will loop through un until um dollar sign is less than or equal to five well now here's the thing about some of these uh loops you need to have a way of incrementing while you're running or if you don't have a way of incrementing what'll happen is the loop will run forever in this case as a matter of fact uh we just you know you can just see by a photo show you want to do that yeah see it's it's actually continuously running i'm going to go up here and this is a great time to show you the stop button because what's happening is it's dollar sign i is set to one and it's it's always going to be less than five so it just keeps doing it over and over again this is what the incrementer is for and let me just show you what it means dollar sign i equals dollar sign i plus one in other words i'm going to increment i by one every time that it loops so that eventually it falls out of condition now i do want to show you the shortened version of this right because nobody's going to type dollar sign i equals dollar sign i plus one lots of people do well yeah i guess lots of people do yeah so the shorter version of this is dollar sign i plus plus and you're going to see that a lot and that means increment by one now yes you can increment by other values as well but watch what happens now with the loop and let me clear the screen down here so you can see the do loop runs yeah you can't do that we mean i can't do that i just did that no the plus plus two no no what was the uh plus equals two plus equals two thank you plus equals two thank you for teaching me didn't know did you know you have coffee there i have coffee [Laughter] so besides the do loop you also have a while loop and this is a similar uh uh setup so dollar sign i equals five while this condition is is is is met and notice in this case i decremented instead of incremented so the do and the while loops are are good loops that you can use and there's about underscore help files for all of these however the super cool loops that we use a lot are things like for each i mean for each is is the deal so here's the thing i want you to think about this you're getting a list of something in this case i'm going to be getting a list of services every computer you don't know exactly how many services are on there the problem with the dual do in the while loop is you kind of have to know how many times you want to do this well if i get service i don't know if there's 200 services if there's 50 services so what i want is i want a loop that will go through each one of those services and then when it runs out the loop knows to stop and that's one of the things that for each does and so is a great example we're going to use for each a lot today so you might want to get service b star just so you have a smaller set oh yeah yeah yeah that's a good idea name b star oops and so we'll just do this for the services beginning with b i'll run that and now i want to go through that and display their name so take a look at the the structure for this for each now this is the tricky part dollar sign service in and i like to say this in english in the collection of services in this case where you're storing your collection of objects we want just one thing at a time out of it and this allows us to reference it so dollar sign service or some people like to use the word dollar sign item in whatever the collection is and that'll give us a way to iterate through it so dollar sign service notice it's the one object at a time dot display name and i'll run the loop and you'll see i think there we go got a short little list of so by the way i find that uh that's that works fine but i often find a little bit confusing dollar sign service versus services so often in these sort of things i'll just say dollar sign i use the first letter of the thing i'll say dollar sign s in dollar sign services so in other words you shorten it to this yeah which does make it it makes a lot of sense to have it and then you know you see those s services and it just reads a little bit reads a lot better what a great point and we're definitely about readability as we start to script so i really like that that's a great tip when it gets more complex and you have a lot of things going on it doesn't work as well but for short little for short things it works fine that's awesome we also have a for loop and a for loop is a nice way a lot of times used to replace a do or while loop and it gives us a nice structure so for a dollar sign i equals zero notice different from when we were doing the do while loops i had to set what i was here and then i put an increment or a decrementer and here's my condition on a for loop it's all in one place dollar sign i equals zero here's the condition i is less than five and here's the incrementer so it's easier to troubleshoot this kind of loop than a do or a while loop and this will loop through and jeffrey was showing us this earlier the range operator can be used to do some looping across the pipeline and this is it's kind of neat um the range operator he's already done a great demonstration of it so you know i'm not going to be able to do one better but here's a great thing that i would like that for one through five i'm gonna pipe this two for each object and what i'd like to happen is to start calculator i know this is a stupid example i'm just saying but this is this is good this shows you the range operation it makes a very good point which is to say the pipeline you pipe objects to a thing the thing doesn't have to do anything with the objects yeah i mean often that's the thing that makes most sense but you don't have to so here you're just saying start calculator it had nothing to do with the integers had nothing to do with the integers and this is a loop so i'll run it and i'll get yeah a whole bunch of calculators out there so why don't you why don't we kill the um calcs there they go and so those are our loops yeah folks using this information let's go back to the slides here so that we can be all caught up using this information of our looping constructs do and while which are a little bit more complicated but there are more refined loops for each we're going to use a lot you've got four loop and you've got the range uh incrementer which uh jeffrey's already done a great demonstration on we are now in position to start to really make something now i i want to point out that this the the linguistic stuff that we've been doing is necessary and it's something that you have to practice with and you have to play with but now we're going to step kind of away from that and now we're going to start making reusable tools and we're going to be using these components inside and everything that we've talked about this morning whether it was as a review or you know the stuff about variables all of that it's you're going to see it all tied together this is the exciting part where we make stuff and he's looking at me like well then get on with the exciting part i just want to point out remind you that uh we have great uh uh people in the chat room that can answer any of your questions so if you have any questions like what did they say or what did that mean just go to the chat room and answer them there or ask them there and you'll get some answers or you can [Music] all right let's get started into our third module we're going to start going uh making simple scripts and functions here this is going to be it's totally wickedly cool because now we're going to take us take a command take any command you want the ones that i have in the demos or any command that you want and we're going to start to turn it into a script for automation we're going to start to use variables we'll parameterize it this will be making a parameterized script and then we're going to keep the parameters really simple right now and then we're going to start to turn it into a function and run it as a function and test the function this is going to become more and more like a tool for us so let's get started here first of all turning commands into scripts now what i'm going to do is two things and to cut down a little bit on the typing first of all guys this is a module three so no i don't want to save any of that so i'm gonna go out and launch my startup here but here's here's a great way to get started with this i'm going to just start with a blank slate first of all so you get the concept this concept is really simple get it to work at console and then paste it in and there's the start to your script so if i do something like get wmi object or or you can get a sim instance if you're on version 8 or server 2012 and we'll do class win32 underscore bios and computer name dc comma client hit enter yay it works so when something works if you know you're going to need it and to do it again then why not just make a script out of it now you know that right now all i have to do is save this and i can run this i can just go file save save as and i'll go out here into my scripts folder and save this and do uh we'll call it t t i like that t that's actually a great depth t and if i want to run this there's two ways i can do this i i can do it from within here but a lot of times i'd like to run it from the console because as we start making tools that's where the tools will be run from so i like to check them there so i'm gonna go into my scripts folder we've made a script i know i know you're not that impressed but i want you to see what the issue is right we've made a script and this is cool this is this is great first started the journey we've started the journey and this is neat but what if i need to do different computers and what if i need to do and what if you wanted this you're not going to want to look at t because i already have one yeah well first of all first of all you don't want to name t and you're not going to want it using the computer's dc and client be nice if i made this a little bit nicer for you since you're a nice guy and so let's start making it a little bit nicer so if you take a look at my first example here i'm going to use a whatever example you want to use i'm going to use a win32 logical disk and if you were joining us for the first one this one down here looks scary but what i'm doing with win32 logicaldisc is i'm just grabbing the c drive from a computer called localhost and i'm selecting certain properties that i want to see the scary part for a lot of people and you need to practice this to get used to it but definitely get used to it i'm doing my own custom column here of size and gigabytes in other words there's a property called size and free space and they're in bytes and i don't like that long looking number so i've decided to improve it and just as an example i'll run it so you can see it and this is what an example that we ran in the last one i need to clear this oops wait a minute sorry i really messed that one up oh stop it there we go i need to go down here and clear this because that's still on the screen so you can see what the results are so i'm going to start off with a slightly more complicated command and as you can see it comes up and it gives the computers and for some reason i don't have my free space showing up but oh it's because i forgot a comma so let me try this there we go so you can see that for client in this case there's a size and it's in gigabytes so these are the calculated properties or custom columns now when you have your own command you might be looking at it and going okay well there's a few things in here that i know somebody's going to want to change when i when i give this to you you're going to want to change the computer name you might even want to change you know i've got it set hard coded for drive c and stuff like that you know you might want to check it for something else so the first thing you want to do is start to identify things that you would like to replace with a variable to make your life easier this is where we're going to start making our variables and in an example two here's a great here's well an example here's where i've done device id and i've said you know what i'm thinking that maybe somebody else might want to specify a different drive letter than c maybe they want to do a different computer name so i've created these variables now all i've done so far is so that i could test i went up here and i filled those variables out i want you to notice that i did put in the type here in front of it client and c and so i'm going to run this i'm going to click the green arrow to run it and notice the information that i'm getting from this so it's c it's on the clients and i'm getting so everything's working perfectly hey now you guys and i mean you smart guys if this was another language the concept of parameterizing my script becomes a challenge and i have to start dealing with arguments and dealing with stuff that is sometimes mentally challenging you guys made it really easy for us to make parameters i've got a simple script where i've identified some variables and i want to make it i want to turn them into parameters yes so how simple did you make it it's totally simple so basically there's a couple ways to do it if you're starting from scratch he's going to show you parameters but if you're an old unix guy and you're used to dollar sign args we support dollar sign args if you don't know what i'm talking about pay no attention to me listen to him but if you're an old unix guy and you're familiar with dollar sign args dollar sign args of zero or dollar cent args of one that works perfectly yeah it does but we're not going to show you that because we're going to show you the way you should do it well and i do want to point out you will see dollar sign args used in scripts that you'll see on the internet and it is supported and it does work and for you unix people here's what we're going to do is they've done this amazing job of i'm going to put in a keyword called param open parentheses and then a closed parenthesis now there's a couple of style notes i want to point out here this is going to start to be hard to read so what we want to do is we want to indent this so i'm going to indent this so jeffrey loves me and it makes it easier to read now i also want you to notice i've got a little red squiggly up here because what this wants is comma separated so i need to put a comma in here between my two variables so so far i've done one thing param put a comma in there i'm going to run this yes go ahead and save it and i get the same result because these are acting as defaults but watch this i'm going to run this as if i was running the script from the console and this is called number two magic ready for magic magic magic dash look at that parameter yeah parameter parameter now if you saw the last episode you you've seen us do this but i can't get over how cool and how easy this is you can add any variable you want so look at what you've done as a solution what you want to change to being variable so somebody else can use them as parameters and then all you have to do is put them up and in the param block you've just started you down the route of making a parameterized script but this is starting to become a tool i mean i could give this to you except that it oh it's not called t um i could give this to you and you could put in your own computer so let's put in computer s1 yep and there we go we got it from computer s1 so we're down the road of using parameters now parameters are actually let me go to demo number three here see this thing that i've got parameters are working but what is the point to this thing called commandlet binding why do you always see people put that in yes so this is that next click in formalization this is where you really transform a function into a full commandlet now full commandlet is the real deal so you'll see that uh well when you when you show tab completion or intellisense you're going to see a ton of extra variables that aren't defined here this when you put this camalet binding you're telling the powershell engine please do a whole bunch of work for me right and also it it's making sure that um it's checking that what i'm putting in are actual arguments for the parameters and not letting me go crazy and add in all kinds of stuff so this is actually something that's very important for us to add now also as we start adding attributes for the parameters the attributes don't work unless you have that in right i mean mandatory does but there's a lot of the attributes that we have to have in so command binding does that work you think this don't work yeah yeah i thought there was ones that didn't work oh i could be wrong no i could be wrong it's one of those two things so here's the thing well let's see i'll bet you it's me that's that's the lesson here the lesson is you don't have to know all this stuff just try it if it works then it's supported if it doesn't work well maybe you're doing it wrong but chances are it's not chances are it's not so we've got commandlet binding in here param that gets our parameters set up and let me uh go ahead and catch us up in the slides so that everybody knows where we're we're actually going to be at so we've chosen our variables that we want to parameterize we put them in there we added param and now we've added commandlet binding so far so good now what we're going to do is now that we've parameterized the script is let's talk about the difference between a parameter script and a function though because all we've done right now is i've got this script but i'm i'm still oops and i keep doing that i need to stop doing that cls when i'm running this i'm still having to type it in like this yes and i'm still you know this is cool this is cool but wouldn't it be nice if it looked oops wait a minute that was the wrong one would it be nice if it looked more like um you know it looked more like a command line yes it seemed to feel more like a command line yes and what if you wanted to have more than one of them in a file if you wanted to have more than one of them in a file so i'm going to go to disk in because otherwise you can get a lot of files a lot a lot a lot of files a lot of them and so very simply just with this one we can go keyword is function and now we could call this anything right we could just call it foo yeah we and we could go like this put in an open squiggly or we could oh let's do let's call it t there's no alias for t right no i would have known that one yeah okay good and down here i notice i'm going to go to the end of my code and we'll go here and we'll close that so i've got a function called t and take this and right now we'll just leave it and i'm going to go ahead and run that yes stop asking me just do it so it ran it so i should be able to come down here and go tee see how easy that was ah watch watch watch if you're testing when you start using functions and you're gonna test a lot of times you're gonna go to the console and you're going to go well gee whiz oh yes i'm going to run my script oops cd scripts um and i forgot where i put it uh there we go um and it's called i'm gonna run my script notice this is the the three params ps1 and i'm going to say t oh now you explained this to me the last time but you know i'm a little dense yes now i think of it as this way okay thanks i think of it as this way i think of it as i ran my script the function got loaded but when the script ended everything dies variables everything gets cleaned up clean up exactly we clean which is a good thing yeah but how do i test well yeah yeah so basically this is what we call a stack so basically stack's an important concept it says hey here's where i find my stuff and so when you execute something new what happens is we go and create a new stack and anything it creates is done in there and then when it exits we pop that stack now the interesting thing is that when you create the stack and you try and access something that's not there we first look for it there and if it's not there we then would look up the stack and up the stacking up the stack that's called dynamic variables okay dynamic scoping of variables now again so the problem you had here was you had a stack we said i want to execute something you created a new stack you define something right but then when you exited it went away it went away so it turns out there's a way to execute something but not create a new stack and it's called dotting and this is going to be very important so i want you to start dot and i want to i want you guys to see this because when you're testing a script that has a function in it we haven't made a module yet we're not going to till the end of the day because we've got cool stuff for that but when you're testing you need to make sure you dot then dot and run your script and this will keep it loaded now watch t will now right so it executed the code in the current scope and then when you exit we don't throw it away and one of the other the downside of that is that you can contaminate your environments by the way this is how profiles work somebody in the chat room asked about profiles i don't think we're planning to talk about profiles no but we could always so okay so basically profiles are scripts that get run at the beginning of every session it's dollar sign profile and there's four of them for each host and whether it's for you or all users and this is code that we dot source to set up your environment so this is where you set up your favorites and and functions that you're going to use all the time you put them in this one script and then at the beginning of every session we dot source these files these profiles now here's where people will get confused you're testing it and you get used to doing dot sourcing or you might be testing in the isc and i want you to notice i didn't have to do that i didn't have to do any of that i just ran it and it just worked so the ise is great because you don't have to do the dot sourcing but then when you go out to actually start testing you'll forget about it so it's you got to keep it straight inside the ise we're good but then when you go out to run the script and we'll do it enough you have to dot source it until we make a module which will be awesome or more commonly what will happen is you'll dot source it and you'll run it and then you'll go and you'll change a bunch of things and you'll save it and you run it again and you don't see the your your changes the answer is because you already got sourced it so you're running the old version so every time you change it you're going to have to dot source it again and then replace and so guys what i'd like to do is i'm going to show you i've got something here called disk info that i've given you which is notice i call this t that's not very commandlet like so on disk info i wanted to make it a little bit more command look like so notice i just gave it you know get dash disk info um which now it has a proper verb so we know we're going to get some information and we know that the noun is going to be disk info so that was easy now you still see that i have parameters notice that i've i've added a one in here and this was because we showed it to you in the last one this is a parameter attribute we're going to talk more about other attributes but mandatory means you're required to fill this in and so i've got a computer name and a drive and i have basically the same line but what i've done is i've used my nicer one that that formats see look at those ugly it's just big numbers like that scare me so this will make it into gigabytes which will be much easier to read and that's all i've done so i i'm going to run this and now it's going to start to look like and i'll clear the i stop doing that because then i break things i'm going to go over here and do get disk oh see see and computer name's now mandatory so i'll put in dc and then i get all of the information back again don't forget if you're going to test this from the console you got to dot source it and now you've got the start of what looks like a commandlet in fact it looks so much like a commandlet at this point let me go over here and just run it i'm going to call up disk info ps1 get disk in uh disk info computer name dc and you notice anything that i put in there i have so i have other commandlet binding has now given me verbose and these things that we're gonna be working with today do output variable just so you can oh do out variable and dollar sign var no oh bar bar thanks and now that output is in a variable so our variable is actually really cool to use with commandlets and so with commandlet binding we now get all of those extra tools that we you know wow i didn't pay for it i didn't i didn't pay for this i didn't work for this exactly powershell does it for you oh in the chat room somebody asked okay so now that you've got sources what if you want to get rid of it my friends that's the thing about dot sourcing you can't actually there's some way you can go clean it up but maybe like i can show you how to go into functions and clean it up in fact i will because it's so cool but but this isn't really the the correct solution so here let's let's do it where is it okay so i'm gonna dot source this okay so okay so i've just dot sourced that now now there's something if you know about the drives get ps drive okay ps drive shows you all the drives on the system now these drives there's the c drive and the d drive the drives you're familiar with i don't think we talked about this earlier but there's other drives in powershell so we have the alias drive the certificate drive uh hkl hkcu and hklm the registry okay so there's a rich set of stuff go have fun explore but in particular there's something called function so now i can do a dir function do the drive and you'll see get disk info yeah so what i can do is i can say you know basically remove item function get disk info and and i get rid of it well get disk info see it's gone now the downside is well did i get rid of everything and the answer is maybe maybe not so this is the downside of dot sourcing scripts and that's why we invented modules which are coming up later yeah absolutely i also want to show you it to also answer that question if you're testing in the console and you want it clean of all variables and you want it clean of all of the script functions that you've been building just close it and then open up a new one or or uh control t you know what that no it's ctrl t you didn't do the control t no it's controlled okay so here we go you have all this stuff look you remember how i had that dollar sign x equals and and i couldn't oh i guess i fixed that anyway i did anyway so uh you have a mess here control t brings you up a second run space oh so yeah again i've got a whole other environment so you know dollar sign so dollar sign x here and i come to here del sign x it says what are you talking about don't have a dollar sign yeah so it's a completely different environment using the same gui now a couple of style features for you notice the indentations that i've done here this actually becomes best practice and very important it helps you read and therefore troubleshoot your scripts a lot better it also makes it and you know there's a lot of conversation about this the maintainability of your script becomes very important because you might not be the only one that needs to work on this so you want to make it easy to read so notice how i've indented things here we're going to be practicing more with indentation just to make it you know look pretty and make it easier to read there's more though that i could do with this script so i've got one script with one function in it but here's the cool part there's one tool maybe i want another tool well i could put in another function and we'll call it get cool or something like that and another function and get more cool and as a matter of fact i gave you an example i think i called it jump tools over here where i have just that the initial function and then oh lookie someone i called him really i called him get jason fun i what was i thinking when i did but here's the moral of the story so first of all i want you to notice the collapsy capabilities here to help you see things maybe i don't want to see the param block so i can just collapse that and i can reopen it when i want to look at it i don't want to look at this function right now i want to see the other functions so i can collapse that see how it's easy for me to take a look at it now in this case i've got a script that's got three functions in it well let's run it and let's see here really did i call it get jason fun i certainly did and voila so you're now starting to create a script of parameterized functions that are kind of becoming a tool set for you a toolbox a very nice little toolbox so can i interrupt a second oh absolutely so earlier jason showed you a script and if you show my machine i'll show you that okay and notice it had computer name and it defaulted to the value client it turned out client on jason's machine he has a virtual machine whose name is his client so you at home none of that worked for you oh yeah yeah you gotta show you in general you type local host so that was what was going on there was that a question that was it was going to it really would it intuited that it was happening out there so yeah guys go ahead and use localhost um instead of my particular machine names yeah if you see things like dc1 dc client s1 s2 those are all his vms yeah so you'll do something different for your machine insert your own computers there or localhost if you just got one box notice the example for disk info that i have up right now i've removed the default values and i made computer name mandatory so you have to type in whatever your computer is anyways of course you can type in localhost and in the jump tools yeah yeah yeah yeah let's see what i did in here i think i get my param block i think yep i got rid of it and had it mandatory in here so hopefully you won't run into too many issues with that well jeffrey this is a nice parameterized script but you know there's this isn't really an advanced function yet so so here's what we're going to do let me catch up in slides because we're going to go to a new module and we're going to start to turn this into something called an advanced function ooh the name is scary so functions creating parameters got that we've shown you how to run them and to work with them make sure you're putting in your questions [Music] now on to module 4 advanced functions so before our we're calling it a meal break right we're going to get started with advanced functions before our meal break and here's what we're going to do we're going to talk about the purpose to advanced functions in other words i'm going to ask jeffrey questions about it show you an advanced function template and the parts of an advanced function it's not much harder than what we've been working with we'll show you how to create and test parameters some more parameters and then where your code should go and then something really cool getting stuff out to the pipeline in other words we're going to start turning this into a pretty doggone cool tool so first of all the purpose to advanced functions now we've already talked about this a little bit but this is this is me being able to make something that looks like commandlets yes and that's important for me to make my own little toolboxes and to make is we're going to bundle them into modules later on so when you originally wrote the manifesto and i got to get me one of those i got to get me a manifesto um the was was this already kind of in your mind at the time that me as an admin i'm going to be able to create these they look and feel just like commandlets yeah we were always super super clear about this distinction between the experience and the implementation okay and you see that everywhere right and remember i showed you in the previous session variables is a syntax but in fact you could store variables in the file system like crazy stuff like that yeah and so we knew that we were going to have command lets which was a high-level task-oriented experience for the user but then how that got implemented we knew that there were going to be lots of different ways to implement it and that's what you've seen right so in version one the only way you could implement it was in.net we had functions but they weren't really commandlets in powershell version two we made commandlets available through uh powershell and this unleashed the the power of the masses and then in version three you saw that you can implement it in net powershell or windows workflow that's right windows workflow technology can be commandlets or you can do uh wmi and so that's native code well and one of the cool things about this is if you take a look at my screen here they're not the same as compiled commands and here's what's not the same about them i didn't have to learn c sharp and use visual studio that's what's not the same about them in other words if you wanted to make commandlets before you had to you had to be more of a dev and and there's nothing wrong with that but you know i'm an i.t guy i've gotten i'm learning a lot as i'm going so this makes it a lot easier they're not the same but they are just perfect for us as it pros because we're making great looking commands and we want to respect the the best practices for that i also want you to notice on my screen the get help and i i could actually run this but there's a lot of help of everything that we're going to be doing today and i'll just do get help for you guys on function there's a lot of help files out here that walk you through everything that we're going to be talking about so everything that we're going to be doing with the parameters the parameter validation so everything we show you is written out for you so keep that in mind when you're like oh what what what was this or what was that it's all there in the hell now to get started with this we're going to show you a little bit different kind of template for this now i've given you one that i started off really easily and let me open up for uh module four here cd module four and my startup script i want you to take a look at this simple little template that i've made up and jeffrey i'm i'm confused by a few things okay here's function now we're going to talk about comment based help i just have it there right now function verb dash now what i wanted to do is make a template that i could easily use to remind me of the things i needed to put in commandlet binding needs to be there and parameter and so i know that i'm going to want parameter and i'm going to have some variables in here that i'm going to want to use so i just put them into my template but here's some other strange things that are in my template begin process and end we we've never seen this before yes and i don't i mean why is this in my template what is what is begin process and end again this is one of the big distinctions between a function and a commandlet command it's a full command line it deals with the pipeline so honestly the best way to get this in focus is to just experiment as always the best way to get things in powershell and focus is to experiment so let's party with that so here the way i do this is i'll just go and i'll i'll take this and i'll put in some code i'll say begin and then i'll say process oh this is awesome yeah and i'll say end okay so then i'll just execute it and see what happens okay okay so then we say verb verb dash noun begin process end yeah okay well so so yeah so what we've got is these code block sections and apparently the begin ran once process ran once and end ran once yeah am i with you so far pretty exciting yeah i know pretty exciting okay so okay go ahead okay so now let's experiment some more so first we've got two parameters that's too many right so let's simplify let's just do with one my string right just to be simple okay and now what i'm going to do is we're going to say dollar sign my string string by the way see why dollar sign t works yeah a lot less typing okay and let's provide that as a variable verb noun minus my string test okay so it's available everywhere so again still no no no not much of a distinction but now let's do this let's say um uh parameter what is it value from am i doing this right equals dollar sign true okay so now what that says is i'm going to say you know 1.3 pipe two verb dash noun okay and what i said here was you're gonna get into this in detail yes okay but what i said was i wanna get the pipeline this string from the pipeline so that says i'm gonna have values one two and three that then get sent to verb dot noun and they're gonna be assigned to this so now let's see how that works ah oops okay mine is oh sorry i did this wrong it's not parameter oh that's everybody didn't like it oh no no check this out yeah so notice now begin does not where's my string it's not defined right right why and the answer is because my string is now coming from the pipeline coming from the pipeline but now notice here i don't get one process i get three processes why did i get why do i get three of them here because it gets called process that script gets called once for every object in it comes through the the that comes down that pipeline right so every object that comes down the process code block is going to run once for each single object exactly so now check this out now let's not make this a string let's make this an integer now let's just call it x okay and here now what i'm going to do is i'm going to say now what the begin block the begin block is about setting up code necessary to establish uh to do your processing so imagine you say i'm going to want to total all the integers so i'm going to say dollar sign total dollar sign total equals 0. and then what i'm going to do is i'm going to set a set of integers here and so i'm going to say dollar sign total plus equals dollar sign x okay so for every object that comes through and then when i'm done what do i want to do and the answer is here i say total equals dollar sign total okay so again i'm going to pipeline a set of things right one you know set of integers to this this is going to run once so i initialize a total then this is going to get called every single time where i take the value from the pipeline and i assign it here and then i run this code so i increment it and then when i'm done when i'm fully complete i call this okay so let's run it and then 6. cool do that 15. by the way so this begin process end imagine that instead of totaling something i was going to add a set of things to a sql database okay so i got a bunch of objects and i'm going to add them to a sql database in the begin that's where you create your connection to the sql database then the process you add row addrend is where you close you clean up so this is actually kind of structuring our code a little bit for us um the beginning as you said is where we could do connections to sql um we could be calling up uh other.net objects instantiating those that we want to use then process process process right and then we close everything down with the uh uh end it's like you do do hobbies hobbies yeah like you know what well like imagine you know oh well there you go if you were like a tool maker right do something with there what you do you're gonna do a project what you first do is you set up your tool bench okay i got my saw you know you get all your tools there then you do the work then you clean up well that's what begin process end is begin set up your toolbox get everything ready okay then do your work do your work do your work do your work and when you're done you clean things up perfect so let me give you a real life example of why this is so important especially about the process block and you've seen this before i'm going to do get service and what i want to do is pipe to a something else select object which is a command now think about this multiple services are going to go across that pipeline and i just want to see the property name now this is going to create a long list of names but what it's doing for me is select object is supporting in the process it's processing it for me so the process block gets run once for everything that gets piped to it or if nothing gets piped to it it still runs just once so when we start putting in our code this is where we're going to start putting in our code now i want to show you something else i like to start off with this simplified very simple kind of template like to have my own simple template this makes it really easy that when you create a one line or something that you like that is solving a problem for you you can quickly as you'll see me do just throw it in here and move on and you're making tools really fast i do want to show you that the isc has built in these snippets and it's ctrl j and they're right here command advanced function and complete now look at the simplified one it's it's better than mine it's got the begin process and end blocks in it it's got some parameters and look at what they've done we're going to talk more about all these different types you can have here but here's value from pipeline and this is by with something else that we'll talk about by property name and so this template is all set up for you to use so you might want to create your own you might want to start with this one but make sure that you have these code blocks in here begin process and end in the template because we're going to need that also and control j by the end of the day you're going to really like this complete one the complete one has everything and i know some of this stuff doesn't make sense right now it will all make sense by the end of the day but look at all these different parameters different parameter validations that you're going to see us use and stuff like that so this complete one may be the one that at the end of the day you want to start so here's your mission when we get to our meal break here which we're coming up on your mission is to either create your own simple template or you start to get familiar with one of the built-in templates into the isc try to think through it's a snippet snip it i keep calling it template yes snip it yeah snip it have some more coffee i keep calling it a template it's a snippet jeez it even says snippet yeah because when you go to again you want to think words matter that's the whole point you want to think type get and so if you think template you're going to look for template you're not going to find it so think snippet it's a snippet and then when you say oh is there any give me some help about snippets you're going to see import export get our new snippet absolutely so go through a snippet take a look at it because pick which one you want to use for today and make sure you understand what we're doing in here even go ahead and start to try out maybe some of your own code in your own parameterized script during the meal break because we're going to take our parameterize script that we've added a function to and now we're going to start to add the begin process end blocks and more parameter validation in other words we're going to start to step up what our tool can actually be so make sure that you kind of understand the components that we've covered so far when we get back it's going to start getting step your tool game up ice t references step your tool game is that an iced tea reference oh yeah what's the step your tool game up step anyway so but there's two things i want to point out here it great question in the chat room somebody asked hey that process block is that uh parallel because that can run concurrently the answer is no and here's a reason why in.net.net unlike java in java things are thread safe okay in.net the objects themselves are not thread safe and therefore you as the caller have to handle all the concurrency control by the way if none of this makes any sense to you don't bother it doesn't matter so um so you have to handle concurrency control so in the programmer does so in this case you are the programmer so you would have to know when and how to handle the concurrency control where do the locking etc now that's just not at all appropriate for look systems programmers the guys i hire they have a hard time getting this right no way i was going to give that to the i.t the admins of the world and they were going to get that right so the answer is no it isn't um it is not parallel now this then brings up an interesting conversation around jason showed you uh like you could do invoke command minus computer blah or you could say for each dollar c in computer let me let me just show you that to draw the distinction let's see do i have this up here okay so there's two things you can do you could do invoke command minus computer you know i get a content servers dot text and do some function like that or you could do for each i'll assign sn get content servers using those parentheses and grabbing the text file yeah and do something with it there okay so either way will work but there is a huge difference and the first one is far more preferable by the way so this would be invoke computer name invoke invoke command command minus computer name uh dollar sign s okay okay huge difference between these two this says okay i get this content and then i grab the first one and i run this when it's done i get the second one and i run this it is serial okay and by the way so i just said i was telling you about the guy i met this weekend yeah there's a funny story about a guy i met this weekend who was talking about this very thing here's what's going to happen imagine one of these computers is down okay then what's going to happen is you're going to get it you're going to do this and it's going to wait it's going to try it's going to try and try and it's going to wait wait wait wait wait before it eventually fails and then it's going to go to the next one so one way or the other even if you didn't have a failure it's serial and it takes a long time and if you've got a failure in there it's going to take a really long time here when you do it when you do invoke command we now here's a deal we handle the concurrency control okay so when you say minus computer name and you get this big list what we do is we produce three lists the to-do list the in progress list and the done list now say you had a thousand servers we go and we put those thousand servers in the to-do list and then we have this in progress list right and there we have i think it's either 32 or 64. you get to control it if you don't like our number outstanding entries right so what we do is we grab the first 32 we issue the request concurrently to all 32 of them and their wedding now when one finishes we take it out of that list and we put in the done and we grab the next one so this acts as a sliding buffer where we have that many outstanding requests operating in parallel exactly if you wanted to you could do minus throttle limit equals a thousand and do them all at once but this is where you get parallelism super powerful we handle it for you it's safe and if one of those machines likes again i got 32 of them if one of them stalled and it's going to take a really long time before it comes back and says you know i tried it and i tried i tried but it just wouldn't work doesn't matter i got 31 other things that are just going to town crankity crank crank so really that's this this technique is far preferable yes and this is the power of powershell remoting so you've got to make sure you have remoting enabled as we talked about in the last episode because that is incredibly powerful it's time that we start to add in some parameters test some parameters add in our own code into the processing block and see if we can start spitting out objects so let's go here we go boys and girls just to take a look at the slides we just got done with the advanced function template and what we're going to do now is move on to creating and testing your own parameters and so i have a couple of examples that i'd like to work with and we'll dive right in here first of all take a look and this is my example we are still on module four scripts in module four yeah scripts in module four yes thank you module module four four so take a look at my test parameter start i've got a couple parameters we've already talked about this but i want you to notice that i've got a couple of additional things first of all param block got a computer named string notice the brackets for string i've got something called a switch this i'm going to show you how this works there's a lot of times that parameters are just switches they're either on or off and this is one of them you as you can tell shortly we're going to be looking at making error logs so i also have a parameter for an error log and i've got a default value in for the error log what i'd like to do is is show you some code to test your parameters to make sure they're working but let me show you what these look like first i'm going to run this so that it's it's and now it's get comp info so i'm going to do get help and get comp info now what you're going to notice is that the help system is already trying to build or help me build a very nice help file what i'd like you to focus on right now is the syntax and take a look at computer name and see how it's string right there string now i don't know if you joined us for the first one but this is where when we were talking about reading syntax there's a special indicator that lets you know whether you can have multiple objects or not well in this script that i'm working on i don't want to take just one computer name i want multiple computer names now if you join us for the first one you see dash computer name then inside of the show who who is yes um that's where your arguments go and you can see that it says string because i defined it as string but what's missing is is there's an internal set of brackets i call them binkies that means we can take multiple objects so the first thing i want to do is take a look at the modification i'm going to do on my script i'm going to go up here and i'm going to put those in there now when i run this and take a look at the syntax again get help of course i could just hit the up arrow key and we take a look at the syntax you can see now it's properly showing that it can take multiple objects but danger will robinson and we mentioned this in the last one is that yeah that means it can take multiple things now but we now have to handle that in our script so i want to show you a couple of things i want to show a cool way to test for switches and a cool way to handle it whether you're getting the multiple objects and it seems to be working correctly and and jeffrey you can help me out with this because um this is one of these weird things take a look at a test parameter end ps1 guys is the same thing let me go up here and put my look what i'm doing here in the begin block and i put it in here just so you can see a good example of putting something in begin and i want you to take a look at the the the if statement see this switch up here switch error log if error log this means is it on or is it not on so you can see if error log has been selected then right verbose error logging is turned on else right verbose error logging turned off in order to use right verbose which i think is the greatest documentation thing in the world so in a lot of uh older scripting that i used to do you would you would write things out and then you would have to go back and comment everything out or yes or remove stuff but right verbose is fantastic because we can turn that feature itself on or off right and so here's the tricky part about testing this is that i want to test this if statement to see if error log switch is turned on or not but i'm using right verbose which means i also have to make sure i use dash verbose when i try this or i'm not going to see a result so let me do that one first and then we'll come back to computer name so let me run this and show you how this looks so this if statement now will let me test it so get comp info and what i'm testing right now is is computer name isn't mandatory but i'm going to put something in just so that it can run and i want to show you i want to see if error log as a switch parameter is turned on so i'm going to say verbose so that's right for both shown without oh yeah without that's good so here's the deal what happens without doing that it just and as a matter of fact i have to tell you guys that when i was putting this simple example together i sat there and ran it like three times and went well it's why isn't it work and then i had to i had to remember that was the slow one not the sharpest knife in the drawer but actually this happens a lot you have to remember that you know right verbose you need to use dash verbose now watch what happens a now i'm going to do it without the dash error log and i'll leave verbose on and now you can see it tested that it's that that's that switch parameter is not turned on this is a great way to test for it but not only that i can use this code later on which is what i'm going to do when we do set up and use an error log so it's a nice way to test the switch parameter and its actual usable code for later now the tricky part is this for each that we're going to have to do if i want to take multiple computer names i'm going to get multiple computer names in dollar sign computer name yep i'm going to have to iterate through that if i want to do a couple of wmi commands yes i'm going to have to iterate through that and that's yes or no yes or no yes yes yes so here's the thing remember some of the command let's take computer name as a collection and they'll do the fan out and they'll do it for me right yeah and other times you iterate through and see this so you have some choices some people say oh well in powershell there's multiple ways to do things and the answer is yeah that's good which is good but take a look at my screen this is what we're talking about is if i do get service and i'll just do name bits um when i do dash computer names he's gonna take multiple computer names and he's to figure it all out well now that we're the ones writing the tool we have to write that intelligence in and let me show you an example because i'm going to use this code over and over and over again so take a look here for each we can use jeffrey's suggestion for this which is actually pretty cool i called it dollar sign computer in that collection of computer names so if i get multiple computers in there i need to iterate through each one of them and in this test for right verbose i'm just saying just print that individual computer out now if you if you like that style of making this shorter the other reason why you do that yeah is because the previous example you actually weren't following a best practice oh because in in powershell it's best practice to not have any plurals everything's singular so you'll notice it says computer name not computer names right right and the reason for that is for everybody else in the world right which is to say english is particularly irregular when it comes to pluralization process processes to es service services services with one s child children and so you know when do you meet one when do you mean multiple and so we said hey in powershell it's always a singular name um and then you can give it a single thing or multiple things now i will be quick to add yeah somebody can do the gotcha somewhere someplace somebody used to plural and so you know we're not we're not perfect but that's the rule and uh the best practice so and so that's why if you had you know computers you know yeah computers is doesn't work so let's use jeffrey's example here and let's shorten this because i like the readability of it so dollar sign c in computer name and computer name can hold one or more um so let's try this for each and see if it will with verbose print them out so you'll need to fix your verbose to dollar sign c ah thank you um and let's run this so let's do this oh i do that all the time stop doing that stop doing that well now i really screwed it up control z control z control z is my friend here we go so clear the screen and get comp info and computer name let's just do one it's verbose and so computer dc shows up here at the last line so it got one of them yay now let's do it for multiple ones up arrow there we go so i'm going to do dc client if i can spell it correct s1 and s2 that's for both and i forgot the verbose do you believe that yeah verbose and now i'm iterating through each and every one of them reason that this is important is this is a very common thing that you're going to need because you're all most of the time your commandments you're going to want to be able to hit multiple computers with and you're going to want to be able to iterate through this so this is almost like boilerplate code that you're going to see us use in the process block here in a second so that's pretty cool right love it does it work okay yeah okay awesome awesome so here's the thing i've got some testing in here i'm going to kind of collapse this so we don't have to keep staring at it the nice part about using something like right verbose is is now if i don't use dash verbose i don't see all that stuff but i can see it when i want to see it to make sure that things are working correctly i just love that now we want to start to add our code where should we be putting our code let's say i want to do just i just want to get a wmi thing maybe like logical desk or something like that i can put it in begin and that's going to just run once yep as long as you're not getting the output from or getting it you're not operating anything that you're going to pipeline to it and that's the issue so we really want to put it in the process process even if we don't intend to use pipeline input we might change that though right yeah exactly so we want to put in the process block so tell you what guys take a look main code four and i'm going to minimize this and we're under four okay this is the same thing that we just saw only this time take a look in the process block i've put that for each computer in computer name and you know i like jeffrey what you've done so much with just making it a single how pretty that looks that let's just do that so it becomes really clear and then you get more on the screen and i get more on the screen awesome but you do need to see there oh but i do i do actually need to see there so for each individual computer name that's in this collection of computer names what do you want to do and in this case guys right now i just got a couple of wmi commands that are going for my main code i don't even have to have them set to variables right now as a matter of fact let me just take the variables out for a second just so you can see this run what's going to happen is is that for each one of these i'm going to get the computer name and i'm going to run that wmi command for that computer and i'm doing win32 operating system and win32 logical disk this will just give me some computer information like if i wanted to retrieve some inventory information i'm just getting some basics and so right now if i run this and clear the screen and we'll do get comp info and let's just use one computer at the moment you'll see that it runs through gives me some of that basic information from wmi and i can also now let's do it for multiple computers dc comma client now this is becoming kind of a cool tool in the sense that i've got something that looks like a commandlet and i've got uh i'm getting a couple of bits of information i could actually add some more wmi do a complete inventory of as many computers as i want but you know what what i i don't like is um um it's just kind of displaying stuff out here this really isn't it's i don't want all of this first of all okay and second of all you know i have to take a look but these are objects coming out but i'm not in control of anything so i'd like to clean this up a little bit what would you like to do i would like to only talk to you i would like to only see certain properties so uh maybe a hash table a hash table a hash table or a new object yeah i'm thinking that we should probably do both let's do a hash table and then and then do a new object so this is this is hash tables are a critical uh component of powershell if we talked about that in the first session if you didn't spend time on it it's worth spending time on you're gonna see lots and lots and lots of hash tables syntax a little bit you just you got to get used to it but they're they're awesome they're everywhere and so let me give you an example of how we're going to start to control the information that we see take a look at my script and this is uh the main code and where we're starting to put this in so for each and i'm going to leave this right now as computer so i have to change everything but you get the idea you know they pointed out in the q a why don't you show them control h control h control h control h all you guys know all these keyboard shortcuts so select dollar sign computer select dollar sign computer and hit control h control h search and replace my friend search and replace what was can see and you probably don't want to do um yeah oh we'll see what it's doing i'm into it yes you know that you don't want that one so you want to skip that one well wait it didn't change this one just keep going just keep going you did fine next i think oh i probably did um so i just want to match whole world sure maybe and then so that one skip that one so replace replace start over at the beginning oh i'll just hand do it here just change it what the heck what did you do and there's a dollar dollar oh i i screwed oh no i have to do it down here too yeah so we'll just do this make it nice how do you do that how do you do what nothing oh okay so ah so notice what i've done here is i've taken these commands and i've put them to a variable so what i'm what we're going to do is we're going to store that object or multiple objects we're going to store them to a variable and then use the dot notation to reference the properties or the methods that we want like we showed you this morning that you can do with variables so look at what i'm doing down here first of all i'll talk about the syntax in just a second but this is a hash table i'm going to use those variables and then just grab the property information that i want from it now if you're saying how do i know what properties are available well you take that var and you pipe it to get member and you'll get the list of all the properties so i'm just going to grab from the os one win32 operating system i'm going to grab caption and build number and then for disk space i want the free space but i don't want that really long ugly number so i actually did the same calculation that i had done earlier and we also had done in the last show of a custom column where i'm dividing it by one gigabyte and storing it as an integer so that it gets rounded up and it's a nice easy to to work with number now here's the the thing about the syntax is this is a hash table right so i got at sign open squiggly and a closed squiggly and i've broken this out do you like the way this is broken out here that the i've broken this out so that instead of putting it all on one line making it nice and pretty did it i did that way doing it this way means it's easy for you to add additional things notice that i have the name that i want to call the column and then what i want that filled with followed by a semicolon now you could just keep typing like this and putting more and more in but it makes it nicer if you line break it so that you can read it easier and add in additional things the tricky part is is to make sure that when you get to the last one you don't put a semicolon that's the closing squiggly that goes there you know you don't need semicolon hair oh you don't no you need the semicolon if you're putting multiple uh properties on the same line so we need to know take this out yeah i thought it yelled at me when i did that oh maybe i'm wrong let's see well it's not showing up as is a problem yeah so let's see what uh what it no it's it seems to me yeah so the semicolon is a statement terminator and you need that if you're going to have multiple statements on the same line in powershell the carriage return is also a statement terminator so and awesome he's right it worked just fine so thank you bruce payet thank you bruce we really did struggle between this how do we have an interactive syntax and a programmatic syntax together can i tell you how many long long hours we spent trying to get this right so well i think it also it brings up what you also keep saying is that you know powershell you guys are going to see this on the internet with semicolons and obviously without semicolons and so powershell gives you multiple ways to do things which is great and i this makes it even easier to read and deal with because hunting down semicolons is not fun so yep so take a look at i've stored this as a very into a variable here because we're going to use this kind of uh use this later um i want you to see what this is going to do so what i've done is i just right output dollar sign prop i just want you to i just want to display it on the screen so you can see what the data looks like notice i've got computer name os name os build free space but we in the in the chat room people are freaking out because at the top you used a dollar sign capital c and then use dollar sign lowercase c everywhere else is that going to work that's going to work just fine but you know what i should be consistent shouldn't i well no because it allows us to point make a point the point is that in powershell or case insensitive yeah it's case insensitive so you know again powershell is a great language because i'm a deeply flawed human there's no way i'm going to remember was that uppercase lowercase doesn't matter we're case insensitive wherever we can be we are case insensitive and and we talked about this during the the first jump start um case incentive being case insensitive a lot of guys that come from munich that's the first thing that they they you know is this going to matter no it doesn't and they some people get kind of upset by that but as a windows admin if this had been made case sensitive i'd i'd done it once and gone i have no idea right and it would have killed me so yeah this is a great point is that for consistency's sake i always like to try to be consistent so i just changed it to a lowercase c but it's a great point that um case does not matter so look at the order that i have these and i'm going to run this so get comp info and computer name dc and we're just going to dump the output out to the screen now this is starting to look a little bit better a little bit better for the output that i want notice that it's it's free well wait a minute it's free free space computer name os it's wait a minute i did computer name os name os built and now it's doing the output is much nicer but it's not doing it in the order that i want jeffrey it's not doing it in the order that i want yes yeah yeah so here's the deal about hash tables hash tables are guaranteed to give you unpredictable ordering yeah it's just swear to god that's that's the whole point yeah yeah no uh you know we could give you a long blah blah blah explanation that that rule is a better rule in general ordering is going to be pretty random in that that's why in powershell version three we have a couple extra keywords like ordered ordered i'm going to show you ordered and i just wanna just be clear ordered is a little more expensive because we order things and that didn't do that so it's a little more expensive but it's not that terribly expensive and it's quite quite useful because because then it is predictable and i just want to point out that in in most cases you really don't care about the order that um your stuff's being displayed because remember if you were giving this as a tool to someone else the idea is that that admin could then pipe to select and get the information that they wanted and that order then would occur for him but in powershell v3 we have this and let's see if i can do this without screwing it up ordered okay i've screwed it up it's order right on ordered or is it order somebody's going to have to yell at me i think maybe i have it over here did i put it in here no yep it's ordered where with or without ordered so let me go back to here i am typing something horribly in wrong incorrect get dollar sign dollar sign so what's the error say oh let's see here the ordered attribute in hash in literal node what does that mean i need a little bit so show it again the ordered attribute can only can be specified only on a hashed literal node oh okay so so here's what you do anytime you get something like that i'll give you bruce payet's home phone number and i want you to call him any time of the night to ask him about any bad error messages so you'll get that home no i'll put it on the other side so you don't put it on the property i was thinking it may have been me and my little semicolon thing is that i don't know no no no no no no put it on the other side so yes it is not a type yes that's it thank you but again bruce payette it's four two five i'll get that i forget it so it's it's dollar sign pro boy you tell my brains equals and then paste it and then there's where it goes there there there literal hash now here's also a good note in the help file on hash tables this example is in there and they show you the correct place to put it so in my case right now in real life me not knowing where it goes or forgetting where it goes i'd have gone into help and looked it up so this is this is a good deal but i would much rather have bruce payet's home phone number he wouldn't like me very much though you'll be just one of the one of the many one of the many that's awesome so now watch guys when i when i run this i've told it that i want it to be ordered so these guys over here are working hard to order it for me so i'm gonna run it and we'll take a look at it here get comp info your name dc and voila now it's in the order that i specified nice now here's the thing i want my own object yes i'm asking a lot here i'm running a couple lines i'm grabbing some data and i want to take an object that i can then send down the pipeline that people can use select and where and yeah it's worth noting showing the output there oh yeah yeah here's the output okay so notice you get this object but you're not getting a table you're not getting getting this well you're giving a table but you're getting a table of name value pairs well that's what a hash table is now for a lot of reasons you can treat a hash table like an object with we call it a hash table object adapter if you want to know the details and so it works just fine but a lot of times like like if it was a proper object this would be showing you a proper table with each one of those names as columns etc so here's where it really shows up if you had two of those things you'd get a table with eight lines and you couldn't tell where one object ended the others whereas if it was a proper object you'd have two lines you know one two rows one for each object where the properties were the the columns and that's what that's what we want to do we don't want this because right now if you look at my screen i pipe this to get member now we set it for ordered so it's ordered dictionary this is not the objects that we're used to working with in powershell this is not what i want i still want the true object that i can send down the pipeline and work with so let's make our own object and to let you guys know this is in the sample number six but i'm just gonna hand type it in here into this example while i'm here i think i am yeah i told you guys i wasn't going to do all this hand typing and i'm doing it so dollar sign obj is kind of what i use equals and we have this command called new object and i'd like to put in the type of object and this is the interesting thing is you guys gave us uh this this we can make an object a piano subject so a powershell object guys i'm creating an object it's it's empty right now but it's an object it's supposed to have you know methods and properties but i can now assign that to it so one way to do that is oops wrong one i'm going to assign it properties and the properties i'm going to assign it is that hash table of dollar sign prop now i want you to notice something this is a very important we do new object type name ps object and i'm assigning the properties to it and notice write output i'm going to send it back out to the the the out to the screen out to the pipeline that kind of thing so let me run this and take a look at the difference get comp info first of all just look at the results dc client whoops i didn't do something right oh i'm sorry did you guys see what i made my mistake i want to write that object out not this so let's run it again get comp info dc client now that's looking like the output that it should be looking like i've got my column names which are actual properties and you for each computer now i'm getting a row in there i'm getting objects as a matter of fact let's go ahead and pipe it to get member and look there's my properties and it's on this custom ps custom object later on we'll show you how you can even in your why you would want to create your own name for this object but for right now we're going to keep it pretty simple at making objects wow you know what i don't i think i think you know this is like the bulk of if you do this now you've got objects you can do select you can do where you've actually kind of really made a tool at this point yeah exactly so at this point guys you might be saying oh wait wait wait because so here's the thing because you've outputted as an object now all of a sudden you have the you have the you have a proper tool but then it also can fit into the tool chain of other things this if you recall we talked about the last time we had the session we talked about how you always want to write things to output versus the host and the reason why is because here because god kills puppies if you write if you use right host yes he's that way yes and this is now part of the tool chain it does a distinct task in and of itself but it does it in a way that then can combine with the other tools to form higher and higher levels of abstraction and as a great example with that is i'm going to do the get info and this time let's just do select and the property that i want to select is i want property computer name so you see i've now made a useful tool that other admins can use so i can select out just a computer name i can do things like uh if you remember from the last session i can now since it's a real object i can convert it to csv http jason do json hey that's cool you wrote my name into the this is awesome dude i love you convert it to json do you want to talk a minute real quick about json because that's becoming real popular just yeah so we uh finish well json it's a it's a new format it's an object format javascript object notation and it's very very popular and there's a bunch of tools that can consume json and output json so now powershell can take any object wmi ado active directory xml whatever you want and convert it to json or take json and import it to make it a powershell object it's a beautiful thing it's beautiful what's our role as a as a glue language like whatever your problem is our jobs to help you get it solved and so some guy and that's the problem the world the world always has been the world is and the world always will be a messy place right it's a messy place in the past because everybody was doing random stuff so right now we're trying to consolidate things and get everybody to do things one way but then there's a whole bunch of people innovating and doing brand new things so it's always going to be messy and that's why powershell isn't too you know dogmatic around this is the way you will do it no that's nonsense our job is to make you successful and that means helping you deal with the messy world that's why we do xml that's why we do all these these things that other people might say are strange like just pick a just have a point of view and make it sell well we do that we we do have a point of view and we optimize for certain cases but the reality is if if you if you have to go if you have to go par some html file because that's the only way you can do it right well the other option is to fail so of course you're gonna do that exactly exactly and so the beauty of what's happened right now is that we've managed to take some functional code and make our own object that can now be used with anything else that we want to use with down the the pipeline whatever formats that we want if xml csv all of that this is the important piece to making the tool and this format by following this all we're going to do at this point this afternoon is add on to it in other words add additional parameter validation add some error handling to it add some cool tricks and features add some help to this but this is kind of the base of the format of being a tool maker and notice that how really how simple this was we just created some simple parameters we used command binding we made it as a function and then we put our code in the process block this guy right here for the properties that's really important and we created our own object that's really important those pieces right there get you started down a road of making your own tools that solve your problems for you because remember the whole point of powershell is they can't imagine every problem that you would have so you now have the ability to solve it yourself for the it pro this is wickedly cool because it i don't have to be a c-sharp developer to to be able to to to make my own commandlet so at this point jeffrey you know this is this is the core but now it's kind of time that we start adding enhancements like i know one of your fun things is parameter validation so guys let me get you let me get you up where we are in the slides so we're finishing off module four here so just so that everybody's aware and you have all this code so we showed you where to add your code and then writing objects out to the pipeline guys tell you what we're going to take a 10 minute break and then we're going to move on and add more and more to our our tools hey see you in 10. [Music] so that takes us to module five oh so let me get them all up shut down your old ise and bring it up again yeah so everybody if you want you can open up the the demonstration code for this and that's what i'm doing and module five and [Music] voila so this is all very nice i like this you like this yeah this is a good job like you did all the work thank you well no you did all the work i'm just talking about the work you did you and the powershell team you made it very easy for people well thank you thank you very much um thank you thank you very much thank you very much anyways so i'm going to show you this first one just because we've we've already shown this in the the previous jump start and you've seen us use this before but parameters can have attributes so i'm going to let jeffrey talk about that but i want you to see one of the ones that we've been using a lot and this is a parameter attribute called mandatory which makes the parameter well mandatory if you don't fill it in that would explain that explain that if you don't fill it in it's going to prompt you and um so this is actually a a a good example of a parameter attribute but you guys have created a ton of attributes why why create all these attributes in the first place because we're talking about validation all kinds of stuff ooh it's one of my favorite topics so basically it's all around um uh having you allowing you to tell powershell to do all the work for you okay and we were talking about this last evening this whole discussion about imperative code versus declarative code and when do you want one versus the other and the answer is a little bit complicated but in general when you can have declarations declarations tell somebody else what you want and then it's their job to make it so and in general that's very very powerful now you can't do everything that way you know world peace equals true okay yeah i'm a genius i'm a good guy so it doesn't always work but often it does and so that's what we've done and i think the parameter validation is a great example now some of you will look at this and they'll say well look i'm you get this pound sign mandatory equals true i could have done that in code right i could have gone and say you know if it's not here generate an error message but here's the deal don't do that if you go do that code you have to do what you have to write an error message okay if jason does that he's going to do what he's going to write an error message and the thousands of people out there watching this you're all going to write error messages and they're all going to be different okay if you use my parameter my attribute mandatory equals true then somebody doesn't specify a parameter there's going to be exactly one error message and people will look at it they're going to recognize it they don't have to read it they're going to recognize it because they've seen it before and they're going to know exactly what it is and they're going to do it if you if there are thousands of different error messages for the same condition they're gonna have to read that say what is it you know call bruce payet at three o'clock in the morning bruce got another one do you know this one and that's not gonna be good now the other thing is like okay so you say well i'm just i'm just not convinced i'm gonna do it and then i say okay great so what happens when you take your script script and um and it's popular and it's run in french in france are you gonna output it in french yeah what am i gonna do about the language exactly and the answer is no you're going to be an ugly american and say just learn english and of course that's not a good answer that's not a good answer so if you use our attributes and your script runs in other countries we do the uh localization of the air message you know so the people in that country are going to get an error message that they can read you had me at you don't have to type in the code yeah i mean i mean quite honestly it gets down to the reason for the parameter attributes is you could hand code all of these but why when first of all they've already done the heavy lifting the hard work you get features like localization for languages in different countries also one of the primary things is is quite honestly this makes it so much easier than writing out all this code so i i think it's awesome well first of all we've got some mandatory parameter in there and i want to show you guys something just as and it's on the slides that you have so get help about underscore and of course my brain is ah functions and you know what tab completion is such a beautiful thing that so all of this stuff about the different types of attributes that we're going to show you is in advanced parameters now i've got a set list of of some of the uh parameter attributes that i want to show you and i'm jeffrey's probably going to want to show you a couple but the complete list and samples of code are in the help file again the help system for powershell is unbelievable i mean just the most amazing thing did we show them the trick about pipe to clip oh i saw that you know would you show the pipe to clip yeah okay so so let's what was that again help was it about function parameters was it it was pipe to clip i no what was the name of the about function um about underscore functions underscore yeah advanced underscore parameters oh you know what this doesn't have help that's why it's not working you don't have help on yet what i'm doing i'm doing all this demo from like their machine i just walked up and i said oh it's got powershell on it i'll use that and we didn't do an update hello it didn't run up there so i'll do it i'll run update health in the background so this is the old clip exe right is that is it yeah exactly yeah so if you take a look at my screen guys um get help about functions advanced parameters pipe to clip and what were we doing that that actually puts it into the and then say uh you know control n it's a new tab and you just paste it there you go to the top of course anyways it's all there so you can read it yeah exactly very nice this is very cool a nice way to get it there so that you can read it and go through it so all of this and all the code samples are right there so let's go to our first one here mandatory just once again i'm going to run this real quick the rest of the code down here hasn't changed just so you know it's we're not dealing with that anymore right now so we're just adding in features so i'll minimize all that mandatory true gonna run this and you guys will know what happens i'm gonna type in git comp info and if i don't specify it comes up and ask i want you to notice that because we're dealing with multiple values the binky's inside there now he wants to know multiple computer names and i'm going to show you something that happens to a lot of folks the first time they see this they'll put in a computer and go that's the one i want and it's it's asking me again swat oh it keeps asking oh it keeps make it stop here's how you make it stop just strike enter on a blank line or control c or control c so it lets you easily enter in multiple values and also yeah you show them with it get rid of the binkies and show them what that would look like get rid of the binkies and so dink dink right again and let's run it and let me get rid of this line and we'll do get comp info now you don't have the brackets yeah with the element numbers in there so now it only takes one right so so if you have a raise binkies matter and uh we'll just keep asking you more more we'll keep asking so mandatory is pretty straightforward let's see and jeffrey got gave you a quick shot of this earlier pretend for a moment that i want to accept these computer names i don't want to hand type them in to the parameter i want to accept them from the pipeline so example two here are the parameter attributes for pipeline now notice i have multiple attributes here mandatory equals true comma value from pipeline is true and value from pipeline by property name and for the difference between being passed by value and by property name i want to reference you back to the first jumpstart where we went through all of that information and that was some pretty heavy duty stuff yeah but basically what this means is just like jeffrey showed earlier i can now take stuff from the pipeline in fact let's do let's do that let's um i made earlier computers dot uh text so let's do this let's do get content see colon computers dot text just so everybody can see watch ah-ha okay so i've got computers there now look at this pipe get comp info look at that no no really guys stop for a minute and look at what just happened with a simple added attribute my commandlet now supports pipeline input for that parameter that is totally awesome so i can do get content computers i could be doing get a d computer i could be doing from a csv i can hand type them in whatever but everybody says well how do i get something to support pipeline input that you got it and you didn't have to code all this you didn't i remember once and you put the comma you put the code in the process block and you put the code in the process block that's oh very important remember our code's in the process block because that's where it needs to be for it to run if it's going to get it catching each time the pipeline has an object this is i remember hand coding this once i did not understand advanced functions well enough when when v2 first came out and i had i hand coded taking information from the pipeline it was ridiculously stupid and you you've got it such a simple sweet solution by just adding it in right there so guys keep it simple whenever you can keep it simple now there's a couple of other things you can do with parameter i'm focused on computer name because i'm playing with it but i kind of like this this this is kind of nice how about we add a help message for the parameter now this is different than the comment based help that we're going to be showing you later but this is a help message in case you think that somebody might get confused this is a nice attribute to add take a look so in i've got mandatory got my values from pipeline i put in another comma and i put in this one help message equals and i put in a little help message one or more computer names i just point out that's not that helpful yeah it's it's really when you do it you do something helpful hopefully i do it yeah but you don't get the i yeah you're going to see exactly what he means that it's not really that that helpful let me show you how this appears though when you run this um yeah it's rocking yeah this is actually get comp info and i obviously it wants something powershell is asking me for something and pretend for a moment that i don't know what computer name is notice what says above there type and and do you call those bangs yeah i do okay i call them bangs too um do you have a special name for the question mark yes question mark oh okay good i always check with the unix guy to see if there's any kind of you know special stuff going on so watch what i'm going to do is is i'm going to type bank did not exist the last time no it wasn't on the screen at all it's only because you provided help a help comment that now we see that and we say oh well do you want some help here it is and now watch i'm going to strike enter and see it comes up with that help message and then gives me basically the mandatory parameter and if that help message was useful then you'd have help and you'd know what to do one or more computer names that's not very useful is it so given that the computer that's the parameter's name computer name i know it's kind of yeah but you guys get the uh the idea here is that this is um a great way to add in some help for the individual parameters um that shows up one of my other again you know for you you might say well i'm not going to do that and again for you you don't right you you've named all your functions t and a and b and so you've got 26 you just iterate through but it's when i'm going to go and i'm going to give it to jason right he obviously needs all the help he can get so i'm going to write this script with lots of help so that again we always think and i encourage you to think this way as well that whenever you're writing these tools think about the person who is using your tool and they expected to use your tool on friday after everybody went home so six o'clock they're gonna do something they're gonna be finished by seven and they're gonna be home for a late night dinner with their family and a movie okay but it's not it's now three o'clock in the morning on saturday everything's gone pear-shaped and they're frantic okay now write your tools for that guy right so don't be esoteric with the names you know at some point you know he's running something make sure there's help make sure there's confirmation make sure you'll think about that person because you know what that person might be me and i want your tool to help me and and it might be and and since you you mentioned that when you write your tool please help message is good and yes use better write something better help than what i wrote because i would appreciate it because yes definitely appreciate it one more uh to take a look at before we take a break um and i have a question about this i let me explain this to you and then um jeffrey this we have this attribute called alias oh i love it and and so so talk about this why does this exist in the first place and why do i have that there's many reasons why you might have an alias imagine you wanted to be like dark carlos or da carlos dangerous did you just call me dark carlos wasn't that the guy's name i'm a politician yeah thanks carla's danger okay um is that um we have some strong guidance for the names for things and again it's about this world where you think you type and you get and therefore we tell everyone hey this should be the term computer name and people like okay that's great i love it i'm into this thing type get that makes total sense for me a middle sense for you guys but not for me because historically i've already got some stuff and everybody knows it as host name not computer name so i'm going to use hostname and we say no actually you're not you're going to call it computer name and they're like but that's not going to work for me it's like no i totally respect that here's how it works so you use minus computer name but then you can have an alias and so that means that when your users use this they'll see computer name and they'll also see that there's this alias hostname so they can use either one so if they're used to using hostname and they'll just naturally use host name but also when they're looking at the help they'll see oh i'm supposed to be using using computer name and they're letting me get away with the host name right now yes now there's another reason for this and this other reason is actually even cooler the other reason is um so remember from the previous session we talked about this pipeline you get from pipeline by property name right okay and so what that means is i got an object i send it to a command led and i first say hey does he bind by value yes is it the correct value yes or no and if it is it gives it to and it goes if it's not it says okay can i bind to it by property name and so it takes the property name and says do you have a value do you have a property by that name and if so it grabs it gives it the value and calls process object with alias it'll say do you have that property name or do you have any of the aliases oh so this is wickedly cool and so what that means is so you got this object like okay i'm sure i'm writing this tool and you wrote an object and you wrote it the right way and so it's got a minus computer name and so i say buy property value computer name and i am able to run your commandlet and pipe it to mine and everything's happy but joe over there joe wrote a tool and he emitted an object called hostname and it's like thanks joe could you please go do it the right way and which joe's already been fired so the answer is no and so like well what am i going to do well i'm in position you'll see that there's other techniques you can use but i'm in a position to just go and say okay the computer name has a an alias hostname or host or ip address or whatever and i can have a number of those things and then as i get the object and says oh do you have a computer name no do you have a host name yes okay well give me that one to go so it's a way to be super super powerful and this is a great by the way the first jump start this is step three in the module on the pipeline deeper where we had to do a custom column where we created our own property name on the fly because they didn't match up and so what you can do is you now as the tool maker well if you know that you're going to get yes they're going to send me things under properties called computer name and somebody's going to send me stuff under host name you can solve the problem up front so that the admin that's using these tools doesn't have to go through all that hard work to make the conversions and to create the properties on the fly so let me show you this and then we'll take a break here's turns out all of our friends in the international lands have no idea what carlos danger is they don't know who carlos danger is well this is the tool making and so we don't want to talk about carlos danger too much because there's an american politician who got himself in trouble using an alias and that would be his alias that he used in its yeah american politician bad bad bad so guys it's called alias and i'm going to say use what jeffrey says instead of host i'm going to just change it to host name watch what happens here i'm going to run it so you can see what the results will be so we get comp info and i want you to see that i can still do dash computer name but here's kind of cool looking i can use hostname and as jeffrey pointed out this is a great solution if you know that you're going to be receiving uh objects and properties that don't match you can make them match to what you want them to be for for pipelining this is awesome just want to make sure if you've been following along with the slides in that that you know where we're at so i just want to show you in the slides where we are where are we um in module 5 what we've talked about so far is we've done mandatory parameters and we've done switch parameters and we've done accepting pipeline input very very important this is the cool part for everybody is is making your command let's accept pipeline input parameter help definitely cool and we just finished um doing with aliases which very helpful to be able to have aliases there are a couple of more things that i i wanted to show and if you want to show something too that's that's awesome but let me see what you think of this um you did this earlier this validate set oh yeah so i just want to show everybody that the validate set is is definitely one of my favorite things now i'm doing it on computer name but you can do this with a lot of things when you know that there's only certain values that you want an admin to be able to type in validate set is your friend so a lot of times with wmi or a lot of times with a lot of the tools that you make you're gonna say um you've got three options you type in the sun the moon the earth and the ocean well i guess that's four options oops the sun the moon the earth those are your options if they type in anything else you could write the code that would check to see if they type but why we've got validate set so take a look at this validate set dc client and i'm going to run this and so as you saw earlier when jeffrey had done this and i i just i just love this git comp info i'm going to do computer name and watch i'm going to do something that it's not going to like [Laughter] guys the production guys are going uh oh and look i'm bleeding all over the screen so get confident but look at what the message says cannot validate argument on parameter the argument local host does not belong to the set dc and client so the error message will tell you what you should be typing in which pretty useful i think is very useful and so validate set is hugely hugely fun and i like to use one of my other favorite ones yeah is and guys this is example number six hey does intellisense work with uh validate set you know intellisense has gotten so cool now that comp emperor computer name is that oh look at that look at it look at look look look look oh i can't get it to do it ctrl d air control space yeah look at that so do you guys see what's what's going on here this is actually really kind of freaking cool is that an intellisense knows your validate set so it's displaying what's in your validate set another reason to use validate sex another reason yeah absolutely so actually that's wickedly awesome um probably hit so hit client and yeah and then comma and then control space again control space is it whatever you did before i just did a space no control space control space let's turn in control space okay so well it gives you the whole list yeah okay so i was wondering how smart it was yeah jason shark did that stuff awesome stuff he's super smart but only so smart oh great so send your email to j snover app no actually he's correct because what that says is you can use those names over and over and over again so no he's correct so he's he's much much higher on the smarts kind of smarter than me because i forgot about that [Laughter] so guys one of my other favorite ones is uh parameter count and so validate the count and this is so that you can you know if you want to control how many things somebody can type in so here's a great example maybe your tool is very network intense and you only want them to be able to use maybe two computer names at once so let me run this and show you i'm going to put in three so that you can see what happens here let me get rid of this guy why am i getting rid of it because it's just what i want but i like to type comp info computer name so dc client and s1 look at the error message that's come up is cannot validate because the number of supplied arguments three exceeds the maximum allowed so what i've done here is i've validated a account that says i only want you to be able to put two computers in so it might be a command like we're going to talk about in a little bit that makes changes and maybe you don't want them to make changes to more than a couple machines at once something like that or maybe your parameter you only want it to be able to make a a couple of things um and then i'm gonna show the last one that is that is my favorite but i have mixed emotions about this and i have mixed emotions about this oh because i i don't remember if we talked about this in the last jump start but the last one is validate pattern yeah what do you mean mixed emotions i have mixed emotions about this because i love it it's what's inside of it i don't love oh well there's that is that okay so guys if you're taking a look at my screen um validate pattern is one of the most useful i use it constantly this is if you know that somebody's going to be typing in data and you want to make you want to validate that data that it's correct before they type it in and the example that i have here is imagine this i want you to type in an ip address well here's the thing i want to make sure that that ip address that you're typing in meets certain rules like it's 0 through 255. you can't type in 999.999. that would be wrong so i want to validate that you're typing in the right number range and that you're putting in four octets not three or two yeah that requires a little bit more finesse and so powershell has the ability to support that but now you have to use regular expressions yep and regular expressions is and i i think i i may have already told you this i may if i did this in the previous jump start i'm sorry but um uh my best mate is this uh super developer and he has he has a word for regular he has a phrase for regular expressions and his his thing is this is so and he's british so and he's pompous so i have to as they all are anyways richard yeah richard yeah exactly richard sorry it's like um so so you've decided that you have a problem that could be solved with regular expressions well now you have british that's my british so so you you decided that you have a problem that could be solved with regular expressions well now you have two problems so guys look the the stuff that's in the middle here is is regular expression syntax um that is going to validate this data the worst accent ever was it really yeah i almost wish we'd get richard on the phone because that would that would help out a lot um i'm sure he'll yell at me for it later um so you can put regular expressions in now if you don't hand coding regular expressions can be challenging so you can go to bing and search for a regular expression which basically is what i did here um also you can learn regular expressions they're kind of bizarre at first when you learn them but you can start to learn them and build your own i'm a web guy so i have to use regular expressions all the time so it's something but validating patterns of data is totally awesome and so let me just uh run this and show you yeah it's pretty hideous i agree yeah it's hideous and the thing is with regular but hideous yeah very powerful but what i've run across with regular expressions is you're not looking for the shortest one you're usually looking for the longest one because we have to get all the rules in the logic of those rules have to get in there and it usually requires a lengthy regular expression to do it in this case i'm just doing some simple ip address checking i'm not really checking on numbers i'm checking on to make sure you have enough octets in here so get confinfo and i'm going to set computer name dc because it's mandatory and i'm going to do ip address and i'm going to 192 168 3. notice i didn't put in enough octets for it so when i run it it comes up and it says yo dude you know your ip address does not match the yeah and that's kind of hideous looking but it does not match that pattern so this is probably every time a developer talks to me about well i want to validate data this is usually the direction that they're headed is they have very specific data validation that they want to do and powershell will let you do it yeah is there anything else yeah can you switch to my screen so here what you want to do is uh there's a ton of these things okay and so here what they did was i did a help about functions advanced parameters my show window okay and so this is a great document and down here well here i'll search i use the old search error validate so now look i'll go up a bit parameter validation attributes so you can do things like allow null you know hey i take a a computer name which is a string do we allow nulls or not okay do we allow empty strings do we allow empty collections okay and then things like validate count okay so that says hey i got the bingy bingies what do you call them binky's binkies pinkies oh like the little thing with the yeah okay you got binkies here how many can i have can i can i put an a a thousand elements in it and this is saying hey no uh i'm gonna validate the count you have to have between one and five things that's it you can validate the length so that says hey i'm going to take a string okay but the string has to be between 1 and 10. that's a fun one let's show that one actually that's it then you know that one did you know you didn't know that one no i i you know what i've i've used that before but you know when i've used it i've actually used it on computer names right because somebody types in date so ridiculously long name you know it can't be validate oh and see that that was control space because i can never remember these things so i know it validates something and what did i say crap as i said validate l you thought i was joking didn't you validate length um let's say it's got between four and ten characters yeah let's give that a try okay so that says okay get go away go okay hi comp info my computer name uh a [Music] too long there you go long see i like that i like it i want to tell you how long it is 18. look at that see that okay so that's that's one well that's all you guys okay so that's god darn it oh i see there it is okay so that was validate length so that was cool validate length you saw validate pattern very powerful validate script now this is the oh no i haven't used this what i haven't used this i can't get don jones to stop talking about this well yeah but who listens to him oh i mean sorry that was on live camera so here's one let's see we'll do again i'll just do one of these and i'm just going to get rid of this stuff start from scratch test and what i'm going to say is let's say that this is a path and i'm going to say cat dollar sign path okay okay so let's make sure we get this okay and then i'm going to say um what the heck was the name there oh i don't want to type all that so of course t [Laughter] so now i can say t c colon jump start jump start uh mod 2 dash that voila it works yeah oh very cool okay blows up so here now i might have done a whole bunch of stuff before it finally blew up because i gave it a name that didn't exist right so what you can do here is you can do validate valid something validate script validate script and dollar sign this uh well i can say test path dollar sign this ah i think i have to put that in squiggle brackets and when i do it again validate cannot bind path because it is null oops oh all right that's all right that's duh oh anyway that's how that works what the heck so so what is the purpose though to the validate script i mean what is his here you know when in doubt when in doubt always bring up the help right yeah so where was that help where'd my show window go help by the way so now here's a little trick i'm trying to bring up the help that i brought up before and i forget where it is and i did hit blah blah blah it's gonna yeah so what you can do is you can say pound sign and a portion of the command and hit tab and it'll oh and it'll go right to it you know i saw you do this before and i've never done that trick i think that's awesome it is okay so there was validate script script validate script oh i can't have two of those okay so here it's a script oh i see dollar sign under bar maybe that was my mistake it says it can't be current date okay so if i say if i said t minus event date get date it would say hey that's not going to work because they already have oh i see i see i said i said get date uh 12 25 2013. christmas happy what the heck oh because the path is still in there yeah so yeah that's why he's yelling about that so validate script is actually your ability to run code that it's going to about that it's going to use as the validation exactly and so and i think why don's been talking about this is because and this is really cool is because in the scripting games that we just that's why it was in the scripting games we um there was one of the challenges had you um check a path or something like that and somebody had tested the path i think yeah validate script that's what it was yeah and it was so cool because if you typed in a pass it wasn't there that's it i used dollar sign this and i so i went back and i fixed it i used dollar sign under bar instead of dollar sign this so validate scripts okay takes a script block takes a script block and and basically returns true or false we evaluate just like our where clause takes a script block evaluates it evaluates to true or evaluates to false if it's true happy happy if it evaluates to false not so if you give it a value um that doesn't exist it's going to evaluate to false and if you give it a value that does exist and the reason that this was such a cool thing and we all learned about this in the scripting games i can't remember who did this as an example but a lot of us that were sitting there looking at these scripts went oh this is wickedly cool because instead of because we were writing the code to check for the path to make sure it existed and this checks for it right there at the validation yeah i did totally awesome awesome damn so yeah there's a ton more stuff about parameters and the document by way so again another technique is when you're kind of messing up and things aren't right go to the examples in the help figure that out so what happened right so i thought i knew it step number one i don't remember all these things okay so i thought i knew it i thought it was dollar sign this it's wrong i'm not sure what to do what do i do go to the help use the example in the help i use the example in the help and then i notice oh it's not dollar sign this it's dollar sign under bar duh and then i go back and i fix the original problem we know what's really interesting is that a lot of people would say well you should have practiced your demos or something and it's like no you don't understand in powershell this is what we do every single day this is what happens to us and i wanted this is how we solve the problem exactly i want to be clear about that right i do not want to come here and show you this polished no problem thing because that's not the real world and i wanted to be super clear that you know i've been working with this technology forever and ever and ever and it's not about memorizing it or being an expert it's about being able to figure this stuff out so that's why we show you all the warts we show you these errors i love it when these errors occur because then it shows you oh and then that happens and then you do this and then that happens you need to do that it that's more valuable than okay now write this down 17 times because after 17 times writing it down you'll commit it to memory that's nonsense and i think that's a very important thing that we brought up in the the very first jump start which was don't memorize stuff that's why the help system exists and it's a really good help system so as we're making mistakes it's what happens to me in real life and i have to go look something up because i screwed something up so yeah anyways so this has been a a a a great run through on some of the the parameter attributes that we can use which is totally awesome so let me catch everybody up in slides because we're going to shift modules here um we got about a half an hour before break and so we've got really cool stuff so we did a couple examples of validate set validate count validate pattern are in there for you check out the help file and try those that really helps out quite a bit and this is a great time for me to just briefly show you before we go to our next module that at this point i'm going to go to a create my own little blank one remember ctrl j where the snippets would come up oh yeah what i love is this complete snippet because it gives you inside of here look at all of these yeah this is what i use that these are here right yeah this is the one i use all the time yeah it's the one that oh there's where i got the sun moon earth that's where it is i know it was in my brain from somewhere um so this is a great example this snippet is is great it's the one that i use all the time too because it helps remind you about some of the things that are available that if you need to you can then they give you samples right here or you can jump into help and get further ones what we're going to do is we're going to take a break a 10 minute break see you in 10 minutes 10. [Music] so let's move on to and this is going to be a cool topic this is one of the most fun things in the world i think this is um to me when i first saw this this was freaky cole if you watch the first session you've seen this but i want to show you a little bit more about this it's writing help now you putting in help into your tool especially if you're going to give it to somebody else is the is is to me one of the most important things in the world however when i started scripting putting in comments and putting in help was the last thing that i did you know it was just bang out the script and you can i have that yeah here so you've given us this capability of creating the same kind of help that all the commandlets come with yep and it's fast and it's easy all we have to do is do it right yep just do it well let's no more excuses no and i think that's no more excuses is the point to that so let me uh open up the new set so we're going to go on into the help system oh and it would be helpful ah see helpful here's what we're going to talk about why you should provide help which we started talking about because you should um 3am in the morning by the way sometimes it's your scripts i've done that myself like something bad's going on and it's like oh well it's my script so i know what i did but i wrote it a long time ago so the fact that it's helpful you know it's helpful it's got comments it's used variable names that you can read these are things that are are good yeah and so you know admins you're the tool maker now so you have to take the responsibility of making sure that admins get the same expected behavior from your tools that they get from the powershell team the active directory team all of those teams that that write help and so you want to have this d uh detailed help and it's so simple to do let me start off by by showing you the help file for the help um the uh just just because okay i thought it was funny this is what module six six um and so let me just get help on and it's comment based help and so i'm just going to search for comment and it it just brought the file up for me but it's about underscore comment underscore based underscore help matter of fact you can see right across the top here is the name of the the file and i have to tell you this is uh definitely one of the help files i spent quite a bit of time reading because of a couple of notes in here first of all comment based help gives you the same format that all the other commands are when somebody uses it and they see it it looks the same way but there are a couple of things you're not gonna get with comment based help how many of you and go ahead and raise your hands because we can see you honestly we can uh it's freaking you out we can see you how many of you have uh done update help with version three powershell yeah so here's the deal when you write comment based help you're not going to get that feature of update help you're not going to be able to update your help and have this magical thing happen with your tools although it does tell you why briefly in here and this is kind of a tricky subject so you're going to be able to do create your own help there's also another way to do this right this this xml base xml yeah yeah that's where you get all those features right where you can you can have it do an update help if you have it you know stored where you can run your own and do all this i know june blender was writing all a bunch of stuff up about it yeah it's actually part of the modules is required for update help we have a url when you do a module you can have a help url to go get it to go get it and so the xml-based stuff and the update help stuff is a little bit more complicated and if you want to dive into that and get in that you certainly can but let's get good at some of the comment-based help because this is a part that a lot of people leave out but that's one of those things i just here's here's the asset test like when do i use comment based help versus when do i use this xml and the update help and all that stuff and i'd say if somebody give are you charging money for your scripts right if you're charging money for your scripts use xml based help if you're not charging money for your scripts probably the other one's just fine now that's not entirely true you know there's a little bit back and forth but that's a that's a good determinant and i've written a lot of commandlets and uh a comment-based help is fast it's easy it works and unless you're right unless i'm selling it to somebody where i need to be able to do updates and stuff comment based help for me is the most wonderful thing in the world so guys let me show you a quick example of comment based help and it's all in this help file every single thing is in here so make sure you you check out the help file for it what i'm going to do is whoops not even close on the bar keep telling me because i ah so if you watched the last one we showed you comment based help real quick but we didn't show you the whole story first of all and this is gonna be kind of interesting because i i'm i'm gonna ask jeffrey a couple of bizarre questions here comment based help is i want to show you guys the comment based help first of all placing it notice um we've got uh block comments which block comments first of all yeah which i love them are great now usually in a lot of languages when you comment something out that means that the parsing engine in that ignores it that's not the case here in canada as a matter of fact we're paying attention to what you do see we not only know when you download stuff from the internet we're watching what you do in your comments so careful what you type in there like santa that's no naughty and nice so i've got one key word in here that i want you want you guys to see it's called dot synopsis and i to i'm going to call it this is the short description just want to make sure i don't have anywhere okay good and i want you to see the the cause and the effect of this first of all before i um run this or actually i'm going to just run it and notice it's called get test help so what i'm going to do is i'm going to do get help on get test help i want you guys to see that lookie synopsis this is the short description so you can start adding in the same things that commandlets have but here's yeah so notice you got name and you didn't do anything for that we gave you that yeah you gave us that and then go down you give them syntax for free and uh and uh then some of the related links and if you did minus four is full yeah let me do that full real quick now just see see what's happening here is we don't have any parameters defined yet but yeah so what happens is this this help we're actually pretty clever about this stuff what we do is we take a look at the document we take a look at the metadata and from that metadata we're able to generate a certain amount of help and then oh you gave us some extra information so we'll bring that in and oh you give us some more and we just incrementally build this up by the way that's why some people were were outraged outraged when they heard that we had removed help from version three we said well no it's downloadable all this and we explained all that and they were outraged and then they got it and then they're like oh but there is help here i mean i wish you had more descriptions but the help's all here right answer is yes that's right so what happens is because we have all the structure we're able to generate the help files on the fly but there aren't the strings you know these descriptions and that's the stuff that you say update help to get in fact i want to show you guys this real quick of what jeffrey's talking about i'm going to go back to one of my module five demos where um let me just do as a matter of fact let me go back to module 4 where i had one of them so you can see this we we did show this a little bit in the last one but this is a great thing to show so i'm going to run the making objects uh script and so six remember it's console you want to add the extra dot in there and i think it was get comp info let me check here get confident feel good watch we didn't do any comment based help back in that module so are back in that section so boy i can't even type get help anymore get help get comp info and i want you to see which every time i look i'm going to do dash full look at everything that they're already providing us look at what they've done those parameters we were creating they've already put those parameters in if you filled in whether it could do pipeline or whatnot you're going to see that later that gets all filled in they've already built the syntax out for us correctly all's we have they gave us the name all we have to do is go fill in a few more things hey can you open it up a second edit it edit it sure yeah because there was a comment in the in the help form or in the q a form i wanted to address okay and grab one of those and uh add a bunch of aliases to any oh i okay computer name so computer name we'll add in uh add a couple so um we want host quotes um oops yep thank you host host name and so is it is it is comma and it's host or yeah let's do that host name host name um ip address and then we'll be done ip address and emperor and bracket got it okay so let me uh run this real quick and do it again actually you know what you can do here's even better say get help and then the name of command getcompinfo minus parameter oh computer name parameter computer name which this is really cool because yeah oops it would help if i could spell correctly computer name and you see someone said oh well you know what how do i get the aliases they're right there they're right here so they're your aliases isn't it correct though in powershell 2-0 though i don't think they did display okay in the parameters so but which is another reason why you need to be running freeze i remember being cranky about that yeah i i think i remember so this is another reason to run powershell v3 because it could be confusing if you have aliases and you don't know how to see them that's a problem in v3 though you will show you those aliases when you look at the full help that's where you see all the definition all the parameter attributes so it's dash full on get help or as we just did here which is really cool if you just call up that individual parameter so i'll show it to you the other way you can also do it you know if you're doing dash full you'll also see it as we go up to the parameter right there so computer name and you see the aliases for it so what's happening already is is awesomely cool you're already building a help file for me now all we need to do is start adding in a little bit more yeah so with this example i want you guys to notice i just added in the synopsis now there's other stuff that we're going to add in in a second but there's a couple of locations where you can put this block comment of help now you can put it like i have it right here right in front of the function be careful of spacing it'll be okay if you do one it's not going to be okay if you do too so let me show you you you need to put these together and be a little bit careful about the spacing of it so get help on get comp info and now you're going to see well there's no synopsis it's the spacing in here so you want these guys to be tied together but there's a couple of other locations where we could hide this i'm really interested in your opinion i usually are you sure turn the center camera on i'm not that interested in it no i actually really am interested in this opinion on this i usually put my help before the function yeah but this is kind of tricky though i'm going to hit control x and take it out there there's a couple of other places right we can put it um here as right under the name inside of the function right we can put it there if you tell me so no we can't we can put it there we can also put it down here at the very end making sure that again spacing is is crucial making sure that the end of the block comment is right here at the end of the squiggly where do you like your help always at the top so here's the thing i i have to say that some of these like it works here and it doesn't work there drives me nuts i'm not entirely sure where in that code base that made sense i made it made sense to do so and especially if it was like wildly like oh you know of course people want to put it here versus there but uh anyway so i just learn one thing and i don't think about the others ever ever ever ever ever and i put it at the top and i make sure there's no spaces and i don't ever think about it again in fact you know what i don't even do that i just type ctrl j because the next step is synopsis who okay stop okay you hit it good okay spell synopsis uh i don't know i bet you you ain't nothing but your english majors are like well of course it's s something or no you spell it out i'm a physics guy right i can't spell synopsis like i can i can sort of say it am i saying it correctly you're saying it okay and i can't spell it either exactly and you remember you're talking to the guy who can't spell description okay synopsis and all these other things and i can't remember all the terms so control j and the the comment based help is the way i always always always do it and a matter of fact we're going to use that but uh so here's the uh uh note guys put your comment based help right here at the beginning of the function if you join us at powershell.org and with the scripting guide we write we have the scripting games great way for you to practice doing challenges and scripting and getting feedback from the community to help you script better you always see the comment based help at the very top of the function when they're when they're writing it now a couple of other things you can put in there is um on comment based help and let me go to example number two where and i'm not going to hand type all this out so you can see it but there's a couple of other things synopsis description if you want to give an explanation of what the parameter is parameter computer name and then you can talk about what it is and you can notice there okay so let's stop there so you say dash parameter and you can get more details about that and that get shows up in the help file uh and you don't have to give it for all of them you can give it for some and now yeah and in this case i think when you look at the script i've i've only given it for one so that's that's kind of cool um you can do multiple examples and you don't have to worry about numbering your examples or anything like that because they'll take care of it you just say dot example give us an example of how it's used and you should give a lot of examples of the way that you intended this oh please help me with this i can't convince guys of this you need to put in examples of how you intended and designed your commandlet to be used versus what versus a lot of times you'll see somebody will put in just one example yeah no don't do that yeah please please help us again i would encourage you to take a look at uh get you know get the help take a look at the examples for process i mean again that's i think we did a really good job there and we showed all the various things you want to do and it wasn't just um you might say we went a little bit overboard on get process because we also tried to teach you some scripting techniques but we did this in the context of hey here are the sort of things that you're going to do with this you're going to run this and then you're going to pipe it to that you're going to run this and then you put it in an object and then you're going to do that all those things that you think are going to be common usage patterns i mean right you wrote the darn thing so you should have a clear idea as to what you think people are going to do now indeed sometimes you get surprised by things but in general if you see something people doing something clever or you want people to emulate you got to show them right because people again again conceptually here's the way you want to think about it you want to think about it as people's people who have hair their hair's on fire and the last thing that they want to do is to spend time like trying to figure stuff out help and and particular examples are just this clear-cut way like look don't don't you don't have to figure it out just here try this and uh if you want to get into the details there's something called vygotsky's activity theory where competence uh comes after performance which is to say you get people doing something above their level of competence by following your examples and in doing so then they're able to learn oh okay and then fill in with competence oh i see how that really works so anyway there's theory behind this use lots of examples so fowatsuki vygotsky vygotsky was this uh russian psychologist did something called activity theory um the bunch that influences a bunch of things we do you know we we have these things called jeffrey isms this is going to become one of them one of my favorite ones you had written a blog on and this this is when i'm scripting i think of this is is is and also when i'm doing help i actually think of this and i'll see if i can get the quote correctly but you were quoting i think somebody else and i'm sure you probably remember um and the quote was um a vision without action is just a dream yeah and so when i'm working with a commandment or selection yeah um when i'm actually writing a command like the way i'm testing the commandlet every way that i'm testing it to me should be an example in there that's the actions that i'm taking is that if i envision that you should be able to use it this way then that needs to be an example of that you can type in that you can use you know in other ways it's the golden rule do unto others as you would have be done done to you when you're trying to learn something how do you like to learn something i bet you it's examples so and it's not carlos whatever so guys take a look i've got a couple of examples in here and i want you to see this this is the comp info commandlet the rest of the code has all been the same this is what we were using earlier on uh i haven't changed anything notice it's still ordered all that kind of stuff and so now we're adding in the the help support and value to this let me run it and so you can see that now we've got beautiful help and we've done it correctly so get comp info let you do show window and i think you're right let's do show window i've got to get in the habit of that because this is totally awesome so as you can see everything that i've written in here everything that gets generated for me automatically the description look at the parameters computer name i gave a definition to so there's my definition also this supports pipeline input so now you're starting to see that being added in for me you can go down and here's each parameter i guess i added in error logging as well but notice i didn't add in a definition for log file and then there's all of the examples so show that where'd you get that text for for air logging for for error logging yeah switch to turn on error logging you know what i have to take a look go look at the parameter uh you didn't know that i didn't know that are you are you kidding me yeah so go put up that was as easy as doing that yeah put a comment in front of log file and do it again guys do you see what he's he's showing it i didn't know this this is kind of freaky cool um i'm going to put a comment with your money to show up wasn't it it was worth my money to show up for this it was definitely worth uh so let's put in um this wow isn't this rocking well yeah if it's going to work wow isn't this rocking rocking so what you're saying is that's going to show up here i don't know let's find out oh my god i didn't know you could do that that's really cool okay so there are again multiple ways to do things uh the best practice is probably going to be to put it up here in the examples and and the parameter where you're defining it but that is awfully kind of cool i didn't know that would happen that's that's surprising um and so you we've got a nice help system at this point and all the help that we need for our people that are going to be working with this this command this at this point has started to become something that is that looks smells and feels very professional so you've got help that's in here you this commandlet um produces objects that's important it produces objects so it can work down the pipeline with select and where and and all and convert to html and all that kind of stuff i also have supported pipeline input for parameters that make sense that i might want to pipeline things to them like computer names it's very helpful to think about what do you want to what is going to need to be pipelined to this commandlet because you want to support pipeline input we've actually been doing quite a bunch of stuff today with all of this yeah by the way so did you show them the examples i'm sorry i was looking thinking about something else in the house oh in the hell i think i did let me just pop it up again real quick down here look how see they numbered the example see powershell guys numbered um there's the definite you know i wrote that in yeah so now here's the thing okay now now make this go away make this go away yeah and let's change it a little bit let's uh let's add let's add like a hostname alias to computer name okay so uh computer name it's alien name yeah so we'll do um alias whoops bridge too far there we go host name um and then compile it did you just say compile it well actually that's what happens that's what it is so you just want to show the help no no no no now i want to write now from here say get comp info my host name and then dc1 or whatever hostname dctc yeah whatever carriage turn okay guys now watch this so now go and select that that at that output oh select the output yeah you want like this yeah and all the way up to the all the way up to the top oh yeah no no it's starting with the command line we're going to i'm showing people how to do help oh okay oh maybe yeah so grab that grab this control c yeah and then just go back up into the this and you just say dot example and paste oh you just say dot example dot example paste and then do it again and then you'll save it and i'll do a show this is really cool because this is a nice way to do it really quick to get your examples in there with some results exactly so you use people can see what the results are so in this case hope this works good well i'm going to feel real stupid if it doesn't a show window and scroll down here i like this a lot because i i know what this is yeah you know you make it wider but yeah yeah we could uh uh do the formatting a little bit different we use format commands that kind of thing to get the formatting but what an easy way to grab it and put it into the help file yeah i think that's awesome oh that's totally cool well guys you know what this is about all we need to do with help files uh comment based help is awesome what you have to do is make sure that you're using it we've gotten our code up to a point to where this is a viable tool now what we're going to do is we're going to take a break a 10 minute break and when we come back we're going to show you a little bit about error handling what happens if we were running this and the computer wasn't available we want to show you what happens what powershell does by default and how to control this and how to create your own error log and then we're going to start showing you the dangerous stuff we've been getting information i know at the carlos dangerous not the carlos danger style we've been getting information what happens when we want to set or change some information set or change so see you in 10 minutes 10. [Music] i was going to eat all these oreo cookies before we went back on so that i had oreo cookie stuff in my yeah okay yeah it was no no i'm air handling because i i never make an error so this one's all yours babe because i don't make mistakes well clearly yeah clearly have you been if you've watched any of this oh that's good so yeah if you've been watching any of this i constantly make mistakes and i constantly um when i'm using commandlets i'm going to make mistakes and it's it's really great to get a helpful or useful error message and it's also really great to to trap or grab hold of a of an error message that doesn't make any sense and give me one that's that does make more sense to me and what we wanted to do is do some basic error handling now there's this can go very deep with the error handling and and what i've done is set up some some simple examples of doing some basic error handling to get you started and so let's take a look at this and see what you think first of all here's what we're going to do is we're going to show you how powershell treats things when errors happen and this is actually what i think this is really cool so this is going to be a fun little demo and then we're going to show you how to start to trap when errors happen and then how to deal with them and something called a try catch finally block and i've got a couple of examples i have obviously since i have something that says a better example you know i'm going to start with a lousy one apparently and then i'll have a better one and we'll see what goes from there and if oh please join it because i'm going to screw this up it's all about errors that's all i i yeah so here's the deal dollar sign air action preference this is one of the the interesting things this kind of freaks people out in powershell at first that when they run something it just does it yeah and it's to me i i'm just that's what i want don't don't ask me you know art are you sure yeah not i was sure when i typed it in just do it and powershell does that but there that's actually controlled though um the air action preference of of getting errors and how errors get handled as it goes through and it does things is something that it's controlled and we have this error action preference variable what is what is this error action preference variable thingy yeah so basically the the issue is when something goes wrong what is the right thing to do for the user and the real answer is who the heck knows right um could be this could be that could be that so what we do is we have a default answer and then we allow the user to decide to do something differently let me give you an example so whenever we as a developer you write code there's always this issue of like the compiler should the compiler generate an error in the first error it sees and then stop uh and some of them used to work that way and see like oh okay great i got an error so i go fix that error and then you compile it again it's like line next line next slide it's like okay well fine fix that next line and it's like hey you know what why don't you give them all to me and then i'll fix them all at once and and so sometimes you want it that way sometimes it's like hey i'm going to go do something to a very large set of systems but if something goes wrong i got to stop like and then roll it on back right because it's all got to be perfect and other times it's like what do i know i think everything's going to be fine i'm not sure what to do something goes wrong come back and tell me and then we'll discuss it and so that's what we do is we allow you to choose what uh behavior you want to have in fact it's a little more the story's a little bit more complicated than that i wish i had a white box a white uh board here okay describe it but i'll use my hand you can just draw on there well you know i've described this uh um in powershell um it looks like a command line right a pipe to b pipe to c looks like a traditional pipeline but that's not all the case it's actually masking some amazingly powerful technology and what happens is it's an actual an object flow engine where a b c are these compute units that then get hooked up in this directed graph notice i didn't say acyclic graph directed graph um and we pass control to one and it runs we output an object we then give that to the powershell runtime engine who looks at the data contract required by the next guy and then the data engine does all this manipulation hey can i just accept it as is do i have to parse it and you know match the properties do i have to do any coercion or casting or parsing required to give it to it and then it hands it to it and then it runs which is the magic about making the pipeline work which is the magic we got into it from an administrative level but that's that's definitely deeper but that is that magic that's making that pipeline work exactly now the original design was that the pipeline was just one of n streams of objects so you have this object and you have any of these n of these streams you got the error stream sorry the output stream the error stream the verbose stream the warning stream etc and the idea was that each one of these streams could have their own pipelines now that was the original design turns out we didn't implement it that way but i still don't know who did that anyway call bruce payne anyway so but we do do is we have we do treat it largely like that but then the actions the point is that ultimately what you'd like to be able to do is to say on the airstream i can just run an entire another pipeline against the error actions now we don't implement that what we did was we have a set of enumerated actions and it's a little bit simpler to understand as a matter of fact let's let's take a look at some of those so first of all guys this is um uh the the first demonstration i have in here i'm going to run these we're in mod 7. yeah module 7 which we and these are the scripts for module 7. and so first of all we have the dollar sign error actually preference variable which shows you what the default is and this is what powershell is going to do when it runs into an error the air is going to get displaced and he's going to continue on so let's take a look at what that looks like i've got a simple get wmi a bunch of people will never remember this the dollar error action preference so the way i remember this because i don't remember this is you do a dollar sign variable oh sorry sorry do it dur space variable colon oh okay variable colon star pref star star press star and you see this we have a set of preferences and one of them is the error action preference so that's how you're going to remember it i know there's some variable somewhere had something to do with errors or something to do with preferences use wildcards and you'll be able to find it and you'll be able to find it that's awesome now guys i want you to notice that um we're not going to change the system default right now instead we're going to do it with the command let's take a look at this get wmi 132 computer name i want you to notice i'm not doing anything special but i've got dc not online and client so you can pretty much guess that there's going to be a failure notice what powershell does when i run this i'm going to clear the screen down here and by the way for you at home you'll want to change dc and client to be like local host yeah exactly oh good great yeah yeah great that was that was that was a good point so let me go ahead and run it because all it's all going to fail if you do it so let me run it you see the first one gets executed so that was it was dc we're trying to reach not online we didn't get it but look i want you to notice we got the first one we get an error message saying hey that server isn't available and then it went and it did the next one in other words powershell's default is to continue so here's the error but i'm going to keep trying and i'm going to keep going now i want you to see what some of the other options are besides continue sometimes when you're running this well you know i know there are some computers out there that aren't going to be available and i don't want my screen bleeding i don't want to see it on the screen i just want it to run well take a look we've got get wmi object 132 computer names same problem but now i'm going to use and i've used the abbreviated versions of this so let me show you what the full versions are on the slide here first we've got error action which can be ea and we've got error variable error action means we can change what with the action that powershell is going to do and we can store the errors in error variable now if you're thinking about this well if i store the error as an error variable oh and i wanted to make an error log i could probably just out file that variable but we'll get there so you can see that we're gonna so take a look i've got error action silently continue and ev error variable my error so i'm going to run this guy and let me clear the screen so you can see what's going to happen here now it's still pausing because it's trying that computer that's not online but look this time i just got the results displayed i didn't see any error messages but here's the best part if i want the error messages dollar my error has the error message in it also and think about this because we're going to do this in our script in a second i want that error in a log file well pipe it out to file whatever you want to call it error log or whatever so you can then quickly dump the air out to a file this is kind of cool i like this because it continues now this is not something oh by the way i have to tell you that um this is a don jones uh uh thing he was trying to get this to break because he was doing not online and he couldn't get any errors it kept returning results and he was at a client he's going dc not online cl you know client and he kept coming back and it kept giving him an answer for not online he's like what's going on and one of the guys says we have a computer called not online which apparently has no security because it lets don jones query yeah because we're letting don jones aquarius so let me show you another way though and jeffrey mentioned this sometimes though when an error it's a way to set low expectations at work i know ping not online well wait a minute why are we calling it not online if i can if it's online i don't get it it's uh so jeffrey mentioned this another thing that we can do is maybe when an error does occur we need to go stop stop stop stop stop stop because we're going to have to do a rollback or something like that so we can do an error action of stop and as you can imagine i'm going to run this let me clear the screen down here yeah let me run it up here it'll go through and what you're going to see is as soon as he errors out notice he didn't do the third computer he stopped right away on this guy just stop and now we could go on and do something else the last one that you can do for error action is inquire and this is this can be useful when you're you're testing something and it's not working and you're trying to figure out what's going on let me show you i'm going to run this one inquire i usually use this when i'm trying to debug what's going on with my command lid or something like that comes up and says hey look i've got this air what do you want me to do you want me to continue now continue do you want me to suspend the suspend is kind of cool right i love suspended because if i suspend see the special prompt the command's still there but in anything that i may have in variables and stuff i can start to look at it right and i can start to inspect what's going on this is actually kind of cool except i forgot how to exit the suspend is it just exit think type get my friends think type get think type cat yes it it is do i want to continue sure i could continue and it'll do the last one so inquire can be really useful if you're having an issue not sure what's going on and you want to kind of debug what's going on now i'm going to start to use some of these so that we can start to do some error handling and we're going to start off with could we say you didn't show the cool stuff what what i didn't show cool stuff i thought that was cool well that's cool but you didn't show the cool stuff oh well then show us the cool stuff okay so let me show you this one so so it turns out that there's a on on windows uh there are no process ids that are end in three it's actually i think any odd number of process ids right so if you do gps like two yeah i like to uh select select id notice notice they're all even numbers okay and the reason for that is i've never actually noticed this i don't ask some some good reason for it i don't know anyway so so here's the deal so you say stop process but so id 13 23 33 okay and and and voila you're going to get three errors right okay so that's that's the point is that is that that's a safe thing to go experiment with okay this error actions right so we you you saw the error variables right or sorry the the interaction right uh all that good stuff but now i want to take a second here on this error variable okay and i'm going to use e right because i because it's like t but it's like t but it's e because it means error right so now what i have is e okay and i have all the errors so that's just an e of zero okay now the point of this is the first one the second one the point of this is it's very very rich now there's a trick to this and and then it's a general powershell technique right so i'm gonna say is okay well i bet you there's more to it than that right so i say pipe two get member and oh there's a ton of stuff look at all this great stuff and i say oh well i'd like to see that and so what i'm gonna do is i'm gonna say dollar sign e is zero just do the first one format list star and it gives me the same thing yeah okay so so what's happening is that the error object is treated especially by the host okay details don't matter it's treated especially by the host in order to see all the rest of the the parameters of the the properties you have to say minus force okay so this by the way is a general powerful thing and general powershell will will do something and if you say no i wanted to i really wanted to do this you just say minus force and we'll do it so you know if if you try and kill lsass why don't you try that do serious it's your machine anyway no well i'll try it i'll i might i'll i'll try it yeah yeah because i bet you it'll work so oh i like this i bet you it'll work let's see yes get process pipe to lsass or so you get process lsass pipe to stop process now if this does not work jason's taking over while i spend a bunch of time starting your machine okay so um and i wouldn't do this until by the way don't do this in powershell version two yeah if you've got version two don't do that okay no i think it'll work in two you should try it so what happens is okay and so what we did was by the way because this because did not used to do this at some point and i used to make these mistakes so i said hey you know what we should do this so what's happening here is it'll come in and it'll say uh oh are you trying to process are you trying to terminate uh um a uh a process that you didn't start oh okay and if so it says hey that doesn't sound right it doesn't sound good and by the way let's make sure i get this right no no now if i wanted to and i'm really here what i'm going to do is i'm going to put a pound sign in front just in case oh man yeah so that no matter what and what you do is you said no i really want to have that do it you just say minus force and then we'll do that we won't tell you anything we'll just go whack that thing okay okay so now back here you say hey i want to see these things i really want to see it so show it to me minus force 4 4 ohm force and you see oh there they are we have all the details okay and there's some really cool stuff here including target object so target object 13. so wait a second so you mean i could have said like dollar sign e dot target object would it be e dot zero oh oh so you can get it for all of them or you can do the individual so this is the point so now imagine so the point is i'm going to do something on i'm going to perform an operation on a thousand things 10 000 things 13 of them fail oh which 13 well good luck my friend no no no you capture it as an error variable you have the target object and those are the thirteen and they say well now i do a remediation action you say dollar sign four you know sorry uh four four four eight four eight thousand well what's short for e i guess i'm kind of doomed um i guess i'll go back to t for each t and dollar sign e this is not best practice by the way and then you do something with it right so then you do a remediation action wait wait oh sorry e dot e dot target object target object and then yeah and then do some action you know restart now that's interesting because i bet you here in a moment you're going to help fix my code to be even better like that because i did something else that was uh but but before we do that you didn't you didn't tell are you going to talk about the views the views the views the views category view category view you know what category i i don't think so what are you talking about okay so again there's some error there's something like an error view or with an error i don't know anyway so how do you find out dur variable there's something about errors so let's just look and so by the way this maximum error count like don't screw that up or okay no so there's um error view and it's normal okay okay well that's odd right so yeah so let's take a look let's say and by the way here's another cool tech trick that's on air review i don't know if it'll work on this one equals oh that's that's not good anyway so you got to know category view okay so you just got to know this one and there's two views normal view and category view okay so before i set that let's make some errors so error number one stop process id 13. don't do that 13 and some nonsense okay and those are those are my errors yeah now what i'm going to do is i'm going to say dot sign category view sorry dollar error view equals category view i'm going to do them again look at that so these are these are very pithy ways and so instead i mean take a look glad that that says hey object not found in 32 it's 13. what was going on i was doing a stop process what was the exception process command exception you know i like that a lot and i haven't seen that before yeah no here's the trick it's always been there my friend really yeah look here so here's the text here's where it happened category info it's right there oh i see what you're saying so that view is just showing the the the terse category info that's there yeah oh that is awesome now here's the thing that that thing there um it's it's not localized it's gonna be the same everywhere as is this this fully qualified air id did you ever pay attention to that do you know what that is so look here let's do this fully qualified let's set it back a second and run it again it's a normal view okay let's do that stop process 13. okay this uh this text is all going to be localized one language to the other one so imagine you're sitting there in poland and uh you run this and you get this this error message and you're like wonder what that is well if you take i guarantee if you take this polish error text and put it into bing it's going to say something back in poland i don't know i don't know or it's going to say i don't know but this is this fully qualified error id now look what it says process no process found for given id comma and then a location so a specific error comma a location and the goal of this is you take this guy and you cut it and then you go in here to bing the best search engine in the world and you paste it then you find all the information about that specific thing so it acts like a unique search engine optimization for all the content about an error you're going to have again you're going to have errors you're going to have errors all the time and so how do you get help about the errors well the answer is well sometimes we have help documentation but the space of errors is pretty vast in general what you're going to find is that somebody else had this problem and they've got some blog somewhere that describes what was going on and and then this fully qualified air id helps you find it i love that that that really makes it helpful then to find those errors and you said it's not low it's not localized so yeah it's never always going to look just like that so you put it into bing it rocks no matter where you're at right so that also means you might find some polish blogs about this but he might not help you might help now yeah i know since i can't read it so well then you can send it to a translator i can send it to a translator do we do do we do that in bing translation somewhere awesome somewhere i don't but someone does okay folks so take a look we're going to do a little error handling here using this error action and error variable and i'm going to show you a simple way to do this and i i want you to see the script that i have here this is the start number two and it's our comp info one i'm going to minimize the parts that we we've already looked at the switch error log to make an error log and i want you to notice that i've already got a log file set up at c culinary log.txt and so let me just go ahead and minimize him my process section take a look at what i'm doing here i'm going to take a bunch of computers and i'm going to grab some information i want some inventory information so i've got like three wmi commands here and i'm going to create an object this is fine and dandy but you know what's a real huge waste of time is if i'm hitting a computer that's in that collection and it's not online well it's going to be banging all these commands at it trying it over and over again it's going to be slow it's going to be trying to retry and then when it fails on the first wmi command what's it going to do it's going to go to the next command and it's going to fail on that one i know it's going to fail but it's going to sit there and it's going to retry and it's going to retry and it's going to fail on the third one but it's going to sit there and you know retry it and retry it what i'd really like is if oh man if you get an error just let's let's let's not do this again let's not keep going through all of these so take a look i've got something called a try catch block now this is one of the ways that you can try and trap errors and then decide to do something so look at the simple format here for it i've got dollar sign computer equals not online we know that that doesn't work so i've got this try catch and technically it's try catch finally right yeah and and a lot of times finally always executes it's just yeah a lot of times in a lot of stuff that we're doing in powershell you don't always see the finely in here because we just don't need it so so if you take a look i've got try just be clear so finally is like when you want to do some cleanup you're doing something you've allocated some resources and whether you succeed or fail you want to clean up so that's what finally is for and so if you're a developer you probably know the try catch finally really well you can have multiple catches that catch at different levels we're not going there but this is in powershell it's a very powerful question it's worth making a point sorry this is completely off topic completely off topic but so here we've been teaching you the scripting and it just kind of popped in my head this tried catch finally there's this little secret thing going on you've been learning c sharp you know by and large everything that you've learned here by and large if you go pick up a c-sharp book you're like oh look these guys stole all this stuff from powershell you know yes they did yes no in fact you'll find it very very similar uh we chose the to be very much match and if you know c same thing it's getting you know c stole from c's plus plus which stole from c and we stole from them all and there are variations again i mentioned you how bruce and jim true and i struggled with this hey you know if you just took any of those languages and tried to make a shell out of it and people have and they're really crappy shells it turns out that that syntax just doesn't work for an interactive shell you need something different and so we struggled with that and we came up with what we think is a good interactive shell but also plays homage to these line of of of programming languages and so if you've learned powershell and you decide hey i'd like to go on and do something else you'll find it a a pretty smooth transition it is different but it's very comfortable and i want to point that out that a lot of research over many many years went into uh you know admins doing administrative stuff very verbose languages versus very terse languages and this really works well c-based style this kind of thing works so well for admins and i just want to reiterate what jeffrey is saying if you decide to start moving towards well i want to do some more development and i need visual studio the language that you're going then for is c sharp you will not you will be amazed at how much you already know because powershell is written very similar to using that and try catch is a great example because it's the same kind of try catch that they would be using in c sharp works the same way so take a look at my try here what you do is you put the your code that you want to try to execute that you think might generate an error you put that in the try block and notice what i've got here i've got 132 operating system this dollar sign computer might fail so i put in an error action i want you to stop and then put the air into a variable called current error so i'm going to try that now here's the important part to this you need to stop that's what gets caught so that we can go to the catch block if you said continue well it's going to continue it's not going to go to the catch block the idea here is try this and if it blows up stop and let me catch it and put up a nicer error in this case look at my catch i'm going to do right warning i'm going to put up a slightly nicer error message or a different error message i could also do some remediation code in here maybe i could you know try to test the connection to the computer and see if it's really online or not so let me just run this for you so you can see what's going to happen i'll clear the screen down here now of course we're waiting for it to fail because not online but look warning you done made a boo-boo with computer not online yes cool is that kind of cool that's that's it's really cool so it created a situation stopped and now i get to do my own thing so the point of the try catch block is try what you think might cause a problem and then air action stop and then you can deal with it the other cool thing about this is is i haven't lost the error and even the cool stuff that jeffrey was showing us dollar sign current error there's the error message and because rpc server is not available so i can now then put that into a file however i wanted to do it in fact that's what we have right here under the better try catch i notice i've got help here this is our function comp info i'm going to minimize the stuff that we don't need take a look i've got my try block and i'm trying to make an object for each computer so i've got all of my codes in here and i'm whoops we don't bridge too far all of my codes in here because i'm trying to make this object and i know the computer might fail well i know that the first time it might fail i certainly don't need to do the rest of this code so i'm going to go over here and go error action stop i'll put the error into a variable but we'll look at my catch block now i've decided that in my catch block down here i've decided to going to give you a warning but if you turned on the switch which we started out with this morning error i wondered where that would be i wanted i was going to wrap it up towards the end i got an amazing kind of guy so rabbits and all kinds of stuff so if air log so if you turn on that switch parameter here's what it's going to do it's going to take the date put it out to a file i've got a force so that it can if the file already exists it'll just overwrite it it'll take the computer name and put it out to that file and then the current error now the stuff that jeffrey was showing us would also be really useful especially that target instance kind of thing would be useful here but let me show you how this how this is going to look so let me run this clear the screen whoa stop doing that there we go down here clear the screen get comp info computer name not online uh we'll do dc so you see that one works and then we'll do not online now if i don't put in the airlock switch there won't be an airlock so i want an error log so i'm going to put in the switch parameter for error log now let's see what happens well there's dc that's awesome that's great i love it it's a yeah oh you done made a booboo with a computer that's called not online really um i wonder what i turned on an air log so there should be an air log out here so let's see what the air log has um uh dii do i have i'll look air log dot text so i can grab hold of that error log and now i've got the airs in there and if i was making multiple mistakes since it's getting appended it would be added in and added in so this is a simple way to do air handling but it gets you started down the road of being able to trap errors when they occur and then do something about the error and record it into a log file so while it's simple this is actually a great place to start with your error handling and working with it yeah that kind of log file is kind of whack come back but yeah kind of whack but yeah does right arrow right event log oh you but see that's that's the other cool thing right so you guys know that you've seen us do like get event log instead of writing to your own log file why not write to the real one yeah show them that um so you know we should tell i think we have a little bit of time yeah we should show them that and then tell them about the conversation we had last night which one well i was drinking so what am i supposed to remember okay so you remember that and okay so guys you've got this command right yeah show this let's go right [Laughter] right event log um log name and you get to tell us where you want to write it so let's say you wanted to application application is a great place for this to go and you know i'm going to i have to specify a source right yes and it is jason saves you money jason saves you it sounds like an insurance commercial at this point money um what else do we need uh just say uh say uh um nope no entry type uh information uh is information yeah which you minimize in like a script you'd probably do entry type of like error error yeah but this one's different where this t's up like some see and then say minus message message and then say quote uh script foo brand ran okay so you can put this into your script and of course fill in with variables for the message and stuff like that and so i'm going to run this oh event id we forgot event id um seven seven sounds good to me it didn't like the source name oh yeah you gotta register a source anyways it's pretty straightforward to do but there's an extra step here the point we had this conversation last night and jason was telling me how uh when he and don train they do training they do awesome training and they point out that hey as you're learning powershell um you're really back to this point of like you should maximize you you do your job and you do your job in a way that maximizes your integrated lifetime earnings now in rome ancient rome they had this notion that in order for justice to be done justice it was not good enough for justice to be done justice must be seen to be done okay so the public awareness that justice was happening was just as crucial as justice actually happening so too saving time and saving money is important being seen to save time and money is just as important so they were talking about how hey every time you write a script you should document the fact that hey this used to take this long and now with the script it takes this long and so we're brainstorming about that and then he said oh yes and at the end of the year you just go up to your boss and you say hey by the way chunk here's what i've done and uh give me the money no i just you know here's my value and we thought well geez you know well how do you actually translate that from like well i ran this script and it generated this much saved this much time to would that save over the course of a year and so we discovered we thought hey you know this would be simple if you just wrote your script and every time you ran your script the first thing you did was you first created the source and your json saves you money and you logged the fact that it that it ran then what you could do is say have a script that says okay i'm now going to go across all these machines i'm going to look in the application event log for that event id you know the json saves you money the source and and the event id and then grab that all up see which script ran have a table that said oh this script saved this much time and then actually do a calculation and maybe even do a chart and you do that on a weekly a monthly or yearly basis to actually be super crisp about how much time scripting and automation was going to save you and so anyway the point is it's actually pretty trivial to do and well the thing is and i have to talk to don about this because this is actually a really cool you're right if i was writing this to the event log that this script or this command got executed that you've built what you're doing is you're showing and you have a way to prove by using get event log by looking at that id you've got a way to show how your tool is being used that you've created this is good for informational purposes but it's also good for you to say hey look i automated i reduced the cost of human man hours or human hours down from you know i've saved you an entire person it sounds a little vindictive but it's time to eat your peers if they're not automating stuff if they're not automating and you are you're probably starting to automate them out you guys have seen the shirt you know and i think it goes something towards the fact of i can replace you with a script well yeah and that's kind of what's happening this is a great way writing it to the event log is a great way to keep track of where it's been run when it's been run how often it was used i think that's a great measurement stick and that will definitely become part of the conversation now that was brilliant last night you came up with that that was awesome well guys we've done some basic error handling for you you've got the examples and i want to show you where we're at in the slides because we're going to move on a little bit here and take a look at dangerous command let's but so air action catching errors with try catch carlos dangerous uh a better example i showed you a better example command list to automatically tweet to all your followers [Laughter] commander take pictures and tweet no oh god that's terrible so [Music] [Music] before we take a break we're gonna get started in chapter eight with tools that make changes tools that make changes like you you know but thanks yeah because i'm gonna make some changes so having said that somebody said what was that i think they misquoted us they said oh you know they it was a great quote i'm going to say i actually said it oh yeah yeah uh this course was it um i used to be a tool and now i'm a tool maker yeah yeah so out on twitter or something they misquoted one of us is hilarious used to be a tool now i'm a tool maker so we've been playing with tools that have just been retrieving information there's more to this if you want to make a tool that's going to change information there are a couple of things that we expect as admins when we work with powershell and let me just give you a quick example and this is a dangerous example so don't follow me through this this is a dangerous example so why am i doing a dangerous one so keep saying watch i know i know but get service stop service now you guys know what's going to happen if i strike enter right now it's it's going to my i'm going to have to restart my machine so when commandlets do things that could be dangerous powershell doesn't by default in most cases ask you are you sure so you have these options of of what if and confirm when you write a tool that's making a change you should provide these options too and so that's what we want to talk about yes so and it's in the uh snippet so it's going to be very helpful um let me open up for module 8. and made me thought of a great idea for a book when bad things happen to good computers when bad things happen to good computers so and and actually it's kind of uh fascinating let me show you the the two lines you will see this in the complete snippet as well but camala binding has some additional stuff so just take a look at my pseudo function here i'm going to call it set stuff i mean we're going to pretend for a moment that it's setting something kamala biling has this option called supports should process equals true support should process equals true that's so that we can use this if down here of should process this will give us our dash what if and our dash confirm capabilities on the commandlet so that we can ask it what are you about to do and i want to have confirmation on this but before we can actually show this to you we need to talk about this confirm impact level so i i think i have it in the slides but confirm impact is is one of these things that always has kind of made sense but i haven't messed with the impact level very much so let me get to the confirm impact level impact level here in the slides low medium or high and so the impact level is low medium or high and we've got the confirmed preference which is set to high by default which means if if a command has a confirm impact level of now okay so help me out with this because i i stumbled by that so basically each of the commandlets tells you what its impact is okay so if you're going to reboot the computer high impact if you're going to delete a user high impact if you're gonna stop a service medium impact if you're gonna change the description on a service low impact okay so it's it's just as rough and vague as that but you get it you know there are high impact things medium pack things and low impact things and the command let's declare that they say hey here's the impact i'm going to have on the system then we have these variables that say well hey based upon whether the impact that a command is going to have what should i do like should i confirm or not so by default i think confirm is high which is to say anytime something has a high impact we will confirm we'll auto confirm something um like i think restart computers yeah when you do like enable ps remoting is is a great example and so we'll ask you like hey but you can throttle that down you can say hey i would like to like you're just beginning powershell you're maybe a little nervous you're not so sure you don't want to screw up the server right it's important server so you might say hey i'd like to set my confirm impact to be medium and what that says is if you do anything that has a medium impact it'll come up and say hey jason are you sure you want to do that it tells you what it would do but doesn't do it until you say yes or no if you didn't do that it would just go ahead and do it you know what i think i can show that real quick let's show them is so take a look at my screen guys i'm going to show you the the default kern confirm preferences hi let's change that um let me just uh do actually get service like doing bits and we'll it's running so you want to do like stop service and you'll see that see powershell just did it right it doesn't so let me just you're a smart guy see we assume everybody's smart yeah and i love that about powershell um you should have talked to me sooner because you probably would have changed that um so i've got the service started again this time for dollar sign confirm uh preference let's set that equal to medium yeah and i think medium will be enough for this that uh this time we'll stop it and now it comes up and it says hey dude are you sure you want to do this or not bucko yeah now one of the things is is that that's what we're supposed to say no that's what we're specifying here when we say confirm impact level what i'm saying is that if you use my set stuff it's it's not going to do anything so detrimental that it's going to shut down your computer or anything like that so i set it to medium if i in my tool set this to high which the default is high then it would always ask every time you were running this do you want to confirm it but since i'm doing it to medium and i need to go back and change this confirm preference equals hi since i'm setting this to medium what's going to happen is if you try to run my command it's not going to ask are you sure or not it's just going to do it boom but because of support shoot process i'm going to be able to use those common parameters dash what if and dash confirm if i want to so let me just run this for you real quick and i'll clear my screen and i'll do set stuff you can see that when i just type set stuff oh computer name i made mandatory seriously i did that to the demo okay it says i'm changing something right now well now let's do this let me uh up arrow up arrow up arrow set stuff dash what if computer name yes what if performing operations set stuff on target dc so now i've got the option where i can say wait a minute before you do it what is it that you're going to do and i've now got the option to say you know what i'm going to run this against a bunch of computers and i want to confirm on each one of these just to make sure it comes up and it bingo lets you confirm is that kind of cool yeah i think that's really cool are you missing one i'm missing one what am i missing say yes say yes down verbose oh for bows wait did i see so um so what if nope nope just get rid of what if and say verbose what if get rid of what if oh get rid of what if and just save your bows there you go oh dc performing opera yeah so you this is really cool this is really really really really cool um i'm so used to using right dash verbose and thinking of it that way that i often times don't think of it this way yeah yeah yeah this is totally cool so what's happening is right verbose is telling you what it's going to do and then it just does it yeah so here's the thing this is like one of the cleverest things i think i ever thought about i'm just so so proud of this i got a patent on this thing seriously it's one of those things that's like one of those you know sometimes you know you get a patent and it's this big complex thing like you know and it's like tourist work and then but this one's just like this really little tiny elegant thought okay and it is let me show you on the screen here what happens is you call is an api called should process you tell us what it is that you're going to do and what you're going to do with 2 by the way so you didn't do it quite right ps commandlet dot should process by the way here's the technique so oh what lps coming out it should process anyway so you tell what you're going to do and what you're going to do it what you what you're going to act on and what you're going to do um mess it up seriously okay and let's make sure we do did that right zoom in zoom in thank you okay let's make sure i did it right set stuff minus verbose or sorry minus what if what if last computer name host okay so see mess it up seriously on target performing operation mess it up seriously on target local host so you tell us what you're going to act on and what you're going to do so this api you just do that and it returns a boolean and if it's returns true then do it if it returns false don't do it okay okay so you say what you're going to do and if it returns false don't do it if it turns true do it now when you say minus what if we print this out and we return saying don't do it don't do it false so here's that if statement so it prints it out and then we don't do this if you say minus most if you say minus verbose we print out a verbose statement and then we return true so that it actually does it do it oh that's awesome and then if you say confirm we say what do you want to do we generate the question right and if you say yes we return true if you say no we return false so this is an incredibly small piece of code that's incredibly powerful that and i and i i have to reiterate that that especially for me when i started doing this it's like okay i want to be able to have what if and confirm it's very important and oh my god this is going to be complex dealing with this and when i had first seen it i went seriously exactly it looks just like what the powershell team does and so my stuff looks and feels so that all admins they get everything looks and feels the same and i also had to do is put in a couple of lines let me show you a real example then we're going to take a break here in a second and the key thing was you need this support should process up at the top here you see that yes yes that's the thing that tells us that's the thing so if you don't have this you'll see a common set of parameters if you add this you'll see that common set of parameters and then you get minus what if and verbose what if and confirm confirm okay and the other thing i want to point out here is that this is super super important i once dealt with an admin who was saying you know every now and again i get to the point where i'm about to do something and i realized if i get this wrong i'm about to hit carriage return if i get this wrong like it's over you know the system's going to be doomed my career is going to be in the toilet muffy and buffy are not going to college it's going to be bad and if you just think about that that's a lousy position to ever put someone in i mean it's just not it's wrong and so this calling should process alleviates that it puts them in power they're going to do something risky and they're taking responsibility and they can say what if and they can know that it's going to be safe and secure they're going to know what is going to happen or if there's a bunch of things they can say minus confirm and double check i mean it's really just really it's a morale thing a moral thing to do because think about this guys if you're making tools and your your tool is changing something you need to give me the ability to go wait a minute maybe i don't understand what's happening here and i can do what if and confirm i mean it's something that is is is just you have to do this if your command is doing anything that makes a change and so here i gave you an example of this is one of my fun ones set volume label oh cool so here's a command support should process true gotta have that confirm impact it's medium it's just a volume label what do we care um i've got a computer name and a label as parameters and here you go i'm gonna put in computer name label change to whatever that label is and i've got a wmi class in here that i'm changing the label with so just so you can see what this looks like when it runs if i remember how i set this thing up when i wrote it it was set volume label what kind of computer that's theirs what's that what you got theirs did you get yours oh sorry nope never mind this isn't it so dc i'll just shut up no no you're good you're good did i happen to put a i didn't put a get in here so i'm going to set the volume label on drive c as as what i set up and i want to do the label of cool are you showing a computer a computer before your character turner yeah are you showing um the file explorer to show the label whack hi how are you doing no i didn't show them file explorer to show them the label um yeah because that's that's the way you can see it right so so oh man now so let's go out here to see oh wait a minute this isn't the machine i'm doing it to okay but this will be the machine so there is no label right there's no label i'll do it to this machine so let me change this to dot yeah let's change this to dot which is my local machine now notice i'm going to do what if because i want to know what's going to happen so i'm going to perform the operation set ball label on target well my local machine label change to cool stuff well let's go ahead okay that's exactly what i want to do bingo oh i love that so now i didn't actually put in a function in here for get vol label or get volume information but i i could have but we'll just do it quickly back out through here oh lookie cool stuff in the extra o's so any time you make a commandment that's going to change something you should be putting in support should process and you should be putting in this if statement and you should make sure that you've given us a way all admins away to use dash what if and dash confirm yeah folks it's about time for a 10-minute break when we come back it's our last last session and you know we've been making tools it's now time to put a bunch of tools together and go from this script way of doing it back to modules putting them into modules and creating module manifests and maybe even creating a custom view so it's worth noting rick kloss never showed you how to do that yeah it's worth noting now that we're showing it never showed you and so when we get to that just remember it was us that so anyway see you guys in 10 minutes [Music] now this is our last our last bit and this is this is taking all of the tools that you're making that you've been you've been testing in your scripts and you've been building them and now you've seen how to put help and all the good stuff in now we're going to turn them into mods now it's it's formal it's safe you're no longer embarrassed about it right but you got to share it you're right now you got to share it and how do you want to share this well you want to share it the same way that microsoft shares it so we want to do it with modules so module 9 is script and manifest modules now if you joined us the last time we did this briefly but there's more to it and so we want to show you some more about it but we're going to start you off one piece at a time first of all here's what we're going to do putting your tools in one place creating a module manifest we're going to take maybe a look at a little custom view action here too that'll be fun and oh yeah that's set up yeah so putting your tools in one place look guys you've got this in the slides but it's pretty simple we want to create a module and i want to show you how to make a module and we're going to start off simple and we're going to make it better and better and better first of all let's make it really easy so let me do this let me launch my scripts in module 9 for this take a look at my tools.ps1 and what i've got is i've got a few things in here actually let me collapse the help down i've got a function called that we made earlier get comp info um i've got my set volume label and so and i've got i think a couple of other ones oh yeah great to get jason fun one and jason fun too so i've now got a collection of uh functions that i want to be able to let other people use and i want to use and i don't want to have to run this script every time i want to use these functions so we want to make a module we're going to start off with how easy it is to make a module so watch i'm just going to do my tools ps1 file save as i'm going to do this in module 9 and watch psm1 oh the strain of it all the strain of it all and this is why we get paid the big bucks right uh we just made a module so watch i'm going to open up a new console and i'm going to go out to well actually i'll just do it right from here import module c colon scripts module 9 and i called it my tools right not ps1 but psm1 so i've now imported the module awesome get command dash module my tools there are the commands i can run help i can get help on the get comp info i can now do get help on and see all the help there's all my commands i can run these this is great this is cool well this isn't that cool though when you get down to it because i well it's it's very cool i mean i made a module my tools but i have to i have to import it every time and i i that's you need virtual modules i i normally just use an award again we i need virtual modules i need dynamically loaded mod imported i need virtual modules is what i need so this is where we're going to have this conversation again now look i've given you guys this launch ps1 and what it does it shows you if you need to manually launch a module like i just did you can do it get command module my tools so you can see all the commands that are in it and if you edit your module one of the things you have to remember is you've got to remove it and re-import it in order to test things that are in it so i've given you an example of that as well but here's the thing i'd like to put the modules in a correct place to put it and if you joined us last time you saw this and i'm just going to do it this way i i was doing ps module path oh wait was that no i wasn't doing that i was doing dollar sign environment ps module path and here's the thing and and jeffrey pointed out why don't we make this a little bit easier to read so i i gave that to you the the the the jeffrey snover trick um and so what i'm going to do is can i just do split on that yes yeah and semicolon oops he wants it in yeah quotes uh quotes so there are two paths that by default uh that we're looking at with this current logged on user so we've got the system 32 windows powershell 10 modules path and if you joined us last time you heard to say don't put your stuff there that's not for you matter of fact let me show this to you visually through file explorer this is where powershell gets installed windows system32 you guys all know system32 and i'm going to go down here and show you where windows powershell is version 10. here you'll see that and we're going to come back here a little bit but you're going to see here's where the executables are for power shell and here is this folder in here called modules and this is where you should not put your stop don't put your stuff here this is where microsoft puts their stuff here this is a sacred place don't mess with the sacred place i think you're doing this wrong why do you ever see that kids in the block skit where the guy says he says what did my mother say about salt and ice don't put salt in your eyes don't put salt in your eyes put salt in your eyes what's on your eyes it turns out there's some psychology behind this it turns out that that as you as you as you teach someone something right and and use a negative right memory goes into short-term memory and then it makes a transition to long-term memory it turns out just statistically that negative things uh do not make the transition to long-term memory as well as positive things therefore they found if you have a safety program that says don't forget your safety glasses literally what happens is people with the first time they hear that they've hear don't forget your safety glasses but as they remember it they remember forget your safety glasses no seriously so uh so the way you do this is you always say the positive thing where are your safety glasses oh okay so instead of me saying don't put your stuff here i should be saying let me show you where you do put your stuff yes exactly so and i'm going to bring up show this to you graphically but let me just show you this is where you want to put your stuff do put your stuff under documents windows powershell modules and i want to point out that by default windows powershell modules does not exist under your documents i'm going to show this to you graphically although i do want to show you i i i i gave you guys uh kind of a quick a scripting way to do this somebody yelled at me well you showed it to us graphically you should have showed it to us in powershell okay well here's yours okay look at what i'm doing okay okay um dollar sign module dick van dyke it's like big man yeah i know yeah actually that it is might might might so i'm gonna i'm gonna run this guys i'm just gonna show you what the code does and i'm going to show you that the path that's the path that we want and what i've got here is module path is path 0 plus my tools what this is going to do is let me show you what module path now looks like and what why i called it my tools here's the important thing under the modules folder you need to name the folder that's going to hold your module the folder name and the module name need to be exactly the same for right now and so for right now i'm going to call the folder my tools and i'm going to call my module my tools and so what i've done here is how you do this in powershell is you can use new item i'm just going to give it that module path and tell it that it's a directory so it created it and it created the windows powershell modules and it created the my tools all in one file swoop and then i'm going to copy the my tools psm1 to that destination so lo and behold i didn't want to let anybody down from the last show i just showed you how to do it in powershell but i'm still going to show it to you graphically it's the reason i want you because i just want you to get this this is under users and it looks a little bit different i've logged in as a user in a domain student.company it's my documents when you look at it visually there's the windows powershell there's the modules there's the folder my tools and there is my module now this is still pretty simple right but since it's in the special place where you do want to put your stuff in virtual module please we're going to get in trouble because somebody has got the virtual module place there's an mvp probably online right now going you dare say that again um since it's in the special place watch what's going to happen is uh let me close off all those and let me open up a brand new one so you know i'm not cheating by the way i i could be wrong but i think i get to do that you think you you know what i do you guys just hear what he just said uh that's that that's hilarious he made up the virtual module thing but he's actually allowed to do that that's the best thing about being the architect oh that's great now wait a minute i'm pretty sure that i made up a term and gave you a term for for a particular thing that that you're working on and and you didn't like the term very much could the the no no not the chihuahuas the truth that was that that was good it was it was it was no no kimbles was good uh my wife loves camelots um the um i'm just establishing the track record here oh that's what it is yeah it was it was the dsc thing when i came up with my don't no don't call them providers call them something really stupid and the choo-choo is and you went are you good at training then maybe you should stick with that because so i don't get to make up terms but he does so guys you know what's going to happen here at this point i've just launched powershell since it's in the special place right now that means it can be dynamically loaded which means all i'm going to do is i'm going to do get comp info and i'm going to hit tab oh look at tab completed i don't have to import the module manually now this is a beautiful thing in version 3. i'm totally excited by this because this makes me so happy and boom there we go so this is the simple part to making modules in a nice simple way but let's do something here let's take a list here get module list available and we'll see if we see my module in here what i call it my tools and for some reason bottom bottom oh bottom bottom bottom bottom bottom bottom no i don't see my module what did you do oh i did a get module uh list available i expected to see a script module in here but i don't see it for some reason i don't know what i've done wrong but i don't see it listed let me see if i can get it this way get module my tools okay so this is what i want to show it's a script module you can see that the module here my tools and it'll even show you what commandlets are available in it so it's a nice way to see it now this is the interesting thing this is a nice script module and this is great but and i need your help with this jeffrey and i think i have a slide with this um manifests yes let's talk about what is a manifest and why do we have a manifest i mean what's the purpose to a manifest okay so in reality a module is a directory of something that either has a a psm1 file or and or a manifest okay and the manifest tells all the things that are part of that module and so you can include a set of modules you can have some sub modules by the way so this is our modules are unlike some of the other people's modules we looked at the way other people did it and we said yeah yeah that's really cool and then we thought about it's like no actually that's not very cool so and and so we ours are much more sophisticated than theirs no so so imagine um you take a dependency on my module i'm going to import jeffrey's module now because it's a an ecosystem i do i look around there and i say oh this guy over here joe has a cool module so i'm going to include it in mine okay just as like helper stuff to deliver my functions now when you using the other guy's stuff the way the typically way to do it because it's easy is when you include me you also include them right and so then and it's actually kind of hard you can't really tell whose are mine and who's of theirs you just get them all okay so then you might take advantage of mine and theirs but then later i go and i said you know joe he's actually not a very good programmer i'm going to take advantage of tom's stuff and so i'm going to use tom instead of joe's and tom might have a different set of functions and so i use them now you take import me new version of my stuff and you break right because i was still using tom dick or harry whoever we had over there the bad guys the bad guys bad programmers okay and so this was what we called an undeclared coupling okay that you coupled to there and so our modules allow us to say hey uh uh here's my module and i'm gonna include all these other things and i'm gonna be very intentional about the things that you see and the things you don't see so there you go got it and so that's what these manifest do they they do lots of things that's part of what they do they include other things to include now just be clear manifest can be just the script file or can be the script file and formatting files like type files and view files we're going to talk about that in a few minutes there are processing that you can do as part of the manifest which is to say hey when you start do this initialization and you can have a ton of data like a data file okay by the way so this is interesting you might we're going to talk a little bit about version four oh four version four it helps motivate manifests and modules so here's the way version four we have this cool new feature called desired state configuration desired state configuration says i just declare the world i want to be i push it to a machine and it makes it so good deal right okay how does that work well it works and so literally when i push it to that machine a wmi provider grabs that document and starts looking at it and says okay what are the resources articulated in here and how do i go make them be that way and so i take a look at resource number one and i look for wmi provider if i find and i say hey wmi make it so makes it so i then take a look at the next one and i say hey is there a wmi provider for it no hmm okay well is there a powershell provider for it yes okay call and now powershell provider is a module that has three well-defined functions get target resource set target resource test target resource and so it has i take a look at the resource name and i look for a module of the same name and if it matches then i call it and it makes it so yeah yeah now i then take a look at the third resource and i say do i have a wmi provider no do i have a powershell provider no what do i do and the answer is you can go and you can register a a depot and we'll go to that depot it's basically a rest api an odata api so this could be linux if you wanted it to be we call this rest api and we say hey do you have the wmi provider for this now at this point it's a powershell module that's zipped up and we'll go and we'll grab that thing and we'll deploy it and then we call the provider we call this virtual providers no virtual modules virtual virtual modules yeah it is the page fault in the module so we'll bring in the provider and do it now here's the thing my provider is is this module that has a script that has these three functions but i mentioned that the module can be as large as you want it to be so imagine i said in addition i can out of the box i can do things like on that machine i want iis and i want these users and these groups that sort of thing but now imagine i want to be able to say i want to have sql with the following attributes set well the way i can do that is i can put sql in a module like take this the kit create a subdirectory okay put the repository in there have a script module that says oh when to set it take this thing and install it install it yeah and then that's my module so now when i go when i say you you you're just a bare bones machine and i say sql should have the following attributes it goes a sequel what's that thing wmi no powershell provider no hey you you got a sql thing yes what do i have a multi-gigabyte thing it can be as big as you want bring that thing down deploy it call the script script says oh take this kit deploy it so that's some of the power of the modules and and putting a manifest on your modules helps describe them and also a few other things as well so versioning versioning so take a look at the slide here a couple of things a manifest has an extension of psd1 um the file name needs to match the folder name because remember there was ps1 ps1 powershell one that's easy then there was psm which is for modules module d wait a minute manifest d manifest d data data yeah psd files are data files so data files are a subset of the powershell language uh used for expressing data and so it doesn't give you the full capabilities of it but it's also a constrained language that's very easy to uh uh secure and as a matter of fact it's not a security boundary let me just say that really security boundaries a formal thing it's a security feature apparently there's a difference between those two apparently there is because people get awfully upset about those so the in this case if you're going to use a manifest the manifest name has to match what the folder name is because you can include so many other things your psm ones your all these other things that don't their names don't necessarily match and they may be in different locations now just to let you know that um if this was uh uh manifest when powershell loads it could be looking at a binary module like my tools.dll or it could be a script module like we're creating in here psm1 um the command to make a manifest is called new module manifest and you can specify a ton of stuff for your manifest as a matter of fact let me show you i gave you guys an example of doing a manifest now let me break this down i didn't want to have to type all this out for you so i want you to oh you know what's a better way to do this what's the better way just to kind of give them a feel for everything do um um well you know about uh new manifest right new new new module manifesto so do show command on it oh okay you want to do uh let's see new module manifest show command show command oh show command okay show command space it's late in the day i'm not even listening to you what the hell's the matter with me slash space space new module manifest remember we mentioned that show command is just this awesome tool to explore and this will point it out so take a look at this this is path which is notice the star is required you have to do a path and you can kind of see this with the example that i have up here you've got to have a path um aliases that you can export out this gives you a nice flavor for all the things you can check this out clr version so you might have a manifest that requires a different version of the clr right so you can declare that here you can specify that what i like is commandlets to export so maybe you've got some things that are just helper functions in that stuff and you can export these you can give the normal stuff like author name and company name copyright information which is kind of what i started filling out up here for you is a path and you can see i'm putting it in the right place called my tools i put in the author and company and copyright this version of this module and we can go down and do description uh net framework version requirements now we're going to show you here formats to process here in just a second oh and then there help info uh yeah remember we mentioned that if you wanted to have formal help that's right and you want to have update help this is how we do remember when you do update help you say what modules you want to update the help on so update helps all about modules and what we do is we find the module we open it up we look at the psd file we look for this help uri and if we find it we download it and if we don't find it we say what are you talking about so this is actually really cool nested modules nested modules yeah tom's a crummy programmer so i'm not going to i'm not going to show his stuff to you what powershell version is required there's what processor architecture for this you want oh and then some of them this one's interesting powershell hostname so we have quite a few hosts that are uh like will only work in ise right like actually let me see let's see if i get this right so let's see get command new uh what was it ise snippet okay that's in the ise module and i bet you if we looked at that manifest we would see that it because these snippets only work in ise in iscs and so if you tried to run them somewhere else actually i have no idea what would happen let's i could embarrass myself here yeah let's see new all right import module ise bigger it can't be bigger sorry oh zoom zoom it can make it bigger and there it goes [Laughter] where'd he go come back oh something bad happened oh i never should have listened to you what bad production guys stay away oh did that work go on i don't know so apparently it's not correctly so apparently it's not yeah no apparently we're gonna let you do it right from the console no but that's good where you can isolate it down to a particular host like the isc yeah and in this switch to him yeah this in this case required assemblies a lot of guys uh bring in dlls from the dotnet or from something else i know i have a friend that works with exchange and there's uh some additional exchange assemblies dlls that he wants to make sure get loaded for his stuff to run so you can do that um how did you just find a bug that i i checked and the modules the module does not have that set up there you just found a bug don't say that yeah dude okay oh we'll put it on connect and then yeah and that's what you do when you find bugs that's what you do you put it on connect um root module and in this case you can see that i've used root module to specify my module my script file so let me show you what this looks like i'm going to run this and then we're going to open up what this manifest is so you can see it so i'm going to select all of this and i'm going to make my own nice little manifest it says that it made it and what we'll do here is uh um uh what do i want to do here i want to show you this module manifest so let me just do this um where did i put this bloody thing oh out at this path that's where all the way out there let me uh copy this path here real quick and let me clear the screen do a quick dir you can see i've got the psd uh file in there and what i want to do is i'm going to actually open this with the isc so ps ps what was i trying to open now i can't even remember what i was doing my tools psd and so take a look at this my tools this is the actual manifest you can see what it generated you can also see that you could go in and create a manifest that you like that's kind of generic that you could then go through and hand modify so if i wanted to change the module version i could run that whole manifest thing again or i could just go in here and change the module version we do assign a unique guide to it and there is a way to create new goods right from the the command line but as you can see i can go through this file and add in or make changes to this manifest in this case this is making my module more proper now that i have an official manifest but i want to show you something that a manifest can be useful for other than just putting in my name and company take a look at something here i've got i'm going to call up powershell again and this time i'm going to do the exact same thing i'm going to do my get comp info and i want you to take a look at how the screen is i'm going to do get comp info and i'm going to say computer name dc notice that powershell has put up the properties that i told it to put up and stuff like that you know it's it's done it in a table and it's make you know you can make your own view in other words you can control through a custom view that you assign and use with a manifest what you want the output to look like now i've got to be honest with you this is a little bit advanced and sometimes considered a little esoteric i mean i don't need to make a custom view because i'm going to let the admin do a select and property star or choose whatever properties they want which is kind of the proper way to do it but maybe i do want a default view because there's certain pieces of information i don't know if you guys have noticed this but here's one of my favorite things get service when you do this you guys know that there's more information about a service than these three things have you ever wondered where that is controlled at or get process look at get process oh i love get process who decided that these are the things that i want to see and that i want to see them in a table who made that decision and where is that decision made i say i did [Laughter] i made it a problem with that so this part of the recording i'm going to use for every class from now on because when i go who made this decision if people are going somebody at microsoft he did so so if you don't like it don't complain is what i'm saying let me show you i don't like it let me give you bruce pants let me i'll have him tell me yeah really let me show you where the default custom views are and there's a reason i'm showing it to you before you go into custom views i'm just i was really bothered by that that that bug it was a bug so what happened yeah what happened was that they in isc that's ise the module is called ise it only works in the isc but they had not added powershell host files so i went and added it i made a copy of this and i added it so here's what should happen so when i run it i import it and it tells me yep here's all the things i'm doing but now okay by the way so so let's make sure let's see so cat cat this file sorry it just really bothered me okay so notice i'll say powershell hostname powershell isc host by the way if you ever wonder it's dollar sign host and there's its name windows powershell ise host yes now here if i say dollar sign host it is that console host so i went and i fixed that that i fixed it at this that that code wasn't there oh it wasn't there at all it wasn't there oh yeah and so i just went and i added it now what happens is i'll say import module dot slash ic and it says look at this the name of the current powershell host is console host the module ise requires the host windows which is exactly what it should do so not only could we go on connect and put the bug in we could put the solution in and it should just magically happen right yeah so by the way this is again one of those things where you declare what you want to have happen and then powershell engine does the work for you standard error message that gets localized et cetera so you don't have to otherwise you'd have to write the code that do does this or you have to generate the bad experience and i'm sorry to interrupt your flow but that was just like no that was actually really important because i mean first of all it shows a couple things one if you run into something that you think's a bug go out to connect and file it yes please two you know there can be some bugs on some things that aren't necessarily working this way and and you just sat down and figured out and figured out what the issue was and bing yeah bing now we're going to have it fixed so so let me just show you guys this view is controlled it was decided by jeffrey and whoever let me show you where this view is actually stored these views and i are out where powershell is installed so again i'm going to go out to don't be moving things i'm going to go out to oh windows system 32 i spent a lot of time out here positive way to say this is ensure the sanctity of the following locations ensure the sanctity of the following locations [Laughter] so um i'm going to go into net types notice see this ps1 xml this is where the views are and i'm going to open this with notepad don't necessarily if you're playing along at home don't do this because i want to show you i originally got into to looking at the views because i wanted to change the view i wanted to change what get service was doing or what get process was doing so i was curious so i started going in and looking at the views guys this is xml so it's kind of confusing if you're if you're brand new to this but let me do this i want to find find process this is the one i always use not process module but process so let me show you an example i don't know if you guys remember but i'll show you real quick this is the type of object that is produced when the get process command that runs so if you do get processed pipe to get member in the first jump start this is very important to us to know so we know what object it is and now down here we actually have look handles oh well you know when i do get process look there's handles um and well now i just lost my notepad and then notice they specified a width for it what the alignment was the next column header in pmk they specified oh look npmk they specified the width a right alignment oh yeah that's right align this is where the columns are defined and further down from the columns these are all the column headers then what fills the data and something should look familiar to you if you were in the first jumpstart look how they're filling the data they're filling it with yeah we made custom columns just like this we did it in flight or by hand in the first jump start but they've just simply written in how they're filling in the data in here well this is totally cool so i was thinking go ahead you just exposed a deep architectural flaw that i'm hugely embarrassed by no no no don't say it no it's true yeah so this was just a mistake an architectural mistake i made a mistake we should never ever ever have allowed uh script blocks in the formatting files and so i encourage you to never do that uh we have not remedied that i'd love to remedy that here's the basic problem again we're trying to give you a world where you think type get okay so if you take a look at the get process in general what we want to be able to do is to say that all the column headers are things that you can program against and if you go type get process pipe to npm or type two uh select a format table npn and p npm i think mpm yeah nothing okay i think it was an n but even if you yeah even if you spelled it right it wouldn't have worked because we calculated on the fly and that's just the wrong way to do it the right way to do it and sometimes the reason why we did it i mean it was just mistake right uh well far it was an architectural mistake i never should allow that and because and what happens is sometimes you have these property names that are really long or in this case you want to divide it by a a a 1k or one megabyte or one gigabyte uh and so you want to do that the correct way to do that is you change the type system and not the formatting now the reason why this is such a deep architectural flaw is that when i go to that machine we use you maybe have seen the implicit remoting i think we showed that yeah last time when i go and i grab the implicit remoting i will grab the formatting information as well okay i'll bring that over and i'll run it i can never allow code from another machine to come over here and run that's no no no i take responsibility that says when i bring this over it's safe that's like jeffrey snover take the badge out i it's safe um and if it isn't i'll fire someone [Laughter] which we went through in the first jump start so no and so it's safe and so i can never allow that script code to be there because a bad guy might have written a formatting file with evil script code that then reboots the computer or steals your information so i can never do that so when i bring that over we look for it and we throw it away and so the formatting will be different if you use script files in remoting so anyway that that's a little bit of background i thought it was worth no definitely and so i what i'm about to show you is something that he considers to be a mistake so let me show you originally i went in here because i wanted to change the default views but guys don't do that because at the bottom of this file you're going to notice that this is not digitally signed and so you're going to break the file if you if you do that you you'll break the file so what i do is i usually go to the process section and i copy the process section and use that as my own template which is what i did for you and here's just so you can see a custom view okay so um yeah i thought that was pretty nice um let me go out here and show you so here um uh with a view oh wait a minute wait a minute oh i didn't show you where's the view where did i put the view oh let me show you the view i gave you it's it's it's out here wait a minute wait a minute that's what you say apparently i'm i'm full of crap now wait a second i know the view is out here hang on hang on hang on i know the view is here let me just do this um was this scripts uh module nine because i wanted to show this guy i show this to you um with feet i did give a view of you so here's the thing i called this view that i made called jasontypes.format.ps1xml now hang on before everybody starts yelling at me i want you to see the view and see what it it looks like because i copy the process block section and it's xml and so i just copied it and then i just created what i wanted as a default view i just did computer name and i did free space as column headers and down here i did computer name as the properties and just the free space now i'm not modifying free space i'm not doing it to making it one gig or anything like that i just wanted you to see an example of a short version so if you wanted to create something off of this you could and so what i need to do is attach this view now to be honest with you there are commandlets that i could say attach this view import my module and attach this view but every single time you opened up powershell you would have to update and reattach this view one of the beautiful things to a manifest is the manifest will let you do it so yes yeah let me just show you here's um uh all of my stuff here and oops uh a manifest with a view so what i'm gonna do here is this is the same manifest file i've done only look formats to process and i'm going to give it the name of my view but i haven't shown you something that is very magical here yeah you did a trick there there's a trick i want to show you there was a modification that i made in my function so my view let me show you my scrolling down capability i just lost down here when i make a new object type name ps object and i gave it the properties lookie what i did i did i inserted my own type name does that work jason dot inventory uh what do you mean does that work yeah that works you don't think that works jason we're gonna find out we're gonna find out no i don't think that'll work you don't think that's gonna work let's find out no oh okay um well let's do this okay you're on you're on you're on so now he's pulling out his money so the first thing i'm going to have to do is i kind of create this new manifest and i'm going to have it use the new view that i'm using so he's he's got money oh god i'm pretty sure so where's mine [Laughter] so i'm going to run this and i've created a new manifest and i think what i have forgotten to do is copy my view out so um uh um actually no it should work right from there because i didn't i didn't give a path to it but i just exploded with confidence okay so let's go back to this oh that's a ps1 i'm going to save this ps1 as uh oh that's what i'm that's where i'm going to screw up no i need to have this as my tools that's fine whatever oh wait my tools psm1 yeah you're good i don't think i'm good because i don't think i have it modified over in in the uh my tools okay fine change the name that's not where you're screwed up keep going oh that's not where i screwed up okay so let me let me go oh don't do that this is the safest dollar i'm ever going to make in my life file uh open uh that's not what i want to open i want to open hang on let me just open and modify this real quick guys so that i can see if i've gotten close and i can earn a buck out of this windows oh no that's we did it under users uh student company oh no no no no my documents windows modules my tools i need to modify my psm1 let me just modify this real quick so that you can see i'm going to put this guy in and go down here and modify it [Music] and save it and let's check my view look at my view my view here that i have is jason dot inventory object so that's how i'm identifying it and lo and behold we're gonna about to find out if this works let's open up a new powershell console get comp info oops wait a minute get compensate get comp info uh-oh uh my module's suddenly not loading well let me see what i did with my manifest hang on guys let's see path uh my tools ah i gotta copy the view out there that's why the manifest isn't loading um let me copy my view out there real quick i'll tell you what why don't you show them that while i show them something else okay okay so i'm going to show you something amazingly cool okay so um views now when you type get process yeah you see the the view and what happens is we pick a default view and we show it to you but it turns out that there is this parameter format table by the way so it's format table and you could have done format list et cetera okay perform a table i can say are you all ready i was watching you i could say minus view and i can specify a view and the question is well what are the views well there's a couple ways to find out but the easiest way to find out is to go something like that right and and you get an error and says ah that the view blah blah blah cannot be found specify one of the following table views and try again process priority start time so let's try that so clear system and here we'll do it for everything i said priority priority and look here oh i like that yeah right it's a different view now notice this is showing an issue you have to be able to sort things first by priority through through sort priority class and there you go that is cool anyway so i did that wrong priority or sort it by start time format table nice view start time and here oh we don't have one and these were all started oh you know what i rebooted right before right before the session so they're all the same start time but uh it would show you the different time days that they were started that's awesome yeah that is actually really cool so i think i got my wagon fixed so let me show everybody here real quick so my module under my tools here is the manifest here is the view and here's my script module so i need to make sure that they're all copied out there and just as a reminder so i didn't you know lose you on this i'm going to do a i'm going to run a new manifest for it create a new manifest and i'm adding formats to process and i'm specifying the name of my view now the view gets tied to my particular function let me show you because lookie json.inventory object i added that as an object type and look in my view my view has type name json.inventory object so if i've done this correctly and i win the magic dollar he hasn't i'm going to launch powershell by the way i'm wrong all the time so i'm not going to be impressed if i get this wrong let's see if i can dynamically load the command letter the module will dynamically load comp okay i got that so computer name and if i did this right remember i only had two things in the view computer name and free space let's see what happens oh god oh i win the dollar i win the dollar i win the dollar i i totally win the dollar that makes no sense would you do me a favor would you autograph that one because it would get you a penny makes no sense at all what do you mean that makes no sense show me because you did it wrong i did it wrong okay go to the code um go to here here's the module okay here let me show you here's where you got it wrong i i do not know how that could possibly have worked okay that's sign x equals gps ls assess okay so here's the thing if you just do get type get type we tell you the dotnet type okay that's very uninteresting make sure you guys switch over guys over to his machine because we can't see what he's there you go though okay so there you go so there you go i'm showing you the net type that right powershell does not work on the dotnet type system it works on the powershell type system which subclasses the dotnet type system we do everything based upon ps type names okay so this is an array starts off with object marshal by ref component and then system.diagnostics.process and there you can put any object you used ps type names that almost certainly should not work so i'm not sure how it is that really oh okay well i'm not certain about that either because that's that's the way so perhaps perhaps there's something tricky with the ps object that i'm not familiar with but as a general rule that will not work you have to say dollar oh by the way so here's the way this works so this ps type names sorry ps type names it's just an array of strings and you can say insert insert insert and zero and then anything you want right which is pretty much what i what i was just doing right exactly and then and then that is the key and by the way that's one of the reasons by the way so we use this string to be any kind of uh uh uh you know to extend everything we use it for views you can dynamically add properties to this etc so if you've ever done this get get event event log minus log log minus blog name say application minus newest let's just say uh three pipe to get member notice you're going to get three different things three different things and why is that and look here that's the type and so that's a little bit odd so let's just take a look at one of those things sorry let's take a look at one newest one i'll sign home dial sign x equals get so x dot ps type names and look what we did here is the dotnet entry and then we synthesize these things right and what this means so we tell you it's in the application log in the source and then we tell the individual id and so what that means somebody the other day asked us hey could you uh uh you know how do we parse the strings in there and the messages and the answer is you can dynamically add properties register them for this log for this log source or for this law particular log entry and then you write the the parsing once and then just access it oh okay there you go so anyways guys on um on the uh code sample that i have in here that uh where i've set it up is ps object type names go out to powershell.org and you will see a blog post in a couple of days and we'll come up with a better way for you or the reason for this that kind of thing and so we'll we'll give you more information on this as we go out there but is using it as a manifest example of adding a custom view you've now seen that you can use manifest for a lot of things and just as a custom view as an example that's pretty good oh you know what they're and jeffrey they did it again did you look at the look at that remember at the end of the the first jump start look at what they're doing what they did they're trying to remind us so if you guys were at the first jump start you got to watch the end of the first jump start the production crew back here thinks they know something about powershell so they put this teleprompter up to try to help us understand that they want us to remind you to do the survey that's below the video and make sure you do the survey you give us the highest score that you possibly can because we need to beat rick claus and that's what we really need so do that but you have to see they wrote it on the screen as what did the do tell audience news survey with a cut actually that was better than the last yeah much better than that yeah that's much better than the garbage you did the first time yeah the first one so so even the production guys have it down by the way i found out why i was confused you did it all sorts of wacky let me show you his code oh sure sure here's here's the thing that one that confused the heck out of me so so this is just the wrong way to do it oh but it works so you still win the buck so yeah so that's all i care about ps object and object and then he called the ps object i don't ever teach people about that there's rich stuff instead what you do is you just go to here and you say ps type names that's the way oh okay that's the best guy screenshot this at home and and look at this because he's shortened it up and made it the correct way so there won't be a blog post there are multiple ways to do it i learned one i never i never would have thought of doing it that way well then i'm gonna start thinking about doing it that way so uh thank you sir that's awesome hey you know what it's this is funny let me catch everybody up but custom views uh you've got all of the sample code everything questions or comments you know what i don't we've uh we've run to the to the end of what yeah this is can it be no seriously seriously this is this is yeah blinky lights everything amazing how 16 hours just goes by like a flash so folks what we did today is is we took you through getting started with scripting and using that scripting capabilities to start making tools right and turning those tools into something that's useful that you can may solve your business problems with using some best practices putting in help getting in parameters validation for the parameters error handling so that you can do error handling and then bundling all of that into modules and creating a module manifest even regardless of how my custom view went off the you have the whole module manifest kind of thing so this is where you get started on this and this is great stuff to practice with and keep in mind at the very beginning of the show you have the slides that we gave you some additional resources to go deeper into into the tool making the don jones learn windows tool making or learn powershell tool making in a month of lunches and also there's a new class that's coming out from microsoft on the microsoft marketplace that does tool making if you want to take a class on it so this is really cool stuff and you can get you know we've only had one day to go through this there's a lot more that you can do but you can also see that as an it pro this is something that's easy for you to learn to do and you should do it and remember you know we started this saying hey each of us really has two jobs the first job is to get the particular task you have at hand done but the second job and you really want to pay attention to that second job is to solve that problem in a way that maximizes your integrated lifetime earnings and by learning how to do these scripts by learning how to do tools you integrate your maximize you maximize your integrated lifetime earnings so you definitely want to be doing that so remember don't be a tool be a tool maker okay great and on that note do you want to send everybody out with the the the the the the sign the system oh yeah yeah yeah okay so you want to give them a little story or no sure yeah go ahead well this is a we have a powershell summit we bring people in we like the world's best powershell people we bring them in and we just geek out we have a blast and somebody i don't even remember who it was came up with the powershell gang tag that's right we're a gang oh yeah we back yeah here's the gang shine it's a requirement you have to do it when you go to the summit it's it's powershell represent yeah represent so guys i don't even know what that means going forward angle bracket cursor type there's no typical processor this one yeah so guys going forward make sure you practice this stuff um come out to powershell.org in the forums and ask questions if you have questions and you'll get awesome help out there and once again that you mention powershell.community.org the powershell powershell.org powershell.org powershell.org is is replace powershellcommunity.org so kirk monroe all those guys so come out to powershell.org and ask your questions and so you can get some continued help and you move forward with this is is so very important and you too can make tools and you know once again i want to say it's been a true honor to be able to do this with you right back at you dude thank you very much this has been great thanks a lot jeffrey you guys take care and stay in touch with us we'll be out there trying to help so you let us know [Music]
Info
Channel: Nerd's lesson
Views: 178,903
Rating: undefined out of 5
Keywords: a powershell script example, powershell basics, powershell commands powershell scripting tutorial, powershell tutorial, powershell scripting, learn powershell, windows powershell, powershell commands, powershell scripting for beginners, powershell tutorial for beginners, powershell for beginners, powershell automation, powershell course
Id: K4YDHFalAK8
Channel Id: undefined
Length: 360min 55sec (21655 seconds)
Published: Sun May 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.