Solved

Powershell : Pipe results to CSV file

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

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 68

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now