Skip to content

Instantly share code, notes, and snippets.

@shaneis
Last active September 21, 2018 10:25
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 shaneis/c4a0c5e0558bcb827d13f2f16b071bfe to your computer and use it in GitHub Desktop.
Save shaneis/c4a0c5e0558bcb827d13f2f16b071bfe to your computer and use it in GitHub Desktop.
Added stop with error
function Test-ParametersAndPrompts {
[cmdletbinding()]
param(
[parameter(Position = 0, HelpMessage = 'Enter the location of the folder.')]
[ValidateScript({ Test-Path -Path $_ })]
[Alias('PSPath')]
[string]
$Path,
[parameter(Position = 1, HelpMessage = 'Enter a number from 1 to 10.')]
[ValidateRange(1, 10)]
[Alias('RandomNumber')]
[int]
$Number,
[parameter(Position = 2, HelpMessage = 'Choose a set of Set1, Set2, or Set3.')]
[ValidateSet('Set1', 'Set2', 'Set3')]
[Alias('Option', 'Choose')]
[string]
$Set
)
process {
switch ($PSBoundParameters) {
{ $_.Keys -notcontains 'Path' } {
Try {
$Path = Read-Host -Prompt 'Enter the location of the folder.' -ErrorAction Stop
} catch {
Write-Error $error[0]
return
}
}
{ $_.Keys -notcontains 'Number' } {
try {
$Number = Read-Host -Prompt 'Enter a number from 1 to 10.' -ErrorAction Stop
} catch {
Write-Error $error[0]
return
}
}
{ $_.Keys -notcontains 'Set' } {
Try {
$Set = Read-Host -Prompt 'Choose a set of Set1, Set2, or Set3.' -ErrorAction Stop
} catch {
Write-Error $error[0]
return
}
}
}
[PSCustomObject]@{
Path = $Path
Number = $Number
Set = $Set
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment