本帖最后由 ckjshi 于 2022-1-7 11:30 编辑
修改原因:在Windows7报错,在Windows10运行一切正常。
我想让此脚本在Windows7、Windows10、Windows11都可以正常运行,能否实现?
提前感谢大家,给我提供帮助。
在Windows7报错这样的错误- 方法调用失败,因为 [System.Net.Sockets.TcpClient] 不包含名为“Dispose”的方法。
- 所在位置 行:41 字符: 27
- + $tcpClient.Dispose <<<< ()
- + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeEx
- ception
- + FullyQualifiedErrorId : MethodNotFound
复制代码 以下是脚本源代码- www.sohu.com NULL
- 192.168.1.111 3389
- 192.168.41.233 NULL
- www.12306.cn 443
复制代码
- <#*,:&cls
- @echo off
- netsh advfirewall set allprofiles state off
- if exist %windir%\system32\telnet.exe (echo (Telnet)已安装,现在开始测试,请耐心等待(测试结果)后,拍照或截图发送给我们。) else (dism /Online /Enable-Feature /FeatureName:TelnetClient)
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- pause
- exit /b
- #>
-
- # IP_List
- echo " "
- echo " "
- $ipListFile = ".\IP_list.txt"
- # Record output
- $outfile = ".\Record.txt"
- # output format
- $format = "{0} - {1} - {2}"
-
- $ping = New-Object System.Net.NetworkInformation.Ping
- Get-Content -Path $ipListFile | ForEach-Object {
- $_ | Write-Host
- $arrIPPort = $_ -split '\s+'
- if ($arrIPPort.Count -gt 1) {
- $ip = $arrIPPort[0]
- $port = $arrIPPort[1]
-
- $pingReply = $ping.Send($ip)
- if ($pingReply.Status -eq 'Success') {
- $pingResult = 'ping通'
- } else {
- $pingResult = 'ping不通'
- }
- try {
- $tcpClient = New-Object System.Net.Sockets.TcpClient -ArgumentList @($ip, $port)
- $tcpResult = "${port}端口通"
- } catch {
- $tcpResult = "${port}端口不通"
- } finally {
- if ($tcpClient) {
- $tcpClient.Dispose()
- }
- }
- $format -f $ip, $pingResult, $tcpResult
- }
- } | Set-Content -Path $outfile
- $ping.Dispose()
- echo " "
- echo " "
- echo "--------------------测试结果--------------------"
- echo " "
- type ".\Record.txt"
复制代码
|