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
Solved

Change Powershell Out to CSV file

Posted on 2015-02-16
6
86 Views
Last Modified: 2015-02-17
I found the Powershell script online to ping a list of IP's and report it's status. This work perfect but I would like to change it from placing the results on the console to writing them to a CSV..

I have tried editing the script to remove write-host and include export-csv but has worked as I was expecting.

$names = Get-Content "c:\devices.txt"
foreach ($name in $names) {
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) {
write-host "$name is up" 
}
else {
write-host "$name is down" 
}
}

Open in new window

0
Comment
Question by:compdigit44
6 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 40612087
Try this:
Get-Content -Path "C:\devices.txt" |
	Select-Object -Property `
		@{Name="Host"; Expression={"Processing $_ ..." | Write-Host; $_}},
		@{Name="Ping"; Expression={Test-Connection -ComputerName $_ -Count 1 -Quiet}} |
	Export-Csv -NoTypeInformation -Path "C:\Temp\test.csv"

Open in new window

0
 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40612152
Or This
$names = Get-Content "c:\temp\devices.txt"
$Results = @()
foreach ($name in $names) {
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) {
$Answer = "$name is up" 
}
else {
$Answer = "$name is down" 
}
$Results += $Answer 
}
$Results | Export-CSV c:\temp\Results.csv -NoTypeInformation

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40612166
Nope, that will not work. String arrays cannot be exported that way, you only get the length of each string exported.

A CSV file contains more than one field, for use in a row/column manner. oBdA's usage is correct.

For the message-only version you "export" to a simple text file. But: In PowerShell you refrain from collecting results in vars just to write them out en block later. That can consume a lot of memory unnecessarily.
Get-Content "c:\temp\devices.txt" | %  {
  if (Test-Connection -ComputerName $_ -Count 1 -Quiet)  {
    "$_ is up" 
  } else {
     "$_ is down" 
  }
} | Out-File c:\temp\Results.txt

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40612179
Thanks Qlemo!

You are right... mine didn't work. Ignore Mine compdigit! I really did only get the string length...
Thanks for the lesson!
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40613128
Thanks everyone!!!

 I will try this tomorrow and let you know how I make out...
0
 
LVL 19

Author Closing Comment

by:compdigit44
ID: 40614995
The script worked perfectly. Thanks again for the help!!!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A procedure for exporting installed hotfix details of remote computers using powershell
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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