Skip to content

Instantly share code, notes, and snippets.

@cezarypiatek
Created March 28, 2017 21:26
Show Gist options
  • Save cezarypiatek/8547adbd1aabb656bc8adb13b9416c78 to your computer and use it in GitHub Desktop.
Save cezarypiatek/8547adbd1aabb656bc8adb13b9416c78 to your computer and use it in GitHub Desktop.
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