Powershell validation of read-host data entry in while loop with nonewline -
i have function inserts y or n menu when called within powershell scripts. uses while loop validate either y or n value entered. works fine, new line created each time error made. use cls , redisplay everything, not ideal solution. instead, find way redisplay read-host prompt on same line while clearing entered answer. here existing code:
# begin function display yes or no menu function ynmenu { $global:ans = $null write-host -foregroundcolor cyan "`n y. [yes]" write-host -foregroundcolor cyan "n. [no]`n" while ($ans -ne "y" -and $ans -ne "n"){ $global:ans = read-host "please select y or n" } } # end function ynmenu
i have few other dynamically populated menus leverage methodology. finding solution resolve issue well.
i don't think there's simple way that.
but yes/no response, can use $pscmdlet.shouldcontinue($query, $caption)
instead, long scope you're in (function, script, etc.) defined attribute [cmdletbinding(supportsshouldprocess=$true)]
. shows appropriate yes/no prompt in ise , in console host , avoids manual processing.
Comments
Post a Comment