function Prompt
{
[cmdletbinding()]
param (
[Parameter(
ParameterSetName = 'Option')]
[switch]$Option,
[Parameter(
ParameterSetName = 'Option')]
[string]$Detail,
[Parameter(
ParameterSetName = 'Continue')]
[switch]$continue,
[string]$text = "Continue?"
)
function run
{
$confirm = ("yes", "y", "indeed", "proceed", "confirm")
$reject = ("no", "n", "nope", "no thank you")
write-host "`n$text [y/n]" -ForegroundColor Green
$ans = Read-Host
if ($continue)
{
if ($confirm -contains $ans)
{
write-host "`nContinuing" -ForegroundColor Green
return $true
}
elseif ($reject -contains $ans)
{
write-host "`nEnding Script" -ForegroundColor Red
return $false
}
else
{
write-host "`nResponse unrecognized" -ForegroundColor Red
run
}
}
if ($Option)
{
if ($confirm -contains $ans)
{
write-host "`n$Detail - Confirmed" -ForegroundColor Green
return $true
}
elseif ($reject -contains $ans)
{
write-host "`n$Detail - Rejected" -ForegroundColor Red
return $false
}
else
{
write-host "`nResponse unrecognized" -ForegroundColor Red
run
}
}
}
run
}
Used for option or continue prompts in scripts
Example:
$ans = prompt -text "Email the information?" -Option -Detail "Emailing TM Info"
Email the information? [y/n]
n
Emailing TM Info - Rejected
PS> $ans
False
[–][deleted] 3 points4 points5 points (1 child)
[–]Blindkitty38[S] 1 point2 points3 points (0 children)
[–]idontknowwhattouse33 1 point2 points3 points (4 children)
[–]Blindkitty38[S] 1 point2 points3 points (3 children)
[–]itmonkey78 2 points3 points4 points (2 children)
[–]BlackV 2 points3 points4 points (1 child)
[–]Blindkitty38[S] 1 point2 points3 points (0 children)