PowerShell
PowerShell

Reputation: 2081

Extracting Nic card info using powershell gwmi object!

Im trying to extract NIc card details for a server, here is the code below, im unable to get the information for $objitem.netconnectionid displayed in the output it always comes blank although rest of out does come.

Write-Host "Network Information" -ForegroundColor Yellow
Write-Host "___________________" -ForegroundColor Yellow
Write-Host

$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration"  -namespace "root\CIMV2" -computername $compname 


foreach ($objItem in $colItems) {
    # 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.DNSHostName -ne $NULL) {
        # Write to screen
        #write-host "Caption: " $objItem.Caption
        write-host "NIC Card Name                 :" $objitem.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 
        }
}

can anyone of powershell gurus help me out !

thanks, vinith

Upvotes: 1

Views: 3638

Answers (2)

JPBlanc
JPBlanc

Reputation: 72680

NetConnectionID is part of Win32_NetworkAdapter WMI class

Here is your code doing what you want

Write-Host "Network Information" -ForegroundColor Yellow
Write-Host "___________________" -ForegroundColor Yellow
Write-Host

$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration"  -namespace "root\CIMV2" -computername $compname 


foreach ($objItem in $colItems) {
    # 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.DNSHostName -ne $NULL) {
        # Write to screen
        #write-host "Caption: " $objItem.Caption
        #write-host "NIC Card Name                 :" $objitem.netconnectionid -ForegroundColor Green
        $netAdp = get-wmiobject -class "Win32_NetworkAdapter"  -Filter "GUID=`'$($objItem.SettingID)`'" -namespace "root\CIMV2" -computername $compname 
        write-host "NIC Card Name                 :" $netAdp.NetConnectionID -ForegroundColor Green
        write-host "NIC Card Description          :" $netAdp.Description -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 
        }
}

Upvotes: 2

vonPryz
vonPryz

Reputation: 24081

There is no such a member as netconnectionid in the Win32_NetworkAdapterConfiguration WMI class. What kind of information you are looking from such a member? It is likely to be available with another a name or member's member object property.

Upvotes: 1

Related Questions