开发者

NIC Card Info Keeps Looping

Im trying to extract NIc card info for a server using double for loop, im facing a problem currently as the NIc card info keeps on looping, like if there are 5 nic cards the loop runs & gives the same out put 5 times, is there any way to break the output? aftre it gives 5 nic card info & make it come out of for loop.

$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 -Foreground开发者_StackOverflowColor 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 
}
}

}

}

Request some one to please help me out with the same.


The nested loop is causing the duplicate output. Try:

$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
    }
}

The line $objItem1 = $colItems1|?{$_.Caption -eq $objItem.Caption} will match the NICs in the two collections; in your example you were combining every NIC from Win32_NetworkAdapter with every NIC from Win32_NetworkAdapterconfiguration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜