You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本GetFirewall函数及主菜单循环退出逻辑异常问题

PowerShell脚本逻辑问题修复方案

问题1:GetFirewall函数按Q退出仍弹出启用/禁用询问

问题原因

代码执行顺序逻辑错误:读取配置选择$choice19后,未先判断是否为退出选项Q,就直接执行了启用/禁用选择的读取步骤,导致即使按下Q也会先弹出第二个询问。

修复方法

读取$choice19后立即判断值,如果为Q直接返回主菜单,跳过后续启用/禁用选择逻辑。

问题2:主菜单按两次Q才能退出

问题原因

函数嵌套调用导致多层菜单循环:当在GetFirewall中调用Mainmenu函数时,会在原有主菜单循环的基础上新增一层嵌套的主菜单循环,第一次按Q仅退出内层嵌套的主菜单,回到外层循环后需要第二次按Q才能退出外层主菜单。密码输错5次时调用Mainmenu也会触发相同的嵌套问题。

修复方法

子函数中不要直接调用Mainmenu,改为使用return语句返回上一层调用,由外层的主菜单循环自行处理后续流程。

修改后的核心代码

修正后的GetFirewall函数

function GetFirewall
{
    $choice18 = read-host "Please enter the computer you wish you get the firewall profiles from"
    $success = $false
    $count = 0
    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,直接返回上一层
            return
        }


    $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"

    # 新增判断:如果选择Q直接返回
    if($choice19 -eq 'Q'){
        return
    }
        
    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}}
                   
            }
        }
    }
}

主菜单函数无需修改,保持原有逻辑即可

内容的提问来源于stack exchange,提问作者user16881248

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 16:24:03