Created
March 28, 2017 21:26
-
-
Save cezarypiatek/8547adbd1aabb656bc8adb13b9416c78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Create-Parameters{ | |
param([scriptblock]$Params) | |
$runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary | |
$parameters = & $Params | |
foreach($parameter in $parameters) | |
{ | |
$runtimeParameterDictionary.Add($parameter.Name, $parameter) | |
} | |
$runtimeParameterDictionary | |
} | |
function New-Parameter{ | |
param($Name, $Position, $ValidateSet, $Mandatory=$false) | |
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] | |
$parameterAttribute = New-Object System.Management.Automation.ParameterAttribute | |
$parameterAttribute.Mandatory = $Mandatory | |
$parameterAttribute.Position = $Position | |
$attributeCollection.Add($parameterAttribute) | |
$validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet | |
$attributeCollection.Add($validateSetAttribute) | |
New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $attributeCollection) | |
} | |
function Kill-Service{ | |
DynamicParam{ | |
Create-Parameters -Params { | |
New-Parameter -Name "Service" -Position 0 -Mandatory $true -ValidateSet $(Get-Service | Select-Object -ExpandProperty Name) | |
} | |
} | |
process{ | |
Get-Service -Name $PsBoundParameters["Service"] | kill | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment