Skip to content

Instantly share code, notes, and snippets.

@IISResetMe IISResetMe/bad.ps1
Created Mar 26, 2019

Embed
What would you like to do?
$params = @{
Name = 'w*'
}
# unrelated stuff happening
# ...
# ...
# ...
$params2 = @{
Exclude = 'w32time'
}
# another 200 lines of logging, comments, setup
# ...
# ...
# ...
# what are we looking for again?
$services = Get-Service @params @params2
# unrelated stuff happening
# ...
# ...
# ...
$params = @{
Name = 'w*'
}
$params2 = @{
Exclude = 'w32time'
}
# why do I have to look twice for these
$services = Get-Service @params @params2
# unrelated stuff happening
# ...
# ...
# ...
$params = @{
Name = 'w*'
Exclude = 'w32time'
}
# lines are short and readable, $params definition is close
$services = Get-Service @params
# Implementing proxy functions
function Get-ServicesWithW
{
param(
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('Cn')]
[ValidateNotNullOrEmpty()]
[string[]]
${ComputerName},
[Alias('DS')]
[switch]
${DependentServices},
[Alias('SDO','ServicesDependedOn')]
[switch]
${RequiredServices},
[Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
[string[]]
${DisplayName},
[ValidateNotNullOrEmpty()]
[string[]]
${Include},
[ValidateNotNullOrEmpty()]
[string[]]
${Exclude},
[Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.ServiceProcess.ServiceController[]]
${InputObject}
)
# Ain't no way in hell I'm gonna write the if/elseif/else logic to inspect and
# ensure every possible permutation of named parameter arguments are supported here.
# I want to focus only on the behavior of this function instead
$PSBoundParameters['Name'] = 'w*'
$PSBoundParameters['Exclude'] = if($PSBoundParameters.Contains('Exclude')){
@($PSBoundParameters['Exclude'];'w32time')
}
else {
,'w32time'
}
Get-Service @PSBoundParameters
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.