<#
.SYNOPSIS
Test script, utilizing parameter sets. Deals with the following logic requirements of a script:
1. If no params specified on command line, ask for the share name.
2. If a share name is specified as a param, the -NoShare param cannot be present.
3. The -NoShare can be present-ass long as (per #2 above) -ShareName is not present.
.DESCRIPTION
This script is the result of a request for a very specific MSP script that was to run remotely. But,
It is also a great demonstration of how Parameter Sets work.
.NOTES
┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│ ORIGIN STORY │
├─────────────────────────────────────────────────────────────────────────────────────────────┤
│ DATE : 2019.02.04
│ AUTHOR : Pat Trainor <Pat@ITGuys.Business>
│ DESCRIPTION : Initial Draft
└─────────────────────────────────────────────────────────────────────────────────────────────┘
.PARAMETER ShareName
[string] $ShareName - the location of the file to be referenced later, as in: "\\frank\foo\iso". Can only be used when -NoShare is NOT specified, otherwise mandatory. At a bare minimum, this must be specified on command line, or if command executed with no parameters, it will ask for this variable before proceeding.
.PARAMETER NoShare
[switch] $NoShare - $true or $false (same as if not present) as to whether to disregard using a share. Only legal to use if mandatory string -ShareName is not specified.
.EXAMPLE
Works:
Test-MultiParamSets -ShareName "\\fred\share\iso"'
Set name is: UseShareName
ShareName is: [\\fred\share\iso], NoShare is [False]
.EXAMPLE
Works:
Test-MultiParamSets -NoShare
Set name is: NoShare
ShareName is: [], NoShare is [True]
.EXAMPLE
Does not work (illegal to specify a share AND say to not use it):
Test-MultiParamSets -NoShare -ShareName "\\frank\share\iso"
Test-MultiParamSets : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\pat\Documents\Untitled601.ps1:41 char:1
+ Test-MultiParamSets -NoShare -ShareName "\\frank\share\iso"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-MultiParamSets], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-MultiParamSets
.EXAMPLE
Works only if share name specified when run, as EITHER share name or -NoShare switch is required-not BOTH, and not NEITHER:
Test-MultiParamSets
cmdlet Test-MultiParamSets at command pipeline position 1
Supply values for the following parameters:
ShareName: \\frank\share\iso <--- script stops and waits for inout-cannot be blank!
Set name is: UseShareName
ShareName is: [\\frank\share\iso], NoShare is [False]
#>
Function Test-MultiParamSets
{
[cmdletbinding(
DefaultParameterSetName='UseShareName'
)]
Param(
[Parameter(
ParameterSetName='UseShareName'
,Mandatory = $true
)]
[Parameter(
ParameterSetName='NoShareName'
)]
[String]
$ShareName,
[Parameter(
ParameterSetName='NoShare'
)]
[switch]
$NoShare
)
'Set name is: {0}' -f $PSCmdlet.ParameterSetName
'ShareName is: [{0}], NoShare is [{1}]' -f $ShareName, $NoShare
}
Function Write-Line { param([int]$howMany) Write-Output ("_" * $howMany) }
Write-Line(40)
Write-Host 'TEST 1 -> Test-MultiParamSets -ShareName "\\fred\share\iso"'
Test-MultiParamSets -ShareName "\\fred\share\iso"
Write-Line(40)
Write-Host 'TEST 2 -> Test-MultiParamSets -NoShare'
Test-MultiParamSets -NoShare
Write-Line(40)
Write-Host 'TEST 3 -> Test-MultiParamSets -NoShare -ShareName "\\fred\share\iso <--- ILLEGAL!'
Test-MultiParamSets -NoShare -ShareName "\\frank\share\iso"
Write-Line(40)
Write-Host 'TEST 4 -> Test-MultiParamSets <--- has to ask for mandatory param -ShareName'
Test-MultiParamSets
Write-Line(40)