Powershell:返回 NIC "Windows Name"而不是"Device Name"



各位下午好。

我有一个脚本,它提供了一堆关于新建服务器的信息。有一件事我想不通,那就是如何让它显示";Windows名称";"VLAN 111"或"内部备份VLAN"(而不是下面在";描述";。我错过了什么?

Foreach ($objAdapter in $colAdapters)
{
if ($objAdapter.IPEnabled -eq "True")
{
$NICCount = $NICCount + 1
If ($NICCount -gt 1)
{
$Report+=  " </TABLE>"
$Report+=  " <DIV class=Solidfiller></DIV>"
$Report+=  " <TABLE>"
}
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>Description</b></font></th>"
******* $Report+=  " <td width='75%'>$($objAdapter.Description)</font></td>"
$Report+=  " </tr>"
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>Physical address</b></font></th>"
$Report+=  " <td width='75%'>$($objAdapter.MACaddress)</font></td>"
$Report+=  " </tr>"
If ($objAdapter.IPAddress -ne $Null)
{
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>IP Address / Subnet Mask</b></font></th>"
$Report+=  " <td width='75%'>$(gwmi -ComputerName $Target Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true } | Foreach-Object { $_.IPAddress } | Foreach-Object { [IPAddress]$_ } | Where-Object { $_.AddressFamily -eq 'Internetwork' } | Foreach-Object { $_.IPAddressToString }) / $($objAdapter.IPSubnet)</font></td>"
$Report+=  " </tr>"
$Report+=  " </tr>"
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>Default Gateway</b></font></th>"
$Report+=  " <td width='75%'>$($objAdapter.DefaultIPGateway)</font></td>"
$Report+=  " </tr>"
}
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>DHCP enabled</b></font></th>"
If ($objAdapter.DHCPEnabled -eq "True")
{
$Report+=  " <td width='75%'>Yes</font></td>"
}
Else
{
$Report+=  " <td width='75%'>No</font></td>"
}
$Report+=  " </tr>"
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>DNS Servers</b></font></th>"
$Report+=  " <td width='75%'>"
If ($objAdapter.DNSServerSearchOrder -ne $Null)
{
$Report+=  " $($objAdapter.DNSServerSearchOrder) "
}
$Report+=  " </tr>"
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>Primary WINS Server</b></font></th>"
$Report+=  " <td width='75%'>$($objAdapter.WINSPrimaryServer)</font></td>"
$Report+=  " </tr>"
$Report+=  " <tr>"
$Report+=  " <th width='25%'><b>Secondary WINS Server</b></font></th>"
$Report+=  " <td width='75%'>$($objAdapter.WINSSecondaryServer)</font></td>"
$Report+=  " </tr>"
$NICCount = $NICCount + 1
}
}

我在脚本中设置得更高的delcaration是:

Write-Output "..Network Configuration"
$NICCount = 0
$colAdapters = gwmi -ComputerName $Target Win32_NetworkAdapterConfiguration

结果显示:
描述:适用于Windows x64的Cisco AnyConnect安全移动客户端虚拟微型端口适配器
物理地址:00:05:9A:3C:7A:00
IP地址/子网掩码:192.168.240.146 192.168.116.1 192.168.153.1 10.20.30.105/255.255.255.0
默认网关192.168.240.1
启用DHCP:否
DNS服务器:8.8.8.8 8.8.4.4

我知道我需要更改的行标有***,但我就是不知道我需要做什么。如果我需要更改我调用的整个命令声明以获得相同的结果,我肯定会参与其中。

提前感谢您的帮助!

那么Win32_NetworkAdapter类包含您想要的值作为NetConnectionId属性?

我们可以将Win32_NetworkAdapter的结果与InterfaceIndex的值进行保存,并将Win32_NetworkAdapterConfigurationWin32_NetworkAdapterInterfaceIndex进行比较,以找到正确的适配器并获得其NetConnectionId值。

您将此添加到您的上级申报中

Write-Output "..Network Configuration"
$NICCount = 0
$colAdapters = gwmi -ComputerName $Target Win32_NetworkAdapterConfiguration
#### NEW CODE START ####
########################
# this will save the results of Win32_NetworkAdapter with the InterfaceIndex which will be used to match against, and the NetConnectionId which will replace your table value
$networkAdapters = gwmi Win32_NetworkAdapter | Select -Property Name, InterfaceIndex, NetConnectionID
##### NEW CODE END #####
########################

并将其添加到循环的开头

foreach ($objAdapter in $colAdapters)
{

#### NEW CODE START ####
########################
# loop through the results of $networkAdapters and compare the InterfaceIndex of Win32_NetworkAdapterConfiguration against Win32_NetworkAdapter
# if they match, we save the NetConnectionId as a new property for $objAdapter which can be accessed as $objAdapter.NetConnectionId
foreach ($adapter in $networkAdapters)
{    
if ($objAdapter.InterfaceIndex -eq $adapter.InterfaceIndex)
{
$objAdapter | Add-Member -NotePropertyName NetConnectionId -NotePropertyValue $adapter.NetConnectionId
}
}
##### NEW CODE END #####
########################
if ($objAdapter.IPEnabled -eq "True")
{
}    
}

并更新您的星型线

******* $Report+= " <td width='75%'>$($objAdapter.Description)</font></td>"

$objAdapter.Description应更换为$objAdapter.NetConnectionId

相关内容

最新更新