【发布时间】:2021-02-23 21:05:22
【问题描述】:
我有一个使用菜单和选择的 powershell 脚本。所以循环直到选择为0。对于一个选择,它使用“Test-NetConnection”测试输入的端口。但是由于某种原因,在运行之后,每当进行另一个选择时,似乎总是运行“Test-NetConnection”,横幅会显示在 powershell 中(即使它非常快,但不应该执行。
我删除了一些选择。而且我还有一些自定义功能。但不确定为什么会这样。任何帮助将不胜感激!
Powershell...
while ($selection -ne 0) {
Write-Output ""
Write-Output "__MENU__"
Write-Output "0. Exit"
Write-Output "1. Set Target Machines"
Write-Output "2. Set Save Directory"
Write-Output "4. Save MSINFO32"
Write-Output "5. Save All Installed Software"
Write-Output "9. Save GPO Info (gpresult)
Write-Output "10. Test WMI"
Write-Output "11. Test TCP Port"
Write-Output "14. Test Syslog"
Write-Output ""
Write-Output "Target Machines: $($targetComputers -Join ',')"
Write-Output "Save Directory: $savePath`n"
$selection = Read-Host -Prompt "Selection"
Write-Output ""
switch ($selection) {
0 { break }
1 { ... }
2 { ... }
11 {
$testPort = Read-Host -Prompt "TCP Port to Test (Leave blank for default: 80)"
Write-Output ""
if (($testPort -eq $null) -Or ($testPort -eq "")) { $testPort = 80 }
foreach ($computer in $targetComputers) {
try {
$netcon = Test-NetConnection -ComputerName $computer -Port $testPort -ErrorAction "Stop"
if($netcon.TcpTestSucceeded) { Write-Output "TCP Port $testPort test was successful on $computer." }
else { Write-Warning "TCP Port $testPort test was unsuccessful on $computer." }
}
catch { Write-Warning "TCP Port $testPort test was unsuccessful on $computer." }
}
} #11
14 {
$sysServer = Read-Host -Prompt "Syslog Server (Leave blank for default: ${env:computername})"
$sysProto = Read-Host -Prompt "Syslog Server Protocol (Leave blank for default: TCP)"
$sysPort = Read-Host -Prompt "Syslog Server Port (Leave blank for default: 514)"
Write-Output ""
if (($sysServer -eq $null) -Or ($sysServer -eq "")) { $sysServer = $env:computername }
if (($sysProto -eq $null) -Or ($sysProto -eq "")) { $sysProto = "TCP" }
if (($sysPort -eq $null) -Or ($sysPort -eq "")) { $sysPort = 514 }
sendSyslog -Server $sysServer -Protocol $sysProto -Port $sysPort
} #14
default { break }
} #switch
} #while
【问题讨论】:
-
请删除与问题无关的所有内容。您有大量不需要的代码,使分析变得不必要地复杂。 lookee ... 如何创建一个最小的、可重现的示例 - 帮助中心 - 堆栈溢出 — stackoverflow.com/help/minimal-reproducible-example
标签: powershell menu switch-statement selection