Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-11
8
Medium Priority
?
839 Views
Last Modified: 2012-08-13
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
Comment
Question by:Kris Montgomery
  • 4
  • 2
  • 2
8 Comments
 
LVL 12

Accepted Solution

by:
FDiskWizard earned 1000 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

by:daveTechSearch
daveTechSearch earned 1000 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

Open in new window

0
 
LVL 12

Expert Comment

by:FDiskWizard
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
WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

 
LVL 6

Author Comment

by:Kris Montgomery
ID: 35111736
Yep.  Both are great.  

Exactly what I needed.

Thanks.
mug
0
 
LVL 6

Author Closing Comment

by:Kris Montgomery
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

by:Kris Montgomery
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

by:daveTechSearch
ID: 35112937
get-childitem | sort -prop LastWriteTime | select -last 1
0
 
LVL 6

Author Comment

by:Kris Montgomery
ID: 35112940
Thank you, thank you.

mug
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Loops Section Overview
Screencast - Getting to Know the Pipeline

877 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