0

Thank you to all who help me figure out what is going on.

The code I am using searches for the response for hog breeds and validates the response isn't garbage. If it's garbage the do loop repeats or exits based on response.

#Get the location of this script
$ScriptPath = (Split-Path $Myinvocation.MyCommand.path -Parent)

    $TemPath = "$ScriptPath\_Template"
    $NewTemPath = "$ScriptPath\_New_TEMPLATE"
    $EarNotchPath = "$ScriptPath\EarNocthes"
    $BoarPath = "$ScriptPath\_Boar_Samples"
    $SowPath = "$ScriptPath\_Sow_Samples"
    $GiltPath = "$ScriptPath\_Gilt_Samples"
    $BarrowPath = "$ScriptPath\_Barrow_Samples"
    $LittersPath = "$ScriptPath\_Litters_Samples"

Do {
#Create variables and collect information from user of script.
$CUST= Read-Host 'Enter Customer Name '
$BoarPath= Read-Host 'Enter Selected Boar Name (example: WildHog)'
$SowPath= Read-Host 'Enter Selected Sow Name (example: TrueBlue)'
$HogBreeds= Read-Host 'Enter Hereford, Yorkshire, Hampshire, Duroc, China_Poland'


#@Error Check $HogBreeds
    If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -cnotcontains $HogBreeds){

        If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -contains $HogBreeds){
            $TextInfo = (Get-Culture).TextInfo

            Switch ($HogBreeds) {
                {$_ -in 'Hereford','Yorkshire','Duroc'} { $HogBreeds = $HogBreeds.ToUpper() }
                'CHINA_Poland'       { $HogBreeds = $HogBreeds.Substring(0,7).ToUpper() + $HogBreeds.Substring(7,5).ToLower() }
            } 
            $Restart = 'StopDoLoop' 
        } Else {

          Write-Warning 'You didnt enter one of: Hereford, Yorkshire, Hampshire, Duroc, China_Poland or Your response was not recongized.' 
          $ANSWER = Read-Host 'Do you want to start over (Yes/No) - type Yes or No'
          If ($ANSWER -eq 'Yes') { $Restart = 'StopDoLoop'}
          If ($ANSWER -eq 'No') { Exit }
         }                 
    }

} Until ($Restart -eq 'StopDoLoop')

If I run this code with Windows PowerShell ISE Administrator the 'Do-While' loop executes with no problems. However, if I just right click on Do-While.ps1 script opening in PowerShell non-ISE the 'Do-While' loop repeats and never breaks. What gives?

Do you see any way to improve the code? I am also wondering about adding a string length check to the $Answer variable, and I fully admit I have not researched this other than a conversation I had with a friend.

RadFox
  • 419
  • 1
  • 4
  • 17

1 Answers1

0

I figured out the issue and now I have my code working and wanted to give you all the solution to the problem.

    #Run as Adminstrator

    If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

    {   
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments

    #Get the location of this script
    $ScriptPath = (Split-Path $Myinvocation.MyCommand.path -Parent)

        $TemPath = "$ScriptPath\_Template"
        $NewTemPath = "$ScriptPath\_New_TEMPLATE"
        $EarNotchPath = "$ScriptPath\EarNocthes"
        $BoarPath = "$ScriptPath\_Boar_Samples"
        $SowPath = "$ScriptPath\_Sow_Samples"
        $GiltPath = "$ScriptPath\_Gilt_Samples"
        $BarrowPath = "$ScriptPath\_Barrow_Samples"
        $LittersPath = "$ScriptPath\_Litters_Samples"

    Do {
    #Create variables and collect information from user of script.
    $CUST= Read-Host 'Enter Customer Name '
    $BoarPath= Read-Host 'Enter Selected Boar Name (example: WildHog)'
    $SowPath= Read-Host 'Enter Selected Sow Name (example: TrueBlue)'
    $HogBreeds= Read-Host 'Enter Hereford, Yorkshire, Hampshire, Duroc, China_Poland'


    #@Error Check $HogBreeds
        If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -cnotcontains $HogBreeds){

            If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -contains $HogBreeds){
                $TextInfo = (Get-Culture).TextInfo

                Switch ($HogBreeds) {
                    {$_ -in 'Hereford','Yorkshire','Duroc'} { $HogBreeds = $HogBreeds.ToUpper() }
                    'CHINA_Poland'       { $HogBreeds = $HogBreeds.Substring(0,7).ToUpper() + $HogBreeds.Substring(7,5).ToLower() }
                } 
                $Restart = 'StopDoLoop' 
            } Else {

              Write-Warning 'You didnt enter one of: Hereford, Yorkshire, Hampshire, Duroc, China_Poland or Your response was not recongized.' 
              $ANSWER = Read-Host 'Do you want to start over (Yes/No) - type Yes or No'
              If ($ANSWER -eq 'Yes') { $Restart = 'StopDoLoop'}
              If ($ANSWER -eq 'No') { Exit }
             }                 
        }

    } Until ($Restart -eq 'StopDoLoop')

}

Wrapping my not just my 'Do-While-Loop' but all my code in the first 'IF' statement has resolved the problem where I can now run all my code in an administrator PowerShell window without requiring PowerShell ISE.

I do hope this helps everyone who comes across this posting.

Thank you all for your help.

RadFox
  • 419
  • 1
  • 4
  • 17