Wednesday, March 23, 2016

PowerShell: Finding commands (functions) with Risk Mitigation capabilities

Yesterday I was talking to a workshop about implementing Risk Mitigation capabilities into your own Advanced Functions in PowerShell. This got me thinking about how to identify the commands that have that capability enabled. More importantly the configuration of the ConfirmImpact setting.

Background:
If you don't know PowerShell uses a number of inputs when determining how the Risk Mitigation capabilities are applied.
For the WhatIf capability it is purely the presence of the -WhatIf switch parameter.
For the Confirm capability it is a combination of the ConfirmImpact value in the [CmdLetBinding()] attribute and the $ConfirmPreference variable value. Or the presence of -Confirm parameter

The following command we show all commands with the RiskMitigation capability.
Get-Command | Where-Object {$PsItem.Parameters.Keys.Count -gt 0 -and $PsItem.Parameters.ContainsKey('WhatIf')}


We can extend this further to retrieve the ConfirmImpact value however this is not exposed through a Parameter on the command object and can only be retrieved from the source definition. Unfortunately for CmdLets this is not exposed as they are compiled. This will work though for Functions so you can check your own commands etc.

Get-Command -CommandType Function | `
   Where-Object {$PsItem.Parameters.Keys.Count -gt 0 -and $PsItem.Parameters.ContainsKey('WhatIf')} | `
   Select-Object -Property Name,CommandType,@{Name="Impact";Expression={[Regex]::Match($PSItem.Definition, "(ConfirmImpact='(?<impact>.{1,})'{1})").Groups["Impact"].Value}}


This pipeline command uses the RegEx type accelerator and Match static member but similar could be achieved with the -Match operator.


Legal Stuff: As always the contents of this blog is provided “as-is”. The information, opinions and views expressed are those of the author and do not necessarily state or reflect those of any other company with affiliation to the products discussed. This includes any URLs or Tools. The author does not accept any responsibility from the use of the information or tools mentioned within this blog, and recommends adequate evaluation against your own requirements to measure suitability.

4 comments:

  1. Kup hublot fałszywy zegarek, najlepsza replika breitling, u-boat italo fontana, repliki cartier , zegarki hublot replica, fałszywe zegarki breitling, replika montblanc, chodź tu kupić repliki zegarków uk, zegarki rolex fałszywe, repliki zegarków repliki, zegarki repliki tag heuer, zegarek omega, najtańsze fałszywe rolex, omega.
    repliki zegarków Rolex
    repliki zegarków Rolex

    ReplyDelete
  2. Fake Cartier watches come in all the fake watches prominent Cartier styles: Tortue, Tank, Santos, Ballon, and replica cartier watchesBaignore to mention a few. These come in a wonderful number of gorgeous gents and womens designs. Our fake Swiss Cartier watches are exact forgeries of the genuine ones, down to the detailing that includes coloured knobs, diamonds encrusting, and stainless-steel cases. These specific replications . are great for showing your love for quality whether at the workplace or perhaps in a social setting.

    ReplyDelete
  3. The Audemars Piguet Jules Audemarsfake watches collection commemorates one of the brand’s founders. The line is characterized by its traditional and timeless round cases and overall minimalist design. replica jules audemars watchesHowever, behind the simplicity of these watches is an array of impressive functions, such as tourbillons, moon phases, and chronographs. Designed with both men and women in mind, the Jules Audemars collection fully embodies both the range and restraint of the brand.

    ReplyDelete
  4. High quality from a trust worthy replica raybans sunglasses here can offer you the best in high quality, including fake raybans clubmaster , etc.

    ReplyDelete