Menu

[ea00bd]: / ps-usage / PS-ParameterSetsTest.ps1  Maximize  Restore  History

Download this file

116 lines (87 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<#
.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)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.