网卡信息不断循环

发布于 2024-11-17 07:39:33 字数 1883 浏览 6 评论 0原文

我正在尝试使用双 for 循环提取服务器的 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 
}
}

}

}

请有人帮我解决同样的问题。

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

}

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2024-11-24 07:39:33

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

$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_NetworkAdapterconfiguration 中的每个 NIC 组合在一起。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文