Solved

Change Powershell Out to CSV file

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

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
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
Comment Utility
Thanks everyone!!!

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

Author Closing Comment

by:compdigit44
Comment Utility
The script worked perfectly. Thanks again for the help!!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now