Solved

Change Powershell Out to CSV file

Posted on 2015-02-16
6
85 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 83

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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