/build/static/layout/Breadcrumb_cap_w.png

Powershell - read list of machines from txt file and output eventvwr to .csv

Hi,
 
I am writing a Powershell cmd to:
 
1) read list of machines stored in a txt file
2) export each machine's 'applciation', 'error' from event viewer
3) save as <computername>.csv
4) read next machine name and so on... 
 
This is the cmd I use to output the remote event viewer for 1 machine (this works):
 
 
Get-EventLog application -computername MACHINE1| where{$_.entrytype -eq"Error"} | Format-Table -auto -wrap | out-file -filepath c:\eventvwr1.txt
 
I am having trouble getting it to work with multiple machines.
 
I have created a txt file c:\machines_List.txt which contains machine names.
 
This is what I have tried:
 
Get-Content c:\machines_List.txt | Foreach-Object {Get-EventLog application -computername ????| where{$_.entrytype -eq"Error"} | Format-Table -auto -wrap | out-file -filepath c:\?????.csv}
 
This doesn't work. I'm not sure what to put after -computername and ???.csv. Not even sure it would work anyhow.
 
Also, in my txt file which contains the machine names, how should I list them? A machine name per line or seperated with comma etc?
 
Any advice would be very much appreciated.
 
Thanks!

0 Comments   [ + ] Show comments

Answers (2)

Posted by: dugullett 12 years ago
Red Belt
4

Give this a shot. This will output the file to c:\temp\computername.csv. I would assume that you would want these somewhere on a share somewhere? If that is the case change "$csvfile = "c:\temp\$computer.csv" to $csvfile = "\\share_name\temp\$computer.csv"

$computer= gc env:computername

$file= "c:\temp\computer.txt"

$csvfile = "c:\temp\$computer.csv"

Get-Content $file | Foreach-Object {Get-EventLog application| where{$_.entrytype -eq"Error"} | Format-Table -auto -wrap | out-file -filepath $csvfile}

Comments:
  • or something like this.

    $name= gc env:computername
    $computers= get-content c:\temp\computer.txt
    $csvfile = "c:\temp\$name.csv"
    foreach ($computer in $computers) {Get-EventLog -computer $computers -logname application |
    where{$_.entrytype -eq"Error"} | Format-Table -auto -wrap | out-file -filepath $csvfile}

    Also you don't want any separators. You can just list them.

    computer_1
    computer_2
    computer_3 - dugullett 12 years ago
Posted by: Meic 12 years ago
Second Degree Blue Belt
2

Thanks very much for taking time to reply. Very much appreciated.

New to Powershell so trying to find the easiest way to get it to run on a system ( save as PS1, signing it etc). One of the machines I will be running it from has Powershell 1 and the other has ver 2.

Never realised it wasn't as simple as dbl clicking a .ps1 file to run it.

Will let you know how I get on.

Cheers!


Comments:
  • Two things that helped me are here. I also suggest snapshotting a VM to run these on. I've broken a few things before.

    http://www.itninja.com/link/learn-powershell
    http://powershell.com/cs/blogs/ebookv2/default.aspx

    Also I'm not sure of your deployment method. Some of my scripts I've created exe's for. Take a look at this link for more details.
    http://www.itninja.com/question/k1000-how-do-you-push-scripts-invisibly-to-your-workstations - dugullett 12 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ