我试图使用PowerShell创建NIc卡信息报告,&使用add member来附加属性,但是如果服务器有超过1个NIC,添加成员会返回错误“你想要owerwrite属性”使用-force,任何人都可以帮我解决问题,也就是我有一个数组$ output = @(),我附加所有这些输入,我尝试添加一个for循环来获得NICS和通过它们逐个迭代作为一个数组,我看不到输出,有人可以帮助我。
< #NIC卡信息检查#>
$colItems1 = get-wmiobject -class "Win32_NetworkAdapter" -namespace "root\CIMV2" -computername $compname
$colItems = get-wmiobject -class "Win32_NetworkAdapterconfiguration" -namespace "root\CIMV2" -computername $compname
$lengthcol=($colItems1).length
for ($i=0;$i-lt$length;$i++){
foreach ($objitem in $colItems)
{
# Match the current $objItem with the correct $ColItems1 element.
$objItem1 = $colItems1| where-object{$_.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
$nicname[$i]=$objitem1.netconnectionid
$dhcpenabled[$i]=$objItem.DHCPEnabled
$ipaddress[$i]=$objItem.IPAddress
$ipsubnet[$i]=$objItem.IPSubnet
$gateway[$i]=$objItem.DefaultIPGateway
$dhcpserver[$i]=$objItem.DHCPServer
$DNSDomain[$i]=$objItem.DNSDomain
$DNSDomainSearchOrder[$i]=$objItem.DNSDomainSuffixSearchOrder
$DNSServerSearchOrder[$i]=$objItem.DNSServerSearchOrder
$output | Add-Member -MemberType Noteproperty -Name NICCardName[$i] -Value $nicname[$i]
$output | Add-Member -MemberType Noteproperty -Name DHCPEnabled[$i] -Value $dhcpenabled[$i]
$output | Add-Member -MemberType Noteproperty -Name IPAddress[$i] -Value $ipaddress[$i]
$output | Add-Member -MemberType Noteproperty -Name SubnetMask[$i] -Value $ipsubnet[$i]
$output | Add-Member -MemberType Noteproperty -Name Gateway[$i] -Value $gateway[$i]
$output | Add-Member -MemberType Noteproperty -Name DHCPServer[$i] -Value $dhcpserver[$i]
$output | Add-Member -MemberType Noteproperty -Name DNSDomain[$i] -Value $DNSDomain[$i]
$output | Add-Member -MemberType Noteproperty -Name DNSDomainSuffixSearchOrder[$i] -Value $DNSDomainSearchOrder[$i]
$output | Add-Member -MemberType Noteproperty -Name DNSServerSearchOrder[$i] -Value $DNSServerSearchOrder[$i]
}
}
}
$输出+ = $输出
答案 0 :(得分:1)
试试这个:
Get-WmiObject Win32_NetworkAdapter -ComputerName $computer| ForEach-Object {
$NetConnectionId = $_.NetConnectionId
$nac = $_.GetRelated('Win32_NetworkAdapterconfiguration') | Select-Object IPEnabled,DHCPEnabled,IPAddress,IPSubnet,DefaultIPGateway,DHCPServer,DNSDomain,DNSDomainSuffixSearchOrder,DNSServerSearchOrder
$nac | Select-Object *,@{Name='NetConnectionId';Expression={$NetConnectionId}}
}
更新(每张海报要求):
$compname='.'
$colItems1 = get-wmiobject -class Win32_NetworkAdapter -computername $compname
$colItems = get-wmiobject -class Win32_NetworkAdapterconfiguration -computername $compname
foreach ($objitem in $colItems)
{
$objItem1 = $colItems1 | where-object{$_.Caption -eq $objItem.Caption}
if ($objItem.ipenabled -eq $true -and $objitem1.netconnectionid)
{
$output = new-object -typename psobject
$output | Add-Member -MemberType Noteproperty -Name NICCardName -Value $objitem1.netconnectionid
$output | Add-Member -MemberType Noteproperty -Name DHCPEnabled -Value $objItem.DHCPEnabled
$output | Add-Member -MemberType Noteproperty -Name IPAddress -Value $objItem.IPAddress
$output | Add-Member -MemberType Noteproperty -Name SubnetMask -Value $objItem.IPSubnet
$output | Add-Member -MemberType Noteproperty -Name Gateway -Value $objItem.DefaultIPGateway
$output | Add-Member -MemberType Noteproperty -Name DHCPServer -Value $objItem.DHCPServer
$output | Add-Member -MemberType Noteproperty -Name DNSDomain -Value $objItem.DNSDomain
$output | Add-Member -MemberType Noteproperty -Name DNSDomainSuffixSearchOrder -Value $objItem.DNSDomainSuffixSearchOrder
$output | Add-Member -MemberType Noteproperty -Name DNSServerSearchOrder -Value $objItem.DNSServerSearchOrder -PassThru
}
}