Advertisement
McDoomy

Powershell Cmdlet with 'dynamic' ConfirmImpact attribute set

May 27th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Class WebSite {
  2.     [string] $Name
  3.     [string] $Category # Can be one of: Production, Test, Development
  4. }
  5.  
  6. Function CategoryToImpact([string]$Category) {
  7.     Switch ($Category) {
  8.         'Production' {
  9.             [System.Management.Automation.ConfirmImpact]::High
  10.             break
  11.         }
  12.         'Test' {
  13.             [System.Management.Automation.ConfirmImpact]::Medium
  14.             break
  15.         }
  16.         'Development' {
  17.             [System.Management.Automation.ConfirmImpact]::Low
  18.             break
  19.         }
  20.         default {
  21.             [System.Management.Automation.ConfirmImpact]::None
  22.             break
  23.         }
  24.     }
  25. }
  26.  
  27. Function Remove-WebSite {
  28.     [CmdletBinding(SupportsShouldProcess=$true<#,ConfirmImpact="Depends!"#>)]
  29.     Param(
  30.         [Parameter(Mandatory=$true)]
  31.         [WebSite] $WebSite
  32.     )
  33.  
  34.     # This doesn't work but I hope it illustrates what I'd *like* to do
  35.     #$PSCmdLet.ConfirmImpact = CategoryToImpact($WebSite.Category)
  36.  
  37.     if ($PSCmdlet.ShouldProcess("$($WebSite.Category) site $($WebSite.Name)")) {
  38.         Write-Host "$($WebSite.Name) was destroyed"
  39.     }
  40. }
  41.  
  42. $prod = New-Object WebSite
  43. $prod.Name = 'www.example.com'
  44. $prod.Category = 'Production'
  45. Remove-WebSite $prod
  46.  
  47. $test = New-Object WebSite
  48. $test.Name = 'test.example.com'
  49. $test.Category = 'Test'
  50. Remove-WebSite $test
  51.  
  52. $dev = New-Object WebSite
  53. $dev.Name = 'dev.example.com'
  54. $dev.Category = 'Development'
  55. Remove-WebSite $dev
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement