Thursday, 21 November 2019

PowerShell Function Good Input

Just a little prompt function that checks for ‘Good Input’.


Function GoodInput{
  Param([System.Array]$InArr,[Switch]$Ceq)
  $GI = $FALSE
  While(!$GI){
    [String]$RH = Read-Host
    $InArr | Foreach{
      If($Ceq){
        If($RH -ceq $_){$GI = $TRUE}}
      Elseif($RH -eq $_){$GI = $TRUE}
    }
  }
  RETURN $RH
}


And an example of using it in the image below:

"Y or N not case sensitive";[String]$Test = GoodInput "Y","N"
"Y or N case sensitive";[String]$Test2 = GoodInput "Y","N" -CEQ

Image: Demo of Using PowerShell Function GoodInput

No comments:

Post a Comment