Posted On May 7, 2020

PowerShell: Function to Confirm Certain Contents

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Function to Confirm Certain Contents

It’s always a good idea to receive user confirmations prior on proceeding to making important changes. Here’s a reusable function to be added to one’s code base for that purpose.

# Version 0.02

function confirmation($content,$testValue="I confirm",$maxAttempts=3){
  $confirmed=$false
  $cancelCondition=@('cancel','no','exit','nope')
  $attempts=0
  clear-host 
  write-host $($content|out-string).trim()
  write-host "`r`nPlease review this content for accuracy.`r`n"
  while ($attempts -le $maxAttempts){
      if($attempts++ -ge $maxAttempts){
          write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
          break;
          }
      $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
      if ($userInput.ToLower() -eq $testValue.ToLower()){
          $confirmed=$true;
          write-host "Confirmed!`r`n";
          break;                
      }elseif($userInput.tolower() -in $cancelCondition){
          write-host 'Cancel command received.'
          $confirmed=$false
          break
      }else{
          clear-host
          $content|write-host
          write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
          }
      }
  return $confirmed
}
# Version 0.01
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
        $confirmed=$false;
        $attempts=0;        
        $content|write-host
        write-host "Please review this content for accuracy.`r`n"
        while ($attempts -le $maxAttempts){
            if($attempts++ -ge $maxAttempts){
                write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
                break;
                }
            $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item.";
            if ($userInput.ToLower() -eq $testValue.ToLower()){
                $confirmed=$true;
                write-host "Confirmed!`r`n";
                break;                
            }elseif($userInput -like 'cancel'){
                write-host 'Cancel command received.'
                $confirmed=$false
                break
            }else{
                cls;
                $content|write-host
                write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
                }
            }
        return $confirmed;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Install Secured Shell SSH on Windows

# Windows 10 & Server 2019 # Install the OpenSSH Server Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0…

HAProxy with Multiple SSL Certs

Method 1: --------- defaults   log 127.0.0.1 local0   option tcplog   frontend ft_test   mode http   bind…

Add Local Windows User

# Dynamic Credential$who = whoamiif ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;}else {$username=$who+"-admin";}$password = Read-Host -Prompt "Input the…