Link to home
Start Free TrialLog in
Avatar of mjm21
mjm21Flag for United States of America

asked on

Powershell Script that would list network adapters ip address and status

Powershell Script that would list all network adapters ip address and status gathered from a list of computers in a .txt file.  The output should be exported to a .csv file.
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Use the following script below...
$List = get-content "c:\name.txt"
foreach ($Computer in $List) {
Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "10.10.*" | ft $Computer, DHCPEnabled, DefaultIPGateway, Description
}

Open in new window


 The script above will take each computer and from your list and only reply the values of the network adapter that actually has IP address associated to it. This is helpful because if this is not present it will present all adapters like IPv6 Tunneling etc.

You will also need to change the 10.10.* to whatever your internal IP scheme is.

Will.
Avatar of mjm21

ASKER

Thanks to both of you!  

Will:  Can we pipe this out to .csv file?  Subnet should be like this:  IE: 10.10.10.0 correct?
Powershell export to csv
http://blogs.technet.com/b/heyscriptingguy/archive/2014/02/04/use-powershell-to-create-csv-file-to-open-in-excel.aspx

for command line (netsh works from both powershell and command line) just add > c:\outputfile.csv
Avatar of mjm21

ASKER

ok thanks Carol
If you want to pipe it to CSV use my code below...

$List = get-content "c:\name.txt"
foreach ($Computer in $List) {
Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "10.10.*" | select $Computer, DHCPEnabled, IPAddress, DefaultIPGateway, Description | out-file "c:\exportNetworkAdapters.csv" -append
}

Open in new window


Will.
Avatar of mjm21

ASKER

Hi Will,

Getting  this  error:

Missing closing '}' in statement block.
At line:4 char:2
My Mistake. I have corrected the code below...

$List = get-content "c:\name.txt"
foreach ($Computer in $List) {
Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "10.10.*"} | select $Computer, DHCPEnabled, IPAddress, DefaultIPGateway, Description | out-file "c:\exportNetworkAdapters.csv" -append
}

Open in new window


I forgot to add the } bracket after "10.10.*"}
Avatar of mjm21

ASKER

ok - testing now.  Also, as far as the ip address schema, not all the  servers are on  the same subnet.
Avatar of mjm21

ASKER

Will...another  error:

Select-Object : Cannot convert System.Management.Automation.PSObject to one of
the following types {System.String, System.Management.Automation.ScriptBlock}.
At line:3 char:88
+ Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Compute
r | select <<<<  $Computer, DHCPEnabled, IPAddress, DefaultIPGateway, Descripti
on | out-file "c:\exportNetworkAdapters.csv" -append
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupport
   edException
    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co
   mmands.SelectObjectCommand
Ok try using FT instead of select. See below...

$List = get-content "c:\name.txt"
foreach ($Computer in $List) {
Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "10.10.*"} | FT $Computer, DHCPEnabled, IPAddress, DefaultIPGateway, Description | out-file "c:\exportNetworkAdapters.csv" -append
}                                          

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of smahi
smahi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial