Solved

Powershell : Pipe results to CSV file

Posted on 2014-02-21
2
2,963 Views
Last Modified: 2014-02-21
Hi,

I feel quite stupid, 'cause for some reason I cannot get the results of a powershellscript piped to a csv file.
I tried to place:
| Select @{N="Name";E={$Name}},@{N="Status";E={$status}}
in combination with:
| Export-Csv D:\PC_ip-info.csv -Delimiter ";" -notype

But it gives me, or: only the results of the last computername of the imported csv, or the: "An empty pipe element is not allowed" error.

I did not place the pipe "|" at the start of an empty line.

This is the script. I would like to have the returned ip values to be exported to a csv file

	$ErrorActionPreference = "SilentlyContinue"
    $CSVFile = "D:\Import_Computers.csv"
	$Computer = import-csv $CSVFile -delimiter ';'	
	 
    Write-Host "Name|    NetworkCard                               | IP          | SUBNET      | GateWay      | MacADD           | DNS "  -ForegroundColor Green  
 
foreach ( $Computer in $Computer ) 
{
	if (Test-Connection $Computer.workstations -Count 1 -ErrorAction SilentlyContinue) 
		{
			$nwINFO = Get-WmiObject -ComputerName $Computer.workstations Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null } 
			$nwServerName = $nwINFO.DNSHostName 
			$nwDescrip = $nwINFO.Description 
			$nwIPADDR = $nwINFO.IPAddress 
			$nwSUBNET = $nwINFO.IpSubnet 
			$nwGateWay = $nwINFO.DefaultIPGateway 
			$nwMacADD = $nwINFO.MACAddress 
			$nwDNS = $nwINFO.DNSServerSearchOrder 			
			#        Server/CompName   |NetworkCard | IPAdress  |  SubnetMask|  Gateway    | MAC Address|   DNS | 
			Write-Host "$nwServerName | $nwDescrip | $nwIPADDR | $nwSUBNET | $nwGateWay | $nwMacADD | $nwDNS " | ft *
		}
	else 
		{ 
			write-host $Computer.workstations "is not pingable" -ForegroundColor Yellow
		} 
}

Open in new window

0
Comment
Question by:Loyall
2 Comments
 
LVL 40

Assisted Solution

by:footech
footech earned 250 total points
ID: 39877232
If you want to pipe to Export-CSV, it is often better to use the ForEach-Object cmdlet instead of the foreach statement, since the statement doesn't work with the pipeline.  If you do use the foreach statement, one way you can get results to a .CSV is to surround the entire foreach statement and its scripblock with array notation @() and then pipe that to Export-CSV.

Side note here, although it appears to work, I would say it is very bad practice to use the same variable twice as in line 7 as it just leads to confusion.  Better would be something like
foreach ($computer in $computers)
Also note that you can't use Write-Host to send output to the pipeline.

What info are you trying to output to .CSV?  I get "Name", but what is "Status"?  I don't see that anywhere in your script.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39877407
The line
      Write-Host "$nwServerName | $nwDescrip | $nwIPADDR | $nwSUBNET | $nwGateWay | $nwMacADD | $nwDNS " | ft *

Open in new window

doesn't make sense - as footech stated, Write-Host doesn't produce pipeline output, so calling the format-table cmdlet is useless.
The script does not work if there is more than one IP address assigned, or more than one NIC active (including virtual NICs like those from (R)RAS).
That's how I would do that:
$ErrorActionPreference = "SilentlyContinue"
$CSVFile = "D:\Import_Computers.csv"

import-csv $CSVFile -delimiter ';'  |
  ? {Test-Connection $_.workstations -Count 1} |
  % { Get-WmiObject -ComputerName $_.workstations Win32_NetworkAdapterConfiguration } |
  ? { $_.IPAddress -ne $null } |
  select @{n='Name'       ; e={$_.DNSHostName}},
         @{n='NetworkCard'; e={$_.Description}},
         @{n='IP'         ; e={$_.IPAddress}},
         @{n='Subnet'     ; e={$_.IPSubnet}},
         @{n='Gateway'    ; e={$_.DefaultIPGateway}},
         @{n='MACAdd'     ; e={$_.MACAddresss}},
         @{n='DNS'        ; e={$_.DNSServerSearchOrder}} |
  Export-Csv D:\PC_ip-info.csv -Delimiter ";" -NoType

Open in new window

I've removed the message if a machine could not be reached - that would be difficult here.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question