Solved

Change Powershell Out to CSV file

Posted on 2015-02-16
6
88 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

718 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