?
Solved

Change Powershell Out to CSV file

Posted on 2015-02-16
6
Medium Priority
?
90 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
[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
6 Comments
 
LVL 85

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 70

Accepted Solution

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

 
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 20

Author Comment

by:compdigit44
ID: 40613128
Thanks everyone!!!

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

Author Closing Comment

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

Featured Post

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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