2

I have a problem with this script which i cant seem to figure out. But everytime i run my getfirewall function, if i press Q at the beginning to quit, it still goes through the next questions about if i want to enable or disable it. And if i do Q again there, then it brings me to main menu. Then once in main menu, if i press q again to quit, it repeats itself making me type q again to quit.

function GetFirewall
{
    $choice18 = read-host "Please enter the computer you wish you get the firewall profiles from"
   
    do{ try{$s1 = New-PSSession -ComputerName $choice18 -Credential domain.com\user
             $success = $true}
        catch{Write-Output "Wrong Password, Next attempt in 5 seconds"
              Start-sleep -Seconds 5}
        $count++
        }until($count -eq 5 -or $success)
        if(-not($success)){Mainmenu}


    $firewallP = icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
    $firewallP
    write-host "Domain(1)"
    write-host "Private(2)"
    write-host "Public(3)"
    write-host "All(4)"
    write-host "Quit(Q)"
    $choice19 = Read-host "Which do you want to modify or exit"
        
    write-host "Enable(1)"
    write-host "Disable(2)"
    $choice20 = read-host "Do you want to Enable or Disable the profile?"    
 
    switch($choice19)
    {

        1{ 
           Switch($choice20)
           {
                1{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Domain" -Enabled True}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                 }
                2{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Domain" -Enabled false}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                 }
           }
         }
        2{
            Switch($choice20)
           {
                1{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Private" -Enabled True}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                  }
                2{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Private" -Enabled false}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                 }
           } }
        3{
            Switch($choice20)
           {
                1{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Public" -Enabled True}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                }
                2{
                    icm -session $s1 -scriptblock{Set-NetFirewallProfile -Name "Public" -Enabled false}
                    icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}
                 }
           } 
         }
        4{
            switch($choice20)
            {
            1{ 
                icm -session $s1 -scriptblock{Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True}
                icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}}

            2{ 
                icm -session $s1 -scriptblock{Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False}
                icm -Session $s1 -scriptblock{Get-NetFirewallProfile -all |FT name, enabled}}
                   
            }
        }
        Q{Mainmenu}
    }
}
Function Mainmenu
{
Do {
    Write-Host "==========================="    
    Write-Host "        Main Menu          "
    Write-Host "==========================="    
    Write-Host "1: Active Directory Search"
    Write-Host "2: Service Administration"  
    Write-Host "3: Rename Remote PC"    
    Write-Host "4: Restart PC"
    Write-Host "5: Windows Update(Not Finished)"
    Write-Host "6: TestConnection"
    Write-Host "7: View Installed Apps(Almost Done)"
    Write-Host "8: View Firewall"
    Write-Host "Q: Quit"    
    Write-Host ''   
    $choice = Read-Host "Please select an option"   
    Switch ($choice) 
    {
        '1' 
        {       
            cls
            ActiveDirectoryInventory
        }
        '2' 
        {       
            cls
            Services
        }
        '3' 
        {
            cls
            RenamePC
        }
        '4' 
        {
            cls
            RestartPC
        }
        '5' 
        {
            cls
            WindowsUpdate
        }
        '6' 
        {
            cls
            Testconnect
        }
        '7'
        {
            cls
            GetApps
        }
        '8'
        {
            cls
            GetFirewall
        }
    }
}Until ($choice -eq 'Q')

}

1 Answer 1

1

Your GetFirewall function is called from function MainMenu.

Therefore, simply exit your GetFirewall function when the user presses Q, and you'll return to the main menu.

Therefore, change Q{Mainmenu} to:

Q { return }

Note: Since your switch statement isn't followed by other statements in the function, Q { break } - which exits just the switch statement - would work too; return instantly exits the function as a whole.

This approach avoids bloating the call stack, which is what your current approach does (MainMenu calls GetFirewall, which calls MainMenu again, ...) and also avoids the problem of returning to MainMenu calls previously placed on the call stack.

Sign up to request clarification or add additional context in comments.

3 Comments

Sweet, Thank you!! been bugging me for a while
@SteveG If you want additional "early exit" after the first question, it's as easy as adding if($choice19 -eq 'Q'){ return } on the line immediately below $choice19 = Read-Host ...
Glad to hear it helped, @SteveG; allow me to give you the standard advice to newcomers in the next comment:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.