$NetConf = Get-NetIPConfiguration
$Adapters = Get-NetAdapter -Physical
$NetAdapterStatus = $Adapters | ForEach-Object {
$Adapter = $PSItem
$AdapterProfiles = $NetConf | where {$Adapter.ifIndex -eq $_.InterfaceIndex}
$AdapterProfiles | Select-Object
@{n='Name';e={$Adapter.Name}},
@{n='Status';e={$Adapter.Status}},
@{n='MAC';e={$Adapter.LinkLayerAddress}},
@{n='IP';e={$Adapter.IPAddress}}
}
Write-Host $NetAdapterStatus
我有结果:名称、状态、MAC 以及类似 IPv4Address=Microsoft.Management.Infrastructure.CimInstance[] 为什么?
答案1
您可以通过类似这样的方式获取信息。
Clear-Host
Get-NetAdapter |
ForEach-Object {
$PSitem |
Select-Object -Property Name, InterfaceDescription, ifIndex, Status,
MacAddress, LinkSpeed,
@{
Name = 'IPAddress'
Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address}
}
}
# Results
<#
Name : Ethernet
InterfaceDescription : Microsoft Hyper-V Network Adapter
ifIndex : 205
Status : Up
MacAddress : 00-15-5D-4A-C1-BC
LinkSpeed : 10 Gbps
IPAddress : {$null, 172.21.159.249}
#>
Clear-Host
Get-NetAdapter |
ForEach-Object {
$PSitem |
Select-Object -Property Name, InterfaceDescription, ifIndex, Status,
MacAddress, LinkSpeed,
@{
Name = 'IPAddress'
Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address}
}
} |
Format-Table -AutoSize
# Results
<#
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed IPAddress
---- -------------------- ------- ------ ---------- --------- ---------
Ethernet Microsoft Hyper-V Network Adapter 205 Up 00-15-5D-4A-C1-BC 10 Gbps {$null, 172.21.159.249}
#>
或者通过您选择以这种方式使用的 cmdlt:
Get-NetIPConfiguration |
ForEach {
$NetIP = $PSItem
Get-NetAdapter -Physical |
Select-Object -Property Name, InterfaceDescription, Status, MacAddress, LinkSpeed, LinkLayerAddress,
@{
Name = 'IPv4Address'
Expression = {$NetIp.IPv4Address}
}
}
# Results
<#
Name : Ethernet
InterfaceDescription : Microsoft Hyper-V Network Adapter
Status : Up
MacAddress : 00-15-5D-4A-CC-B2
LinkSpeed : 10 Gbps
LinkLayerAddress : 00-15-5D-4A-CC-B2
IPv4Address : 172.21.151.33
#>
Get-NetIPConfiguration |
ForEach {
$NetIP = $PSItem
Get-NetAdapter -Physical |
Select-Object -Property Name, InterfaceDescription, Status, MacAddress, LinkSpeed, LinkLayerAddress,
@{
Name = 'IPv4Address'
Expression = {$NetIp.IPv4Address}
}
} |
Format-Table -AutoSize
# Results
<#
Name InterfaceDescription Status MacAddress LinkSpeed LinkLayerAddress IPv4Address
---- -------------------- ------ ---------- --------- ---------------- -----------
Ethernet Microsoft Hyper-V Network Adapter Up 00-15-5D-4A-CC-B2 10 Gbps 00-15-5D-4A-CC-B2 172.21.151.33
#>