Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 使用特定NIC测试连接性_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 使用特定NIC测试连接性

Powershell 使用特定NIC测试连接性,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我们需要更改几个服务器的DNS配置,在此之前,我需要测试这些服务器上的DNS端口。 问题是我们的服务器有多个NIC,因此我们需要确保DNS端口测试通过特定NIC,在这种情况下,将是已经配置了DNS的NIC 因此,我们需要检索NIC列表,确定哪个NIC配置了DNS,然后使用该特定NIC对新DNS服务器执行端口测试,并检查其是否正常 第二个问题是,我们的网络范围从Windows 2003到Windows 2019,因此该解决方案必须适用于从2.0到最新版本的不同PowerShell版本 对于较新的P

我们需要更改几个服务器的DNS配置,在此之前,我需要测试这些服务器上的DNS端口。 问题是我们的服务器有多个NIC,因此我们需要确保DNS端口测试通过特定NIC,在这种情况下,将是已经配置了DNS的NIC

因此,我们需要检索NIC列表,确定哪个NIC配置了DNS,然后使用该特定NIC对新DNS服务器执行端口测试,并检查其是否正常

第二个问题是,我们的网络范围从Windows 2003到Windows 2019,因此该解决方案必须适用于从2.0到最新版本的不同PowerShell版本

对于较新的PowerShell版本,这不会是一个问题,但由于我们有PS 2.0及更高版本,场景会发生变化

有人有或知道这个任务的任何例子吗

谢谢。

cmdlet

确实为此用例提供了一种机制

Test-Connection -Source 'SomeIPA' -Destination 'SomeDest'
任何操作系统中的本机ping也通过源开关提供这一功能

您可以使用

PowerShell最新版本中的cmdlet。--

---在v3以下,您将执行--

---或--

任何连接性测试都只能在具有连接性且路由正确的nic上进行。有几篇文章和问答讨论了您的用例。快速的网络搜索将向您显示它们

示例:

选择的适配器基于路由表。您可以检查 通过运行“路由打印”或“netstat-r”路由表。每个条目 将有一个指示路线权重的指标-较低 数字优先。您可以使用 “路由”命令。路线/?详情请参阅

路由添加(目标子网)掩码(目标子网掩码) (网关)IF(接口)

可能更容易的方法是使用-S开关,它允许您指定 源IP地址(有关更多信息,请参阅ping/?)

您可以做的另一件事是更改绑定顺序,这符合您的要求 “一个适配器优于另一个适配器的选项”。虽然它与 Windows操作系统到操作系统,它位于与此示例类似的位置:例如 Windows 7在网络上单击鼠标右键,然后单击“属性”。那你呢 单击“更改适配器设置”。然后单击菜单 高级>高级设置并移动您想要的连接 最高优先权

从Windows 7(版本6.1 Build 7601:Service Pack 1)ping/?用法: ping[-t][-a][-n计数][-l大小][-f][-i TTL][-v TOS] [-r计数][s计数][-j主机列表]|[-k主机列表]] [-w超时][-R][-S srcadr][-4][-6]目标\u名称

Ping使用特定的源IP地址可以设置套接字内容 语法是:


好的,错过了端口,但是,Test NetConnection不在传统PowerShell期间,因此不是选项,而且它也不提供源交换机

(Get-Command Test-Connection).Parameters.Keys
AsJob
DcomAuthentication
WsmanAuthentication
Protocol
BufferSize
ComputerName
Count
Credential
Source
Impersonation
ThrottleLimit
TimeToLive
Delay
Quiet
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable

(Get-Command Test-NetConnection).Parameters.Keys
ComputerName
TraceRoute
Hops
CommonTCPPort
Port
DiagnoseRouting
ConstrainSourceAddress
ConstrainInterface
InformationLevel
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
您可以选择使用.Net库。这就是传统PowerShell中对它的记录方式。使用诸如

---新对象Net.Sockets.TcpClient--

有关该命名空间的详细信息,请参见:

…将ping或测试连接的结果与连接到网络插座的管道相结合,以处理端口检查

例如(因为这有点有趣,所以我把它放在一起测试了一下-所以我知道它是有效的-在W2K3上没有,因为我已经十年没有这些了。):


ping和.Net命名空间也可以采用类似的方法。

到目前为止您尝试了什么?我可以通过配置dns来获取nic索引,但我正在搜索一个库,我可以在powershell 2.0中使用该库来测试连接性。解决方案必须在Windows 2003上运行,正如我在开始时所说的。我可以获取dapter问题在于使用使用该接口的测试连接命令。Ping与测试端口不同。从我的测试中,Ping for windows 2003中的-S参数仅适用于IPV6,test Connection中的-Source参数适用于Ping发起的计算机的名称,而不是interface/IP,因此这也不起作用.关于“Net.Sockets.tcp客户端”“,是的,我正在使用它,但问题仍然存在,无法绑定到特定NIC,这是我的主要问题。我发现最有希望的是将Net.IPEndPoint与Net.Sockets.TcpClient结合在一起,但仍然无法将其绑定到特定的NICI。最后,我终于让IPEndPoint工作了,我需要执行更多的测试,但到目前为止,它在2003中工作得很好。解决方案很简单,使用IPEndPoint对象并将其作为构造函数参数传递给TcpClient,之后只需处理可能抛出到IPEndPoint的无效地址,try/catch块就可以轻松解决此问题。很高兴您得到了一些有用的东西。遗留Windows是一种痛苦,到目前为止,我知道为什么人们会保留它,就像那些WinXP的人一样,但仍然如此。你知道我的意思。
netsh interface ipv4 show interfaces 
Get-WmiObject -Class  win32_networkadapter 
ping -S ip dest
ping -S 192.168.2.24 www.cyberciti.biz
ping -c 4 -S 10.207.0.54 1.1.1.1
(Get-Command Test-Connection).Parameters.Keys
AsJob
DcomAuthentication
WsmanAuthentication
Protocol
BufferSize
ComputerName
Count
Credential
Source
Impersonation
ThrottleLimit
TimeToLive
Delay
Quiet
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable

(Get-Command Test-NetConnection).Parameters.Keys
ComputerName
TraceRoute
Hops
CommonTCPPort
Port
DiagnoseRouting
ConstrainSourceAddress
ConstrainInterface
InformationLevel
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
$tcpObj = New-Object Net.Sockets.TcpClient
$tcpObj.Connect($Ipaddress,$Port)
Clear-Host
$SourceIpa = 'SomeSourceNicIpa'
$TargetDestination = 'Stackoverflow.com'
$TargetResponseCount = 1
$DestinationPort = 443

try
{
    $DestinationIpa = (Test-Connection -Source $SourceIpa -ComputerName $TargetDestination -Count $TargetResponseCount).ipv4address.ipaddressToString
    $tcpObj = New-Object Net.Sockets.TcpClient($DestinationIpa,$DestinationPort)

    if($tcpObj -ne $null)
    {
        "Using the source $SourceIpa. The port $DestinationPort on destination $TargetDestination is open."
        $tcpObj.close()
    }
}
catch
{
    Write-Warning -Message "Using the source $SourceIpa. The port $DestinationPort on destination $TargetDestination is not open."
    $tcpObj.close()
}