Solved

Powershell : Pipe results to CSV file

Posted on 2014-02-21
2
2,950 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
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 …

856 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