Last active
September 21, 2018 10:25
-
-
Save shaneis/c4a0c5e0558bcb827d13f2f16b071bfe to your computer and use it in GitHub Desktop.
Added stop with error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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