Skip to content

Instantly share code, notes, and snippets.

@ashdar
Last active June 16, 2017 13:38
Show Gist options
  • Select an option

  • Save ashdar/d555f174b1fccef17e9630ecf20357af to your computer and use it in GitHub Desktop.

Select an option

Save ashdar/d555f174b1fccef17e9630ecf20357af to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
A simple example of how to use parameter sets
.DESCRIPTION
A simple example of how to use parameter sets.
Imagine that you have a function that could take a file ID or a file name.
If provided a file name, then look up the file ID and proceed as normal with that value.
As a coder, you want to avoid coding things like:
if ($FileID -and $FileName) {
throw "You can't do both things!"
}
How do you specify one or the other? How can the code determine what course of action to take?
.EXAMPLE
See the body of the script. This script is for a demo purposes, not production use
#>
function foo {
param (
[Parameter(ValueFromPipeline=$false, Mandatory=$true)]
[string] $ServerInstance,
[Parameter(ValueFromPipeline=$false, Mandatory=$true)]
[string] $Database,
# Marking the parameter as Mandatory doesn't seem as important as usual
# [Parameter(ParameterSetName='ByFileID', Mandatory=$true)]
[Parameter(ParameterSetName='ByFileID')]
[int] $FileID,
# [Parameter(ParameterSetName='ByFileID', Mandatory=$true)]
[Parameter(ParameterSetName='ByFileName')]
[string] $FileName,
[int] $Multiplier = 1,
[switch] $Force
)
Write-Output "Called function: foo"
Write-Output "fileid: $fileid"
Write-Output "filename: $filename"
Write-Output "ParameterSetName: $($PSCmdlet.ParameterSetName)"
if ($PSCmdlet.ParameterSetName -eq 'ByFileName') {
Write-Output "Looking up the filename '$FileName' using a complex function"
# Fake a value with .Length
$FileIDToUse = $FileName.Length
}
elseif ($PSCmdlet.ParameterSetName -eq 'ByFileID') {
Write-Output "Use the value we were called with"
$FileIDToUse = $FileID
}
else {
<#
It seems guaranteed that if you use ParameterSets, you will always be handed a valid ParamterSetName.
Even if the parameter in those sets are not marked as mandatory.
#>
throw "Invalid ParameterSetName"
}
Write-Output "Do the thing using FileID = $FileIDToUse"
}
# don't accidentally run this whole script.
break
<#
This will fail
The FileID and FileName parameter are both decorated as "Mandatory"
PowerShell will force you to specify one or the other.
#>
foo -serverinstance 'hal9000' -database 'astronauts'
<#
This will fail
The FileID and FileName parameter in different parameter sets.
PowerShell prevents you from using both at the same time
#>
foo -serverinstance 'hal9000' -database 'astronauts' -fileid 123 -filename 'foobar'
<#
This will run correctly
We specify only the FileID parameter and Powershell figures it out.
#>
foo -serverinstance 'hal9000' -database 'astronauts' -fileid 123
<#
This will run correctly
We specify only the FileName parameter and Powershell figures it out.
#>
foo -serverinstance 'hal9000' -database 'astronauts' -filename 'db-data-42'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment