Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Cool PowerShell Conversations at Ignite

$
0
0

Summary: Ed Wilson talks about some Windows PowerShell conversations at the Scripting Guys booth.

One of the cool things about getting together with a bunch of people at the Scripting Guys booth is that I learn stuff. The conversations are great. Here is a case in point: Windows PowerShell MVP, Steve Murawski, stopped by the booth this morning. At the same time Microsoft PFEs, Gary Siepser, Brian Wilhite, and Jason Walker were here. Here is a picture:

steveBrianJasonGary

Ayway, while I was talking to Steve, I mentioned a conversation I had yesterday with a guy at lunch. I was talking about the importance of using –Whatif on a regular basis. In fact, I mentioned a story about the time I deleted my RAS certificate accidently. It happened because I knew what the command would do, and then...oops, my RAS certificate went into the bit bucket.

So, as I was talking to Steve, I had a great idea: Why don’t I use A$PSDefaultParameterValues and set any cmdlet that uses –Whatif to $true. This means that any cmdlet that had –Whatif as a parameter would automatically prompt with "What if." Here is an example:

PS C:\Users\ed> $PSDefaultParameterValues = @{"*:whatif"=$true}
PS C:\Users\ed> New-Item c:\fso -ItemType directory
What if: Performing the operation "Create Directory" on target "Destination: C:\fso".
PS C:\Users\ed> New-Item c:\fso -ItemType directory -WhatIf:$false


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          5/4/2015  11:23 AM            fso

I was like really excited, and I told Gary, Brian, and Jason to come over, and see what I had done. I said, “Dude, this is cool!"

Gary looked at it for a few minutes, shook his head and said, “Why didn’t you just set the $WhatIfPreference variable to $true.

I looked at him and said, “Because I didn’t know about it.” It seems that the $WhatIfPreference variable has been in Windows PowerShell since at least version 2. Hmmmm…I must have missed that one. So here is how to set it:

PS C:\Users\ed> $WhatIfPreference
False
PS C:\Users\ed> $WhatIfPreference = $true
PS C:\Users\ed> $WhatIfPreference
True

Now any cmdlet that has a –WhatIf parameter will prompt. This is shown here:

S C:\Users\ed> $WhatIfPreference = $true
S C:\Users\ed> Remove-Item c:\fso
hat if: Performing the operation "Remove Directory" on target "C:\fso".
S C:\Users\ed>

But if I want to override it, here is the code I would use:

PS C:\Users\ed> Remove-Item c:\fso -WhatIf:$False
PS C:\Users\ed>

As it turns out, I might not want the prompt every time I use Set* or New*. It might become annoying. Maybe all I really want is protection from Remove* cmdlets. I can go back to my $DefaultParameterValues that I began this post with, and modify it as shown here:

PS C:\Users\ed> $PSDefaultParameterValues = @{"Remove*:whatif"=$true}
PS C:\Users\ed> Remove-Item c:\fso
What if: Performing the operation "Remove Directory" on target "C:\fso".
PS C:\Users\ed> Remove-Item c:\fso -WhatIf:$false
PS C:\Users\ed> New-Item c:\fso -ItemType directory


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          5/4/2015  12:02 PM            fso

Cool! This is what happens when a bunch of Windows PowerShell people congregate.

~Ed


Viewing all articles
Browse latest Browse all 3333

Trending Articles