Solved

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

Posted on 2011-03-11
8
832 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 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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

by:Kris Montgomery
Comment Utility
Yep.  Both are great.  

Exactly what I needed.

Thanks.
mug
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Author Closing Comment

by:Kris Montgomery
Comment Utility
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
Comment Utility
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
Comment Utility
get-childitem | sort -prop LastWriteTime | select -last 1
0
 
LVL 6

Author Comment

by:Kris Montgomery
Comment Utility
Thank you, thank you.

mug
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now