I came across the following website the other day http://skripting.com/2013/10/10/producing-an-intentional-bsod/
Get-process | Stop-Process
..and it reminds me both some key concepts of PowerShell as well as Don Jones and Jeffrey Snover demonstrating cmdlet binding in this video http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/WSV321-R (@25 minutes)
The first concept is “Read Help!”
Get-help Stop-Process -Full
On Windows Vista and later versions of Windows, to stop a process that is not owned by the current user, you must start Windows PowerShell with the “Run as administrator” option. Also, you are prompted for confirmation unless you use the Force parameter.
The help of Powershell confirms that it’s not as straightforward as the above the above blog post assumes.
The above piped cmdlets won’t generate a BSOD because the vast majority of cmdlets that modify the system will ask for confirmation by default.
Of course pressing “A” is a bad idea as it really ends with a BSOD:
The second concept is “when in doubt, use a risk management parameter”.
To simulate and see what these commands would do, you just need to add the -Whatif parameter:
Get-process | Stop-Process -WhatIf
Wait, there’s another crucial thing here. I said
the vast majority of cmdlets that modify the system will ask for confirmation by default
Powershell’s default behavior is controlled by preferences variables.
So, if you want the default behavior (and you should really stick to it), don’t alter these preferences variables.
One of the most widely spread error is people starting a script by doing
$ErrorActionPreference = "SilentlyContinue"
Repeat after me, “this is wrong!”
Now let’s go back to one of the most important concept of Powershell: CmdletBinding
Jeffrey Snover quickly explained it in the above video http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/WSV321-R I mentioned (~@30 minutes).
Powershell MVP Don Jones has a great nice example to explain it.
Can you figure out what this will do?
Get-Service | Stop-Process -WhatIf
Let’s have Don Jones explain how CmdletBinding works:
ByValue (Plan A)
ByPropertyName (Plan B)
Want to go deeper, here are some insightful links:
- Advanced Automation Using Windows PowerShell http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/MDC-B400
- MVP Boe Prox shows how cmdletbinding works and how to trace it http://learn-powershell.net/2013/05/07/tips-on-implementing-pipeline-support/
- Bartek Bielawski shows how to correctly implement SupportsShouldProcess http://becomelotr.wordpress.com/2013/05/01/supports-should-process-oh-really/