我对这个脚本有一个问题,我似乎无法解决。但每次我运行我的getfirewall功能时,如果我一开始按Q退出,它仍然会处理下一个问题,即我是想启用还是禁用它。如果我在那里再次执行Q,它会将我带到主菜单。然后在主菜单中,如果我再次按q退出,它会重复,使我再次键入q退出。
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.comuser
$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')
}
您的GetFirewall
函数从函数MainMenu
调用。
因此,当用户按下Q
时,只需退出GetFirewall
功能,即可返回主菜单。
因此,将Q{Mainmenu}
更改为:
Q { return }
注意:由于函数中的switch
语句后面没有其他语句,因此Q { break }
(仅退出switch
语句)也可以工作;return
立即作为一个整体退出该功能。
这种方法避免了当前方法所做的调用堆栈膨胀(MainMenu
调用GetFirewall
,它再次调用MainMenu
,…),还避免了返回到先前放置在调用堆栈上的MainMenu
调用的问题。