none
Having Problems with Control Structures loops in my menu script RRS feed

  • Question

  • 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)

    Friday, October 18, 2019 6:25 PM

All replies

  • This is the easiest way to validate a date entry.

    while($true){
    	Try{
    		[datetime]$date1 = Read-Host 'Please enter the first date(format is mm/dd/yyyy)'
    		$date1
    		break
        }
    	Catch{
    		Write-Host 'Please enter format mm/dd/yyyy or m/d/yyyy' -fore red
        }
    }
    $date1.ToString('MM/dd/yyyy')
    


    \_(ツ)_/

    Friday, October 18, 2019 7:24 PM
    Moderator
  • These are all of the valid input string formats that can be converted directly to a date.  After capturing the date it can be converted easily to any string format as I have shown above.  String date formats are Locale sensitive. GetDateTimeFormats() will show all formats for the current Locale.

    $date1.GetDateTimeFormats()

    PS C:\> $date1.GetDateTimeFormats()
    9/1/2002
    9/1/02
    09/01/02
    09/01/2002
    02/09/01
    2002-09-01
    01-Sep-02
    Sunday, September 1, 2002
    September 1, 2002
    Sunday, 1 September, 2002
    1 September, 2002
    Sunday, September 1, 2002 12:00 AM
    Sunday, September 1, 2002 12:00 AM
    Sunday, September 1, 2002 0:00
    Sunday, September 1, 2002 00:00
    September 1, 2002 12:00 AM
    September 1, 2002 12:00 AM
    September 1, 2002 0:00
    September 1, 2002 00:00
    Sunday, 1 September, 2002 12:00 AM
    Sunday, 1 September, 2002 12:00 AM
    Sunday, 1 September, 2002 0:00
    Sunday, 1 September, 2002 00:00
    1 September, 2002 12:00 AM
    1 September, 2002 12:00 AM
    1 September, 2002 0:00
    1 September, 2002 00:00
    Sunday, September 1, 2002 12:00:00 AM
    Sunday, September 1, 2002 12:00:00 AM
    Sunday, September 1, 2002 0:00:00
    Sunday, September 1, 2002 00:00:00
    September 1, 2002 12:00:00 AM
    September 1, 2002 12:00:00 AM
    September 1, 2002 0:00:00
    September 1, 2002 00:00:00
    Sunday, 1 September, 2002 12:00:00 AM
    Sunday, 1 September, 2002 12:00:00 AM
    Sunday, 1 September, 2002 0:00:00
    Sunday, 1 September, 2002 00:00:00
    1 September, 2002 12:00:00 AM
    1 September, 2002 12:00:00 AM
    1 September, 2002 0:00:00
    1 September, 2002 00:00:00
    9/1/2002 12:00 AM
    9/1/2002 12:00 AM
    9/1/2002 0:00
    9/1/2002 00:00
    9/1/02 12:00 AM
    9/1/02 12:00 AM
    9/1/02 0:00
    9/1/02 00:00
    09/01/02 12:00 AM
    09/01/02 12:00 AM
    09/01/02 0:00
    09/01/02 00:00
    09/01/2002 12:00 AM
    09/01/2002 12:00 AM
    09/01/2002 0:00
    09/01/2002 00:00
    02/09/01 12:00 AM
    02/09/01 12:00 AM
    02/09/01 0:00
    02/09/01 00:00
    2002-09-01 12:00 AM
    2002-09-01 12:00 AM
    2002-09-01 0:00
    2002-09-01 00:00
    01-Sep-02 12:00 AM
    01-Sep-02 12:00 AM
    01-Sep-02 0:00
    01-Sep-02 00:00
    9/1/2002 12:00:00 AM
    9/1/2002 12:00:00 AM
    9/1/2002 0:00:00
    9/1/2002 00:00:00
    9/1/02 12:00:00 AM
    9/1/02 12:00:00 AM
    9/1/02 0:00:00
    9/1/02 00:00:00
    09/01/02 12:00:00 AM
    09/01/02 12:00:00 AM
    09/01/02 0:00:00
    09/01/02 00:00:00
    09/01/2002 12:00:00 AM
    09/01/2002 12:00:00 AM
    09/01/2002 0:00:00
    09/01/2002 00:00:00
    02/09/01 12:00:00 AM
    02/09/01 12:00:00 AM
    02/09/01 0:00:00
    02/09/01 00:00:00
    2002-09-01 12:00:00 AM
    2002-09-01 12:00:00 AM
    2002-09-01 0:00:00
    2002-09-01 00:00:00
    01-Sep-02 12:00:00 AM
    01-Sep-02 12:00:00 AM
    01-Sep-02 0:00:00
    01-Sep-02 00:00:00
    September 1
    September 1
    2002-09-01T00:00:00.0000000
    2002-09-01T00:00:00.0000000
    Sun, 01 Sep 2002 00:00:00 GMT
    Sun, 01 Sep 2002 00:00:00 GMT
    2002-09-01T00:00:00
    12:00 AM
    12:00 AM
    0:00
    00:00
    12:00:00 AM
    12:00:00 AM
    0:00:00
    00:00:00
    2002-09-01 00:00:00Z
    Sunday, September 1, 2002 4:00:00 AM
    Sunday, September 1, 2002 04:00:00 AM
    Sunday, September 1, 2002 4:00:00
    Sunday, September 1, 2002 04:00:00
    September 1, 2002 4:00:00 AM
    September 1, 2002 04:00:00 AM
    September 1, 2002 4:00:00
    September 1, 2002 04:00:00
    Sunday, 1 September, 2002 4:00:00 AM
    Sunday, 1 September, 2002 04:00:00 AM
    Sunday, 1 September, 2002 4:00:00
    Sunday, 1 September, 2002 04:00:00
    1 September, 2002 4:00:00 AM
    1 September, 2002 04:00:00 AM
    1 September, 2002 4:00:00
    1 September, 2002 04:00:00
    September 2002
    September 2002


    \_(ツ)_/


    Friday, October 18, 2019 7:28 PM
    Moderator
  • Thanks, also how would you fix my if statements at the end?
    Friday, October 18, 2019 7:34 PM
  • Same way. Put it in a loop and test the response and break when the correct value is entered.


    \_(ツ)_/

    Friday, October 18, 2019 7:38 PM
    Moderator
  • First, you need to correct that last part of your code. Using "-ne" and "-or" as you did will fail becasue if $terminate has a value of "Y" it will return $true because it isn't "N" (and vice versa). Use "-notmatch" instead, or just eliminate the test for "N" because you "exit" if $terminate is "N" before getting to the test!

        [String]$terminate = Read-Host "Do you wish to continue (Y/N)"
    
        if ($terminate -eq "N") {
            exit;
        }
        elsif ( $terminate -notmatch "Y|N" ) {
            Write-host "Invalid Response"
        }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Friday, October 18, 2019 7:52 PM
  • Here is a cleaner way to build short text menus.

    $options = [System.Management.Automation.Host.ChoiceDescription[]] @('&Yes', '&No')
    switch ($host.UI.PromptForChoice('Script ending' , 'Do you wish to quit?' , $Options,1)){
    	0 { 
    		'you chose Yes' 
    	}
    	1 {
    		'you chose No' 
    	}
    }
    


    \_(ツ)_/

    Friday, October 18, 2019 7:52 PM
    Moderator
  • So should look something like this at the end?:

    $GroupArray = @(
        'a'
        'b'
        'c'
    )
    
    do {
    cls
    while($true){
    	Try{
    		[datetime]$date1 = Read-Host 'Please enter Date 1(format is mm/dd/yyyy)'
    		$date1
    		break
        }
    	Catch{
    		Write-Host 'Please enter format mm/dd/yyyy' -fore red
        }
    }
    $date1.ToString('MM/dd/yyyy')
    cls
    while($true){
    	Try{
    		[datetime]$date2 = Read-Host 'Please enter Date 2(format is mm/dd/yyyy)'
    		$date2
    		break
        }
    	Catch{
    		Write-Host 'Please enter format mm/dd/yyyy' -fore red
        }
    }
    $date2.ToString('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
    
    while($true){
    	Try{
    		[String]$terminate = Read-Host 'Do you wish to continue (Y/N)'
    		$terminate
    if ($terminate -eq "N") {
    
    exit;
    }
    		break
        }
    	Catch{
    		Write-Host 'Invalid Response' -fore red
        }
    
    }
    
    }while($true)
    Anything that should clean up in the script/and/or fix?


    • Edited by jw933 Friday, October 18, 2019 8:12 PM
    Friday, October 18, 2019 7:57 PM
  • Anything that should clean up in the script/and/or fix?

    Yes - fix the formatting of the code for consistency and radability:

    The PowerShell Best Practices and Style Guide

    Also:


    \_(ツ)_/

    Friday, October 18, 2019 8:06 PM
    Moderator
  • Anything else you see that is wrong, particularly with the if statements/loops at the end?
    Friday, October 18, 2019 8:09 PM
  • $options = [System.Management.Automation.Host.ChoiceDescription[]]@(
    	'&FIrst Report/Our Tasks',
    	'&Second Report/Our Tasks',
    	'&Combined Report',
    	'Combined Report (&All Tasks)',
    	'E&xit'
    )
    switch ($host.UI.PromptForChoice('Reports Menu' , 'Select report' , $options, 4)){
    	0 { 
    		# code here
    	}
    	1 {
    		# code here
    	}
    	2 {
    		# code here
    	}
    	3 {
    		# code here
    	}
    	4 {
    		# code here
    	}
    }


    \_(ツ)_/




    Friday, October 18, 2019 8:17 PM
    Moderator
  • the elsif didnt work out.
    Friday, October 18, 2019 8:29 PM
  • Hey what is wrong with this part of the code?

    while($true){
    	Try{
    		[String]$terminate = Read-Host 'Do you wish to continue (Y/N)'
    		$terminate
    if ($terminate -eq "N") {
    
    exit;
    }
    		break
        }
    	Catch{
    		Write-Host 'Invalid Response' -fore red
        }
    
    }
    
    }while($true)

    Friday, October 18, 2019 8:32 PM
  • the elsif didnt work out.

    Please take time to learn basic PowerShell. This forum cannot provide personal instructions in how to write basic PowerShell code.

    while($true){
    	if('N' -eq (Read-Host 'Do you wish to continue (Y/N)')){
    	    exit
    	}else{
    break
    } }
    Without the basics you will end up being mislead due to your lack of knowledge and understanding and that cannot be fixed in a forum.  It is incumbent on you to learn basic PowerShell and coding.  I posted some links to resources.  Please avail yourself of these resources.


    \_(ツ)_/



    Friday, October 18, 2019 8:36 PM
    Moderator
  • Hey what is wrong with this part of the code?

    It shows a total lack of knowledge of coding. You are just copying without understanding. If you take time to actually learn the basics then everything that has been proposed will be understandable and yo will be able to correctly ask specific questions.

    Please learn basic PowerShell.  We cannot give you personal instruction one line at a time.  That is not the purpose of this forum.

    Please read the forum rules posted at the very top of the forum list:


    \_(ツ)_/

    Friday, October 18, 2019 8:59 PM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:30 PM
    Moderator