Use PowerShell WhatIf So You Don't Break Stuff
1K views
May 25, 2024
Don't break stuff! Learn how you can add the WhatIf parameter to your scripts and functions so you know what your code is going to do before it does it. š Get the Code Here! š All Code: https://github.com/JeffBrownTech/jeffbrowntech_youtube This Video: https://github.com/JeffBrownTech/jeffbrowntech_youtube/tree/main/2024-05-20-PowerShellWhatIf š Read More About This š https://jeffbrown.tech/using-powershell-whatif-so-you-dont-break-stuff/ ā° Chapters ā° 00:00 Introduction 00:47 WhatIf Introduction 01:29 Function Overview 01:50 Updating CmdletBinding Attribute 02:35 Adding WhatIf Logic 05:21 Customize WhatIf Message 06:48 Summary Social Links š LinkedIn: https://www.linkedin.com/jeffbrowntech
View Video Transcript
0:00
What's up everyone? Today we're going to talk about the what-if parameter that you can use inside of PowerShell
0:06
The what-if parameter will show you what your script is going to do before it actually does it
0:10
so you can see logically if it's going to do what you think it's going to do
0:14
and you can add this to your own scripts or functions quite easily
0:18
Before we get into the video, one bit of housekeeping. I am now putting all my code examples out into a public repository in GitHub
0:26
I'll include a link to that down in the video description. I'll also put it up on the screen right now
0:31
So any of the videos you've watched before and moving forward, all the example code will be out in PowerShell
0:36
and I also went back and updated my previous video's descriptions with links out to that code
0:42
That way you can reference them there if you need to. Now, let's go ahead and get started
0:47
As I mentioned in the introduction, the what-if parameter will show you what your script or function is going to do before it actually does it
0:54
It will show what logic or steps it's going to take, and just give you an idea of what it will do
0:58
One thing the what-if parameter won't do is it won't check to see if you have permissions to actually perform that command
1:04
Maybe you're doing something like creating a user or making a change to a database
1:08
It's not going to check to see if that's successful. This means you might run your script or function with the what-if parameter
1:14
and it looks like everything is good to go, but then you go to actually execute it
1:17
and you don't have permissions to do the thing that it's trying to do. But it will at least show you logically what your script or function is going to do
1:25
Let's take a look at how you can implement this in your own scripts or functions
1:29
First, let's go over the example function that I have here. It's just called new widget
1:34
It's going to create new widgets for us. It takes three parameters, shape, color, and quantity
1:41
We're creating a widget object here, and we're just adding the content of our widget out to our widget.txt file here
1:48
That's how we're creating our widget. The first thing we want to do is, before our parameter definition
1:54
is add our cmdlet binding keyword here. The cmdlet binding attribute gives your script or function capabilities
2:01
that you would find in compiled cmdlets written in something like C-sharp
2:05
and you can have that same functionality in your own scripts or functions that you're writing
2:10
We've talked a little bit about cmdlet binding in other videos I've done
2:14
and getting started with parameters. Inside of our cmdlet binding, we have several properties or attributes that we can assign here
2:22
and one of them is supports should process. It's a Boolean value. You just need to add it here. That means it equals true
2:30
By putting this in here, we're saying our function here does have this capability
2:35
Now, adding this isn't just enough. You have to add logic into your script to show what would happen
2:41
if someone added the what-if parameter to your cmdlet or script as they're running it
2:47
You want to add this logic right where the action is happening
2:50
You don't want to do it too early and include a lot of extra information
2:54
You want to put it right around where the script or function is doing its main thing
3:00
and that for us is right here at line 23 where we're adding content to our text file
3:04
basically creating our widget. To add in the logic to see if they are using the what-if parameter we're going to use
3:11
an if statement. So we'll start off if, and we're going to look at the built-in variable called pscmdlet
3:19
This contains all the information about when the function or script was ran
3:23
including what parameters it was used. We've seen this before when we talked about parameter sets
3:28
and we use this to check to see which parameter set was being used
3:32
And it has an option here of should process. I'm going to highlight should process, and it brings up information on what parameters
3:40
we can pass to it here. We can see at a minimum we have to pass it a target, and that should be a string value
3:48
And then we have some additional ones that we're going to go through, such as the action
3:52
We can do custom warning messages or descriptions and everything. But let's just start off with the basics right now
3:57
We'll do should process. And here I'm going to say we're processing something on our widget text file
4:05
And then we need to put in the rest of our if statement, and then move our action of what we're actually doing in here
4:15
Clean this up a little bit. All right, perfect. We're saying if we've used the what if parameter and what are we processing
4:23
we're processing the widget onto our widget text file, and that should display our output message on what it's going to do when we run the cmdlet
4:32
So let me go ahead and highlight my function code here and bring it into my existing session
4:39
And then we are going to run new widget. I've got something here in my history
4:44
We'll pass in circle, if I can spell it correctly. The color red, quantity 10, and we have the what if parameter
4:54
So we'll run this, and it comes back saying, performing the operation new widget on our target widget.txt
5:01
Now, if you notice on the right-hand side, our widget text file doesn't have any information in it because it didn't actually perform the action
5:08
It is just showing us what it was going to do. If we were to bring this back up and take off our what if parameter
5:16
and then we can see our text file was updated with our new widget that we created
5:21
So far, looking pretty good. We can add additional information in here on what it's processing
5:27
or just give some more description around it. So our next value we're going to put in here is we're just going to customize our message
5:34
and say in creating widget. So we'll go ahead and save that
5:40
And let me highlight and press F8 to bring our function into our system again
5:46
And let's bring back our what if example. Now it's changed our output message
5:52
performing the operation creating widget on target widget.txt. Again, just customizing the message so the person running the script or function
6:00
just gets a better idea of what it would do when they ran it with what if
6:05
If you want to completely customize the what if message, we actually go back to the beginning of our call here to should process
6:15
creating a brand new widget. Okay, we'll save that and again bring it into our current session here
6:25
And we'll rerun it again with what if. What if creating a brand new widget
6:30
Again, we'd have to include more information in there on our string based on what we had before
6:38
But again, you're getting the idea. Just need to come in here to should process
6:42
There's a lot of other parameters you can add in order to customize your message. That does it for this video
6:47
Hopefully you can take something like this with the what if parameter, add it into your own scripts or functions to give it more of a compiled commandlet feel
6:54
and being able to do things and check to see what your script is going to do before it actually does it
6:59
Thank you for watching and we'll see you next time. Thank you
#Education
#Programming
#Software