Transcript
Yep, I'm just cleaning up, getting us ready to go here. We're going to have VS Code open, but you made some packages. I made some packages for Josh. I cleaned them up this morning too, Josh. I literally made like six or seven. So I was like, I don't know which ones Josh is going to like. And let's remember, this is day 24 of me trying PowerShell. So let's lower our expectations, first of all. No, we're not going to lower our expectations. You are learning something from zero to one, and you're doing an amazing job. Thank you. And you're doing it very publicly. Yeah. That's great. It's really hard to put yourself out there. So what were the things that you saw? Where do we want to start with this thing? Okay, the problem that was in Discord was Fortinet. I think it was actually a newer user. I'm not a hundred percent sure, but it was one that I'm not super familiar with, said, I want to see if the process Fortinet is running on the machines in my environment. And if it is, I want to stop it. So it actually seemed like relatively simple, but maybe I'm crazy. I don't know. So this is the script that I came up with. Okay. Get the process, and then wildcarded it. The name of the process is Fortinet. I'm going to grab this. I'm going to pull it over here into VS Code. Maybe we can see it a little better. Because the line one is just the variable process. The value is get process. The name is Fortinet. And then the little wildcard basically contains it. And then the error silently continues is because I don't want to see red because I'm a big chicken and it freaks me out. So this is where we started talking about this yesterday. Right? As I asked Tara the question, I said, hey, why did you use error action silently continue? Because at this point in my little journey, any red, I'm getting better, but it tends to freak me out. So I wanted to just continue and not give me crazy errors that I don't really care about. If it runs into a problem, just move on with your life. But sometimes we don't want it to move on, right? I know. Sometimes we want that terminating error. That red text is a terminating error. And we want that. But sometimes it's also not a terminating error and it can go about its business. It freaks me out. But well, it depends. Like, do you want an error here? Right? So use like in this script, like if someone's just like learning PowerShell for the first time, right? Like, yeah, you might want to do this because you don't want to see it. Yeah. But really if your get process here doesn't match. Sometimes it will just stop. Do we want to run the rest of the script? Well, that's a good point. What is a little confusing to me right now is sometimes it will just stop and it doesn't return any results. And I'm like, did you find anything or what you doing? Yeah. And that can be confusing to me. Yeah, absolutely. And then Andrew told me the pass through and then sometimes it will tell you, but if it's a state changing command, it won't. I'm like, great. That's not confusing at all, PowerShell. So yeah, sometimes that helps me, but you're going to hate it. So go ahead and take that off. No, no, no. I don't hate it at all. I actually don't hate it at all. Okay. This is an option that you can try something else. So like, if you're in this case and you're like, I don't like seeing that stuff, maybe a new thing you could do here is a try catch. Okay, what's that? Okay. So try catch is like, hey, I actually want you to go and look for this process and then like, just try it. And if it doesn't do it, then a terminating exception happens, right? So you could in here, you could do your try, like your exact thing that you have instead of storing it in a variable, right? You could do an exit and then you could just give it some random code. So you could do exit and you can give it like a string here if you want. An error occurred. I didn't find FortiClient, right? That's cool if you're just living in the terminal. So if we want to take this back to like Connect land though. Can you go back and do the try thing again? Cause I noticed it put comments in when you did that. That's kind of cool. Will you show that really quick? So we can, let's check this out. So let's do try catch finally, right? Ooh, I like that. So it's automatically putting this stuff in here to kind of help you see what you're doing. It's contextual learning. Okay, cause I gotta tell you, I have not been in VS Code yet. Like I might start, it's super busy for my eyes to handle, but I really like what you just showed because it's telling you what it's doing. And then you don't have to remember all of the syntax. That's the thing that's the hardest for me with like any language is like remembering the syntax or like you start doing stuff like this, right? Like if I do something silly in this catch block, if I want to do like another try catch inside, like this highlighting, reminding me where I'm at inside of this is really, really helpful. Okay, I think I might be almost ready to go in there, but okay. I think you are. So let's take this back to Connect. Okay, let's do. And let's maybe change this. So Connect, the output log is great if you're going to read it, but output codes from Standard Out is what it operates on for like is success, is failure. So you can write your scripts to write to Standard Out, right, by doing something like this. So maybe your exit is an exit one, two, three. And one, two, three means that the process wasn't there. And so it failed successfully. Okay, so like we don't have this running in our environment. So I would expect it to fail on that line. But your overall objective was if it exists, stop it. Okay. So if it exists and it stops it, it'll exit zero because it did it. If it tries to find the process, but the process doesn't exist, it would exit like as a terminating error. So it would look like a failure, but that's not really what it is. Oh, I really like this. So this is a really good one for beginners. Yes, it was a failure that you wanted to have happen. So we'll exit one, two, three. Okay. And then down here in connect, we'll say exit zero is fine. Also an exit of one, two, three. Okay. It's fine. That's super helpful. And so now this package, when you run it, returns a successful, either because it got the process and killed it or the process just doesn't exist at all. But your desired outcome was just make sure it's not running. Love that. So little tweaks, little tweaks. Yep, love that one. All right, I think we have time to maybe go through one more. Okay. What else was on the list? So I did want to find out what's using all the CPU. And then I did a printer one. Do you want to do this one or do you want to do a printer one? You pick. Let's do this one. I've already got it open. Okay. Okay, we're going to pull this thing over here back again. All right, so walk me through it. What was the goal here? What were we trying to do? Okay, well, this is just one that I've been doing, practicing with Josh Bot. So I understand what this one means. So it's just, I can do this easily. So get the processes. And then once you get those processes, sort them by using, which ones are using the most CPU, sort them in descending order. Give me the first five and list them by process name, ID, and then CPU. Okay. After you've listed those. There they are. Yeah, there they are. So say you wanted to see on this machine, what's consuming all the CPU. Then you go to the next part of the script. Skip anything that's idle. Yep, well, we're not idle. Okay, nothing's idle. And then, I'm lost for where we are, sorry. We're going to be here in descending. Oh yeah, sorry, there we are. Okay, start by descending. Select the one that is consuming the most. Yep. And then stop it. Okay. But then the very last one, because I'm a big baby, and ever since Andrew told me about this, I use it constantly. Just tell me what it's going to do. Don't do anything. And you see I wrote, because I'm a big chicken. A big what if? Yes, I love what if. What if is my favorite. What if is great. So in this case, this vMix desktop capture is what's allowing the audience to see our screen. Yeah, don't commit that one. We don't want to kill that one. We don't want to kill this one, right? No. So there's a couple of ways we can do this, right? There's a couple of different things. Like the what if is cool, but what if I told you that what if doesn't work on every command? Oh my gosh, why? Why doesn't it? Okay, tell me. No, I want to know, tell me. So it does generally, but there are some modules where what if doesn't do anything and the command actually executes. Oh, let's do it right now for Drew. Drew, can we try it on this one? No, let's not try that. I'm like 99.9% sure it works for stop process, but I don't want to mess with it. So you just want to make sure that you're checking the help documentation to see if there's that what if parameter is supported with that commandlet. Yes. Okay. So there's some things that you can do, right? You've got things here in like the select object. So you're piping to select object. So we could get into here and we could do things like, you know, add another filter set that filters out things that you know you don't want. Okay, let's do that. So we can do that. The thing that I think might be most helpful for people that are like one of the tips that I wanted to show today was like, this is cool if you're running on the machine that you're in. What if you're on a remote machine and you don't have something like Connect, right? Okay. So one of the fun things that happens a lot is you end up in this invoke command thing, right? And it ends up looking something like this, where you have all of these computers and this script block, right? And it just so happens to be that in this help command, they're also doing get process, right? Where you're stored object, but you've stored this previously as top. This gets really important when you're in invoke command because scope matters, right? So if I do something like this, if I call top again, like you did up there, this is not evaluated at the script runtime. This is evaluated on, in this case, server one, two, and three. Okay, and so you're gonna change the value of the variable now that I set at the top of the script because it has to mean something different on the remote computer? You defined it on this computer we're on. Okay, but it can't mean the same. You defined it as top as get process with a handful of different select objects in it. On server one, two, and three, top is meaningless, but we don't want top to be meaningless so that we need to do something like this. We need to go using top, right? Oops. Using top. So now what happens in this case, when we pass it this way in using, this is kind of a hacky way to do it. Anyone who's like really familiar with PowerShell is probably pulling their hair out going, why don't you just like create a function and pass arguments? Do that if you know how to do it. If you don't know how to do that and you get stuck in this place, you can actually have top defined and you can pass it into the script block on the remote machines with this fun thing called using. And this wasn't added until, you know, PowerShell three or somewhere around there. Yeah, it was later in the PowerShell. So I mean, this was a problem that people were running into. It's like, hey, I can't easily pass these variables to these remote computers because the variable is specific to my local computer. And so like, oh, okay, we'll come out with a function to help you do that. Yeah, little tweaks here because generally you're probably not looking at the CPU on this machine. You want to kill it on lots of machines and maybe you don't have something like PDQ Connect that can help you just do it in mass and you're just having to like comma separated value all these guys here. Little upgrades. You're doing awesome in PowerShell. Thank you. Variables are still messing me up a little bit because I expect them to be defined 100%. They're very fluid and they're just kind of messy. In my brain, I'm like, what? Yeah. I'm kind of getting it, but still a little. That, the concepts of like scope and the pipeline and executing. And then you throw in this like PowerShell remoting thing and the whole thing goes out the window. Yeah. You're doing awesome. Baby steps. Thanks for watching this segment from PDQ Live. If you like this, you'll love the full show. Check it out every Thursday at 10 a.m. Mountain. Oh, and like and subscribe, please.