Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.




515 posts

Ultimate Geek
+1 received by user: 32


# 208376 9-Feb-2017 10:53

Hi Guys,
Need help with the Powershell command to output the results in a csv file.
The file gets created but the contents is blank or shows something which isn't the results.

Using Powershell ISE I can see the command is executing and giving the results but its not writing to the file. I'm sure its something simple that I am missing, just can't spot it.



$servers = "geekzone.co.nz","trademe.co.nz"

foreach ( $server in $servers )
{

Write-Host "Starting Ping for $server 'n" -ForegroundColor Yellow
test-Connection -ComputerName $server -Count 3
Write-Host "Completed `n" -ForegroundColor Yellow

}

$DesktopPath = [Environment]::GetFolderPath("Desktop") |
Export-Csv -Path "$DesktopPath\Ping.csv" -Append



Also tried to change from Export to Out-File but no dice (however the export and out-file should will give me the file so I think its something else I am missing.

Out-File -filepath "$DesktopPath\Ping.txt"

Filter this topic showing only the reply marked as answer Create new topic
Infrastructure Geek
4057 posts

Uber Geek
+1 received by user: 195

Trusted
Microsoft NZ
Subscriber

  # 1717590 9-Feb-2017 11:48
Send private message

Looks like you're not piping your output to the file . Either pipe each line to file, or pipe to variable and then pipe variable to file.

See some exampeles here:

https://msdn.microsoft.com/en-us/powershell/reference/4.0/microsoft.powershell.utility/out-file


(sorry about short answer, typing from phone in dark in conference session)




Technical Evangelist
Microsoft NZ
about.me/nzregs
Twitter: @nzregs


2534 posts

Uber Geek
+1 received by user: 943

Subscriber

  # 1717592 9-Feb-2017 11:49
One person supports this post
Send private message

At no point are you passing the output of your test-connection requests to any output - you're only piping the output of 

 

[Environment]::GetFolderPath("Desktop")

 

to the export-csv cmdlet

 

Try this:

 

 

 

$servers = "geekzone.co.nz","trademe.co.nz"

 

$outFile = "$([Environment]::GetFolderPath('Desktop'))\Ping.csv"

 

foreach ( $server in $servers )
{

 

Write-Output "Starting Ping for $server" | Out-File $outFile -Append
test-Connection -ComputerName $server -Count 3 | Out-File $outFile -Append
Write-Output "Completed"| Out-File $outFile -Append

 

}

 

 

 

Note that this won't display the output in the console, only in ping.csv on your desktop

 

 


 
 
 
 




515 posts

Ultimate Geek
+1 received by user: 32


  # 1717598 9-Feb-2017 12:09

Thanks for the prompt reply guys.

@Inphinity that works great.

Do you know how I can format the excel so that each column goes into a separate cell rather then in the first one?

I found this article which says you can use

GPS winword,Excel,Outlook | Export-Csv c:\fso\procoff.csv –NoTypeInformation

However the export-csv bit you have removed and added it in one line. Is there a way to add that in the script?

https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/23/use-powershell-to-work-with-csv-formatted-text/



2534 posts

Uber Geek
+1 received by user: 943

Subscriber

  # 1717645 9-Feb-2017 13:12
One person supports this post
Send private message

You may need to delete the ping.csv file first

 

 

 

$servers = "geekzone.co.nz","trademe.co.nz"

 

$outFile = "$([Environment]::GetFolderPath('Desktop'))\Ping.csv"

 

foreach ( $server in $servers )

 

{

 

test-Connection -ComputerName $server -Count 3 | Export-Csv $outFile -Append
}




515 posts

Ultimate Geek
+1 received by user: 32


  # 1717739 9-Feb-2017 15:14

Thanks for your help :) That works great now.

Filter this topic showing only the reply marked as answer Create new topic



Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Video game market in New Zealand passes half billion dollar mark
Posted 24-May-2019 16:15


WLG-X festival to celebrate creativity and innovation
Posted 22-May-2019 17:53


HPE to acquire supercomputing leader Cray
Posted 20-May-2019 11:07


Techweek starting around NZ today
Posted 20-May-2019 09:52


Porirua City Council first to adopt new council software solution Datascape
Posted 15-May-2019 12:00


New survey provides insight into schools' technology challenges and plans
Posted 15-May-2019 09:30


Apple Music now available on Alexa devices in Australia and New Zealand
Posted 15-May-2019 09:11


Make a stand against cyberbullying this Pink Shirt Day
Posted 14-May-2019 20:23


Samsung first TV manufacturer to launch the Apple TV App and Airplay 2
Posted 14-May-2019 20:11


Vodafone New Zealand sold
Posted 14-May-2019 07:25


Kordia boosts cloud performance with locally-hosted Microsoft Azure ExpressRoute
Posted 8-May-2019 10:25


Microsoft Azure ExpressRoute in New Zealand opens up faster, more secure internet for Kiwi businesses
Posted 8-May-2019 09:39


Vocus Communications to deliver Microsoft Azure Cloud Solutions through Azure ExpressRoute
Posted 8-May-2019 09:25


Independent NZ feature film #statusPending to premiere during WLG-X
Posted 6-May-2019 22:13


The ultimate dog photoshoot with Nokia 9 PureView #ForgottenDogsofInstagram
Posted 6-May-2019 09:41



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.