1

I have a script that requires a set of choices, it chooses one of those choices and repeats this process a set number of times. Then it will list the choice with the number of times it was selected.

The problem is, I want it to ask if the user would like to run the script again at the end, however, the script will skip the output of the results until the question is answered. Once the user answers the question, it will show the output. If the user answers Y, it will show the output 3 times, once from the original output, then a results with no count, then results of the newest input.

I have tried: Laying out the entire code in the order I want it to run. Creating a function just for the random selection Putting the entire script in a function then asking the question outside of the function and then calling the function again. Putting both the entire script and the question in separate functions and then calling each function after the other.

Tried putting the random selection in its own variable, then calling the variable.

I have provided an example code.

function LunchChoice {
    clear-host
    $lunchPlaces = @("Chick-Fil-A","PX - Food Court","Pig Place","Tropical Smoothie Cafe","Great Escape","PMS Deli")
    $countMin = 1
    $countMax = Read-host "`nHow many times do you want to run this?"

    $choices = "`nYour choices are:"
    Write-Host -ForegroundColor DarkYellow $choices
    $lunchPlaces

    Write-Host "`nRandomly selecting a choice " -ForegroundColor Green $countMax" times." 

    $countMin..$countMax | % {
        $lunchPlaces | Get-Random
    }  | Group-Object | Sort-Object Count -Descending | Select-Object Name, Count

    runAgain
    }

Function runAgain {
    $AskAgain = Read-Host "`nDo you want to do it again?"
    if ($AskAgain -eq "Y") {
        LunchChoice
        }
    }

LunchChoice
  • start with a `while` or a `do` loop. get that to work for your `want to do it again?` question. then, inside that loop, build your lunch choice routine. after you have that working, if you really want a function, add such. i would not bother, tho. [*grin*] – Lee_Dailey Nov 08 '19 at 18:49
  • `select-object` does not behave well with `write-host`, replace `select-object` with `format-table`. See https://stackoverflow.com/questions/38599976/powershell-get-childitem-output-delayed – zdan Nov 08 '19 at 19:08
  • @zdan That was it. Thank you so much. It works like a charm now. – Allen Scott Nov 08 '19 at 19:16

0 Answers0