PowerShell Not Your Father's Command Line Part 24 of 31: PowerShell Did What!?!? How to Mitigate Risk!

imageHave you ever had the moment, and it is usually right after you hit the enter key, that OMG what I have done moment.  That please, please stop moment.  Followed by the pounding of fists, weeping, and a whole lot of NO NO NO as your c: drive is being polited wiped out because of a typo in diskpart.  Then you are rewarded with this message, followed by even more fist pounding and weeping:

Command Completed Successfully.

Today’s post is how can PowerShell help you look into the future before you hit the enter key, and see what is going to happen.  This post comes courtesy of the Hey Scripting Guy Ed Wilson.  I ran into Ed at a dinner party and we chatted about PowerShell and he told me an interesting story.  Centered around that Oh No moment we have all encountered.  This was what sparked our conversation and brought to mind a great ability in PowerShell you need to know about! 

There is this little known and extremely useful switch called -WhatIf.  This is PowerShell’s version of the Amazing Kreskin, and will allow you to see what a PowerShell command will do before it does it!   WhatIf is a part of the common parameters and is commonly referred to as a risk mitigation parameter.  WhatIf will show you messages on the potential effect of PowerShell commands instead of executing them.   During this series we have been a bit cavalier with the PowerShell examples and you may have tried some of them right after you read the post.  For the most part this is what Sarah and I wanted with the series, and the examples are pretty benign.  It really gets us into to the mindset when we are working with scripts. just type and go.   If it runs it runs, if not we troubleshoot.  But what if it runs too well?  What then, well lets hope you have a good backup or you were in Hyper-V.  You may have gone through some of this series , like the registry provider or AD recycle bin which took you out of your comfort zone with PowerShell.   When you go out of your comfort zone in PowerShell  that is where –WhatIf comes into play.

Take the cmdlet Get-Service for example, this will get us all the services on the current system.  Then what about this cmdlet Stop-Service, which will stop the service well tell it to.

But What If we did this (BTW do not do this):

Get-Service | Stop-Service

Surely this would not stop all the running services, surely this would not crash my server, surely this is safe….Starting to think about what may happen now?  Maybe even starting to get a little concerned, is your heart rate rising.  What do we do? -WhatIf to the rescue.  Now we run the same command but with the -WhatIf switch at the end (BTW you can try this):

Get-Service | Stop-Service –WhatIf

Your results will look something like the following screenshot, and shows all the services that would be stopped:

image

Oooh that was a close one, and with -WhatIf I can see what would have happened.  As you can imagine this little switch should be a part of your PowerShell toolbox.  So if your not sure what PowerShell is going to do, it is now simple.  Just ask. 

WhatIf is a part of the common parameters and is one of the risk mitigation parameters.  The other risk mitigation parameter is –Confirm, which will put a confirmation prompt on your PowerShell commands prior to executing them.  In the case of the previous example it would prompt for each service that was going to be stopped and allow you to control the action.   It is another very handy switch to make things run safely in your PowerShell world.  The other common parameters provide an array of functionality and they are:

  • -Verbose
  • -Debug
  • -WarningAction
  • -WarningVariable
  • -ErrorAction
  • -ErrorVariable
  • -OutVariable
  • -OutBuffer

To learn about the other common parameters run Get-Help about_CommonParameters

Again huge thanks for Ed Wilson asking me the question What If?  Thanks Ed!  Make sure you check out his blog: https://blogs.technet.com/heyscriptingguy/

Thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell. Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day and be careful out there!