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