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.


Oriphix

523 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
Regs
4066 posts

Uber Geek
+1 received by user: 206

Trusted
Snowflake

  #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)






Inphinity
2780 posts

Uber Geek
+1 received by user: 1184


  #1717592 9-Feb-2017 11:49
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

 

 


Oriphix

523 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/





Inphinity
2780 posts

Uber Geek
+1 received by user: 1184


  #1717645 9-Feb-2017 13:12
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
}


Oriphix

523 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








Geekzone Live »

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



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.