Solved

Powershell : Pipe results to CSV file

Posted on 2014-02-21
2
2,979 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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