powershell - nics相关的路由
问题描述
我在用着
Get-WmiObject Win32_IP4PersistedRouteTable
要获得持久的路由条目,我需要将这些路由条目与它们重新关联的接口相关联,有什么方法可以为每个对应的持久路由返回 NIC?
谢谢你。
解决方案
这不是 PowerShell 代码问题或限制。这是 .Net 限制。此类中没有允许您识别关联的 NIC 接口的属性。
操作系统将持久化路由保存在注册表中……此类仅适用于 IPv4,不返回 IPX 或 IPv6 数据。
[dynamic, provider("RouteProvider"), UUID("{2CAF4666-AC9B-45AB-00A6-AF8C537794C2}"), SupportsCreate, CreateBy("PutInstance"), SupportsDelete, DeleteBy("DeleteInstance"), AMENDMENT]
class Win32_IP4PersistedRouteTable : CIM_LogicalElement
{
string Caption; # This property is inherited from CIM_ManagedSystemElement.
string Description;
string Destination;
datetime InstallDate;
string Mask;
sint32 Metric1;
string Name;
string NextHop;
string Status;
};
根据我们的以下线程更新
所以,你问...
'无论如何要为每个通信者持久路由返回 NIC?
...正如我所说,答案是我不知道的,只使用你所展示的。您必须获取该数据并将其与其他网络 cmdlet 的其他结果进行比较。
WMI IP Route Provider 和网络类为 IPv4 地址提供数据。从 Windows Vista 开始,WMI 还提供对 IPv6 网络功能的有限支持。
WMI IP 数据 以下类仅提供 IPv4 数据:
Win32_IP4路由表
Win32_IP4PersistedRouteTable
Win32_IP4RouteTableEvent
Win32_ActiveRoute
Win32_NetworkAdapter
尽管有 PowerShell。关键属性是可读的。
VBSCript
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4PersistedRouteTable",,48)
Dim objItem 'as Win32_IP4PersistedRouteTable
For Each objItem in colItems
WScript.Echo "Caption: " & objItem.Caption
WScript.Echo "Description: " & objItem.Description
WScript.Echo "Destination: " & objItem.Destination
WScript.Echo "InstallDate: " & objItem.InstallDate
WScript.Echo "Mask: " & objItem.Mask
WScript.Echo "Metric1: " & objItem.Metric1
WScript.Echo "Name: " & objItem.Name
WScript.Echo "NextHop: " & objItem.NextHop
WScript.Echo "Status: " & objItem.Status
WScript.Echo ""
Next
属性掩码为字符串(只读)
Mask 属性包含此持久条目中使用的掩码。在与 ipRouteDest 字段中的值进行比较之前,掩码应与目标地址进行逻辑与运算。
属性 NextHop As string(只读)
NextHop 属性包含此持久路由的下一跃点的 IP 地址。(在路由绑定到通过广播媒体实现的接口的情况下,该字段的值是该接口上代理的 IP 地址。)。
Windows 中的工具(route、netsh 等)使用这些类来完成它们的工作。
如果有您要查找的信息,您可以只解析路由打印命令:
PowerShell 对象的 Cmd.exe (DOS) 工具
$tmpRoute = (route print | ? { $_.trimstart() -like "0.0.0.0*" }).split() | ? { $_ }
$route = @{
'Destination' = $tmpRoute[0];
'Netmask' = $tmpRoute[1];
'Gateway' = $tmpRoute[2];
'Interface' = $tmpRoute[3];
'Metric' = $tmpRoute[4];
}
$route
$computer = 'localhost'
$wmi = get-wmiobject -namespace root\StandardCimv2 -computername 'localhost' -Query "Select * from MSFT_NetRoute"
$wmi | ? { $_.DestinationPrefix -eq '0.0.0.0/0' } |
Select DestinationPrefix, InterfaceAlias, InterfaceIndex, InterfaceMetric, NextHop, RouteMetric
...显示对该命名空间的直接 WMI 调用...
# WMI data
$wmi_routes = Get-WmiObject @WMIHast -Class win32_ip4RouteTable
$wmi_persistedroutes = Get-WmiObject @WMIHast -Class win32_IP4PersistedRouteTable
foreach ($iproute in $wmi_routes)
{
$Persistant = $false
foreach ($piproute in $wmi_persistedroutes)
{
if (($iproute.Destination -eq $piproute.Destination) -and
($iproute.Mask -eq $piproute.Mask) -and
($iproute.NextHop -eq $piproute.NextHop))
{
$Persistant = $true
}
}
$RouteProperty = @{
'InterfaceIndex' = $iproute.InterfaceIndex
'Destination' = $iproute.Destination
'Mask' = $iproute.Mask
'NextHop' = $iproute.NextHop
'Metric' = $iproute.Metric1
'Persistent' = $Persistant
'Type' = $RouteType[[int]$iproute.Type]
}
$Routes += New-Object -TypeName PSObject -Property $RouteProperty
}
...
甚至使用该类直接设置持久路由。
# Set objWMIClass = objWMIServices.Get (cWMIIPPersistedRouteClass)
$class = gwmi -list win32_ip4persistedroutetable -EnableAll -Computer remotehost1
#Set objWMIPersistentInstance = objWMIClass.SpawnInstance_
$newroute = $class.CreateInstance()
#objWMIPersistentInstance.Destination = strDestination
$newroute.Destination = $destination
#objWMIPersistentInstance.Mask = strMask
$newroute.Mask = $mask
#objWMIPersistentInstance.NextHop = strNextHop
$newroute.NextHop = $nexthop
#objWMIPersistentInstance.Metric1 = intMetric
$newroute.Metric1=$metric
$newroute.Put()
$newroute.Destination='10.10.10.101'
$newroute.Mask='255.255.255.0'
$newroute.NextHop='10.10.10.1'
$newroute.Metric1=30
$newroute.Put()
推荐阅读
- dialogflow-es - 是否可以创建一个所有数据都存储在欧盟的谷歌助手?(DSGVO?)
- notepad++ - 如何在记事本++中找到小于另一个值的值?
- python - Scrapy:Itemloader处理器/方法TypeError:'ItemMeta'对象不可下标
- javascript - 向表单中动态生成的文本框添加 jquery 验证
- python - 返回 xy 坐标的 z 值
- php - 如何使用 .htaccess 将 url 从 a 重写为 b
- spring - PCF 上 Spring Boot 中的 Hibernate 验证器问题
- reactjs - 如何使用 typescript 模拟或断言 window.alert 是否已在 React & Jest 中触发?
- javascript - 如何阻止用户输入一些字符来输入?
- python - Scrapy 204 没有收到数据