Windows 网卡信息不断循环

Windows 网卡信息不断循环,windows,arrays,powershell,nic,Windows,Arrays,Powershell,Nic,我正在尝试使用double for loop为服务器提取NIc卡信息,由于NIc卡信息不断循环,我目前面临一个问题,例如,如果有5个NIc卡循环运行并给出5次相同的输出,有没有办法中断输出?之后,它给出了5个网卡信息,并使其脱离for循环 $colItems1 = get-wmiobject -class "Win32_NetworkAdapter" -namespace "root\CIMV2" -computername localhost $colItems = get-wmiobjec

我正在尝试使用double for loop为服务器提取NIc卡信息,由于NIc卡信息不断循环,我目前面临一个问题,例如,如果有5个NIc卡循环运行并给出5次相同的输出,有没有办法中断输出?之后,它给出了5个网卡信息,并使其脱离for循环

$colItems1 = get-wmiobject -class "Win32_NetworkAdapter"  -namespace "root\CIMV2" -computername localhost
$colItems = get-wmiobject -class "Win32_NetworkAdapterconfiguration"  -namespace "root\CIMV2" -computername localhost

foreach ($objitem in $colItems)
{
foreach ($objItem1 in $colItems1) {
# A test is needed here as the loop will find a number of virtual network configurations with no  "Hostname" 
# So if the "Hostname" does not exist, do NOT display it!
if ($objItem.ipenabled -eq "true" ) {
if ($objitem1.netconnectionid){
# Write to screen
#write-host "Caption: " $objItem.Caption
write-host "NIC Card Name                 :" $objitem1.netconnectionid -ForegroundColor Green
Write-Host "DHCP Enabled                  :" $objItem.DHCPEnabled -ForegroundColor green
Write-Host "IP Address                    :" $objItem.IPAddress -ForegroundColor green
Write-Host "Subnet Mask                   :" $objItem.IPSubnet -ForegroundColor green
Write-Host "Gateway                       :" $objItem.DefaultIPGateway -ForegroundColor green
#Write-Host "MAC Address                   :"$ojbItem.MACAddress -ForegroundColor green
#write-host "Default IP Gateway: " $objItem.DefaultIPGateway
#write-host "Description: " $objItem.Description
write-host "DHCP Server                   :" $objItem.DHCPServer -ForegroundColor green
write-host "DNS Domain                    :" $objItem.DNSDomain -ForegroundColor green
write-host "DNS Domain Suffix Search Order:" $objItem.DNSDomainSuffixSearchOrder -ForegroundColor green
write-host "DNS Server Search Order       :" $objItem.DNSServerSearchOrder -ForegroundColor green
write-host
#write-host "Index: " $objItem.Index
# Create HTML Output 
}
}

}

}

请找人帮我解决这个问题。

嵌套循环导致重复输出。尝试:

$colItems1 = get-wmiobject -class "Win32_NetworkAdapter"  -namespace "root\CIMV2" -computername localhost
$colItems = get-wmiobject -class "Win32_NetworkAdapterconfiguration"  -namespace "root\CIMV2" -computername localhost

foreach ($objitem in $colItems)
{
    # Match the current $objItem with the correct $ColItems1 element.
    $objItem1 = $colItems1|?{$_.Caption -eq $objItem.Caption}
    # A test is needed here as the loop will find a number of virtual network configurations with no  "Hostname" 
    # So if the "Hostname" does not exist, do NOT display it!
    if ($objItem.ipenabled -eq "true" -and  $objitem1.netconnectionid) {
        # Write to screen
        #write-host "Caption: " $objItem.Caption
        write-host "NIC Card Name                 :" $objitem1.netconnectionid -ForegroundColor Green
        Write-Host "DHCP Enabled                  :" $objItem.DHCPEnabled -ForegroundColor green
        Write-Host "IP Address                    :" $objItem.IPAddress -ForegroundColor green
        Write-Host "Subnet Mask                   :" $objItem.IPSubnet -ForegroundColor green
        Write-Host "Gateway                       :" $objItem.DefaultIPGateway -ForegroundColor green

        write-host "DHCP Server                   :" $objItem.DHCPServer -ForegroundColor green
        write-host "DNS Domain                    :" $objItem.DNSDomain -ForegroundColor green
        write-host "DNS Domain Suffix Search Order:" $objItem.DNSDomainSuffixSearchOrder -ForegroundColor green
        write-host "DNS Server Search Order       :" $objItem.DNSServerSearchOrder -ForegroundColor green
        write-host
    }
}

$objItem1=$colItems1 |?{$\.Caption-eq$objItem.Caption}
将匹配两个集合中的NIC;在您的示例中,您将Win32_NetworkAdapter中的每个NIC与Win32_NetworkAdapter配置中的每个NIC组合在一起

嵌套循环导致重复输出。尝试:

$colItems1 = get-wmiobject -class "Win32_NetworkAdapter"  -namespace "root\CIMV2" -computername localhost
$colItems = get-wmiobject -class "Win32_NetworkAdapterconfiguration"  -namespace "root\CIMV2" -computername localhost

foreach ($objitem in $colItems)
{
    # Match the current $objItem with the correct $ColItems1 element.
    $objItem1 = $colItems1|?{$_.Caption -eq $objItem.Caption}
    # A test is needed here as the loop will find a number of virtual network configurations with no  "Hostname" 
    # So if the "Hostname" does not exist, do NOT display it!
    if ($objItem.ipenabled -eq "true" -and  $objitem1.netconnectionid) {
        # Write to screen
        #write-host "Caption: " $objItem.Caption
        write-host "NIC Card Name                 :" $objitem1.netconnectionid -ForegroundColor Green
        Write-Host "DHCP Enabled                  :" $objItem.DHCPEnabled -ForegroundColor green
        Write-Host "IP Address                    :" $objItem.IPAddress -ForegroundColor green
        Write-Host "Subnet Mask                   :" $objItem.IPSubnet -ForegroundColor green
        Write-Host "Gateway                       :" $objItem.DefaultIPGateway -ForegroundColor green

        write-host "DHCP Server                   :" $objItem.DHCPServer -ForegroundColor green
        write-host "DNS Domain                    :" $objItem.DNSDomain -ForegroundColor green
        write-host "DNS Domain Suffix Search Order:" $objItem.DNSDomainSuffixSearchOrder -ForegroundColor green
        write-host "DNS Server Search Order       :" $objItem.DNSServerSearchOrder -ForegroundColor green
        write-host
    }
}
$objItem1=$colItems1 |?{$\.Caption-eq$objItem.Caption}
将匹配两个集合中的NIC;在您的示例中,您将Win32_NetworkAdapter中的每个NIC与Win32_NetworkAdapter配置中的每个NIC组合在一起

与另一个答案非常相似,但你从未接受。与另一个答案非常相似,但你从未接受。