[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 76
  • Last Modified:

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.
0
mjm21
Asked:
mjm21
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
 
mjm21Author Commented:
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Carol ChisholmCommented:
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
0
 
mjm21Author Commented:
ok thanks Carol
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
 
mjm21Author Commented:
Hi Will,

Getting  this  error:

Missing closing '}' in statement block.
At line:4 char:2
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
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.*"}
0
 
mjm21Author Commented:
ok - testing now.  Also, as far as the ip address schema, not all the  servers are on  the same subnet.
0
 
mjm21Author Commented:
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
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
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

0
 
smahiCommented:
Check this out simple and fast one

$servers = get-content List_of_Servers.txt
$serversAndIps = "List_of_servers_with_ips.csv"

$results =@()
  foreach ($server in $servers )
   {
	$result=@() 
	$result = "" | Select ServerName , IPaddress
	$result.IPaddress = (test-connection -computername $server -count 1 | select -expand "IPV4Address")
	$result.servername = $server
	$results += $result
   }
   
$results | export-csv -NoTypeInformation $serversandips

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now