Solved

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

Posted on 2011-03-11
836 Views
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
0
Question by:Kris Montgomery
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2
• 2

LVL 12

Accepted Solution

FDiskWizard earned 250 total points
ID: 35111542
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

LVL 5

Assisted Solution

daveTechSearch earned 250 total points
ID: 35111602
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

0

LVL 12

Expert Comment

ID: 35111699
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

LVL 6

Author Comment

ID: 35111736
Yep.  Both are great.

Exactly what I needed.

Thanks.
mug
0

LVL 6

Author Closing Comment

ID: 35111834
These are both perfect answers.  The first one is just a bit easier to follow for a newbie.
0

LVL 6

Author Comment

ID: 35112584
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

LVL 5

Expert Comment

ID: 35112937
get-childitem | sort -prop LastWriteTime | select -last 1
0

LVL 6

Author Comment

ID: 35112940
Thank you, thank you.

mug
0

## Featured Post

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll