Powershell: How to pull txt logs off remote server and parse text?

I would like to retrieve text file logs off network servers (all in the same application path on remote servers), parse the text for the last 20 lines of the file and put that all in to one local csv file.

I need a starting point.

Thanks.
mug
LVL 6
Kris MontgomeryAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
FDiskWizardConnect With a Mentor Commented:
I think you want: GET-CONTENT, and Export-CSV at a minimum
http://technet.microsoft.com/en-us/library/ee176843.aspx

I think this will do it... Just add: "| Export-csv MyFile.csv" on the end.

[PS] C:\>  foreach ($server in ("Server01","Sevrer02")) {dir \\$server\c$\Logs\*.log | foreach-object {$_ | get-content | select-object -last 20}}
0
 
daveTechSearchConnect With a Mentor Commented:
one similar one i've used in the past.
$comps = get-content c:\temp\servers.txt
$filepath = '\c$\path\to\file.txt'

@(foreach($comp in $comps){
(get-content \\$comp$filepath)[-20 .. -1]
}) | export-csv -notype c:\temp\output.csv

Open in new window

0
 
FDiskWizardCommented:
Looks like Dave came up with the same answer basically, just a little more elegant :)
Mine is meant as a single line straight from the command line, and his is meant to be saved in a .PS1 file and executed.

Enjoy. I just started playing with Powershell in December.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Kris MontgomeryAuthor Commented:
Yep.  Both are great.  

Exactly what I needed.

Thanks.
mug
0
 
Kris MontgomeryAuthor Commented:
These are both perfect answers.  The first one is just a bit easier to follow for a newbie.
0
 
Kris MontgomeryAuthor Commented:
Can one of you guys add the code on how to get only the most recent log file from that folder/location?

I appreciate it.

Thanks.
mug
0
 
daveTechSearchCommented:
get-childitem | sort -prop LastWriteTime | select -last 1
0
 
Kris MontgomeryAuthor Commented:
Thank you, thank you.

mug
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.