当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

作者:精品下载站 日期:2024-12-14 04:59:10 浏览:15 分类:玩电脑

PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南


[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

PowerShell Test-NetConnection简介

测试网连接用于与连接相关的各种信息,例如诊断和连接性。在 PowerShell v4.0 之前,有不同的工具可用于解决与网络相关的不同问题。为了避免这种情况,引入了此 cmdlet 作为单点源来解决各种与连接相关的问题。此 cmdlet 支持的各种诊断包括路由跟踪和选择诊断、ping 和 TCP。根据提供的输入,输出范围包括各种信息,例如 DNS 查找结果、配置的各种 IP 地址和安全规则、源和目标 IP 地址选择结果以及与连接相关的信息。本文将详细介绍测试网连接、其参数及其用法。

语法:

NAME
Test-NetConnection

语法:

Test-NetConnection [[-ComputerName] <string>] [-TraceRoute] [-Hops <int>] [-InformationLevel {Quiet | Detailed}]
[<CommonParameters>]
Test-NetConnection [[-ComputerName] <string>] [-CommonTCPPort] {HTTP | RDP | SMB | WINRM} [-InformationLevel {Quiet | Detailed}]
[<CommonParameters>]
Test-NetConnection [[-ComputerName] <string>] -Port <int> [-InformationLevel {Quiet | Detailed}]  [<CommonParameters>]
Test-NetConnection [[-ComputerName] <string>] -DiagnoseRouting [-ConstrainSourceAddress <string>] [-ConstrainInterface <uint32>]
[-InformationLevel {Quiet | Detailed}]  [<CommonParameters>]
ALIASES
TNC

例如:

输入:

Test-NetConnection

输出:

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

PowerShell Test-NetConnection 的参数

  • CommonTcpPort:

这表示要检查的 tcp 端口号。该参数的数据类型是字符串。这不是强制参数。该cmdlet中参数的位置是1。它不接受pipline和通配符作为输入。此参数唯一可接受的值是 SMB、HTTP、WINRM 和 HTTP。

  • 计算机名称:

这指定目标系统的 DNS 或 IP 地址。该参数的数据类型为字符串。也可以使用RemoteAddress、cn 进行引用。此参数在 cmdlet 中的位置为零。默认值为无。它接受管道输入,但不允许使用通配符。

  • -约束接口:

这表示过去用于诊断路由的机制。该参数的数据类型为Uint32。默认值为无。它不接受管道字符和通配符作为输入。

  • -约束源地址:

这表示用于诊断源地址的约束。该参数的数据类型为字符串。默认值为无。它不接受管道字符和通配符作为输入。

  • -诊断路由:

这表示诊断从源运行到目标。该参数的数据类型为字符串。默认值为无。它不接受管道字符和通配符作为输入。

  • -啤酒花:

这表示作为遍历跟踪路由 cmdlet 的一部分应执行的跳转次数。该参数的数据类型为int32。默认值为无。它不接受管道字符和通配符作为输入。

  • -信息级别:

这表示应显示的信息类型。该参数的数据类型是字符串。默认值为无。它不接受管道字符和通配符作为输入。以下是可以提供给此参数的值;细致而安静。如果值为quiet,则仅返回基本信息。

  • -端口:

这表示要检查连接的远程计算机的端口号。也可以参考使用Remoteport。该参数的数据类型为int32。默认值为无。它接受管道输入,但不允许使用通配符。

  • -跟踪路线:

这表示在目标计算机上运行的连接测试。该参数的数据类型为switch。默认值为 none 它不接受管道字符和通配符作为输入。

在Test-Netconnection之前,之前的版本是Test-Connection。它具有缓冲区大小和延迟等参数,可用于定义连续 ping 之间的秒数。

示例

让我们讨论 PowerShell Test-NetConnection 的示例。

示例#1:获取详细且安静的信息

输入:

Write-Host "Welcome to demo of test-netconnection" -ForegroundColor Green
Write-Host "Running the command with informational level"
$infolevel=Read-Host "specify the information level"
Write-Host "The speified information level is" $infolevel -ForegroundColor Green
Test-NetConnection -InformationLevel $infolevel
$infolevel=Read-Host "specify the information level"
Write-Host "The speified information level is" $infolevel -ForegroundColor Green
Test-NetConnection -InformationLevel $infolevel

输出:

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

安静信息级别仅返回二进制值。如果连接成功则返回 true,如果连接不存在则返回 false。

例子#2

使用端口号检查远程连接并 ping 远程服务器

输入:

Write-Host "testing conenction using port number" -ForegroundColor Green
$infolevel= Read-Host "Enter the information level to be used"
$portno= Read-Host "Enter the port no to check"
Write-Host "The details are as follows" -ForegroundColor Green
Test-NetConnection -Port $portno -InformationLevel $infolevel
Write-Host "Demo of reching out to remote computer"
$remotecmp= Read-Host "Enter the remote computer details"
$infolevel1= Read-Host "Enter the info level to be used"
Write-Host "The details are as follows" -ForegroundColor Green
Test-NetConnection -ComputerName $remotecmp -InformationLevel $infolevel1

输出:

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

例子#3

输入:

Write-Host "Demo to see if a port is open" -ForegroundColor Green
$rs= Read-Host "enter the remote server"
Test-NetConnection -ComputerName $rs -CommonTCPPort HTTP
$portno=Read-Host "enter the port no"
$ipadd= Read-Host "enter the ip address"
$con = New-Object System.Net.Sockets.TcpClient($ipadd, $portno)
if ($connection.Connected) {
Write-Host "connection is succeeded" -ForegroundColor Green
} else {
Write-Host "connection is failed" -ForegroundColor Red
}

输出:

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

例子#4

输入:

Write-Host "Demo of trace route in test-netonnection"
$servername= Read-Host "enter the server name"
Test-NetConnection -ComputerName $servername -TraceRoute
Write-Host "With detailed information" -ForegroundColor Green
Test-NetConnection -ComputerName $servername -TraceRoute -InformationLevel Detailed
Write-Host "demo of diagnostic routing"
$sname= Read-Host "enter the site address"
Test-NetConnection -ComputerName $sname -DiagnoseRouting
Write-Host "With detailed information" -ForegroundColor Green
Test-NetConnection -ComputerName $sname -DiagnoseRouting -InformationLevel Detailed

输出:

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

[玩转系统] PowerShell 测试-NetConnection | PowerShell Test-NetConnection 指南

结论

因此,本文详细解释了 Test-NetcOnnection cmdlet。它涵盖了可以使用它的各种场景以及适当的示例。它还解释了与 cmdlet 关联的各种参数。要详细了解更多信息,建议编写并执行示例脚本。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯