Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell : Pipe results to CSV file

Posted on 2014-02-21
2
Medium Priority
?
3,114 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 41

Assisted Solution

by:footech
footech earned 1000 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 71

Accepted Solution

by:
Qlemo earned 1000 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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

824 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