Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created March 26, 2019 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/447f3bb2c271ad993ac84c7d1cb6849c to your computer and use it in GitHub Desktop.
Save IISResetMe/447f3bb2c271ad993ac84c7d1cb6849c to your computer and use it in GitHub Desktop.
$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