I created a menu on Pshell to excute the different options based on the user's response. But I want to handle error handling for dates (i.e it is not in the correct format
mm/dd/yyyy) and the end after a cycle through my do loop, a user chooses a option whether or not they want to continue with the program or not. When I try to
prompt the user if they wish to continue at the end,if I type in nonsense like "sdgahadhd" it won't display what I show which is invalid response and go back to "Do you wish to continue(Y/N)"
Write-host "Invalid Response"
Also when Im going through the do while loop for a second round, it wont prompt anymore
about dates and go directly to
"Please enter 1 of the following 5 selections (First Report/Our Tasks,Second Report/Our Tasks, Combined Report/Our Tasks, Combined Report/All Tasks,Exit)"
which is an issue because I want it to continually prompt me for dates in my do while loop.
Is there a way where I can fix my script to meet those conditions as described above? Thanks!
My script
$GroupArray = @(
'a'
'b'
'c'
)
do {
cls
while ($Date1 -notmatch '^\d{2}\/\d{2}\/\d{4}$') {
$Date1 = Read-Host "Please enter the first date(format is mm/dd/yyyy)"
}
cls
while ($Date2 -notmatch '^\d{2}\/\d{2}\/\d{4}$') {
$Date2 = Read-Host "Please enter the second date(format is mm/dd/yyyy)"
}
cls
[String]$Response = Read-Host "Please enter 1 of the following 5 selections (First Report/Our Tasks,Second Report/Our Tasks, Combined Report/Our Tasks, Combined Report/All Tasks,Exit)"
cls
switch($Response) {"FirstReport/Our Tasks" {Import-CSV -Path 'C:\Temp\FirstReport.csv' | Where { ([DateTime]$_.'Date 1' -ge [DateTime]$Date1) -and ([DateTime]$_.'Date 2' -le [DateTime]$Date2) -and ($_.'Assignment Tasks' -in $GroupArray) } | Export-Excel -Path "C:\Temp\FirstReportDate.xls" -Title "First Report";pause;break}"Second Report/Our Tasks" {Import-CSV -Path 'C:\Temp\SecondReport.csv' | Where { ([DateTime]$_.'Date 1' -ge [DateTime]$Date1) -and ([DateTime]$_.'Date 2' -le [DateTime]$Date2) -and ($_.'Assignment Tasks' -in $GroupArray) } | Export-Excel -Path "C:\Temp\SecondReportDate.xls" -Title "Second Report";pause;break}"Combined Report/Our Tasks" {Import-CSV -Path 'C:\Temp\CombinedReport.csv' | Where { ([DateTime]$_.'Date 1' -ge [DateTime]$Date1) -and ([DateTime]$_.'Date 2' -le [DateTime]$Date2) -and ($_.'Assignment Tasks' -in $GroupArray) } | Export-Excel -Path "C:\Temp\Combinedreportdate.xls" -Title "Combined Report";pause;break}"Combined Report/All Tasks" {Import-CSV -Path 'C:\Temp\Combinedreport.csv' | Where { ([DateTime]$_.'Date 1' -ge [DateTime]$Date1) -and ([DateTime]$_.'Date 2' -le [DateTime]$Date2) } | Export-Excel -Path "C:\Temp\CombinedreportDate.xls" -Title "Combined Report"}"Exit" {Write-host "Exiting script now";pause;exit}
default {Write-host "Invalid Selection"; pause;break}
}
cls
[String]$terminate = Read-Host "Do you wish to continue (Y/N)"
if ($terminate -eq "N") {
exit;
}
elsif ($terminate -ne "Y") -or ($terminate -ne "N") {
Write-host "Invalid Response"
}
}while($true)