1

This thread got me started very well, but now I need more help

I am trying to loop through my serverlist.txt file, and pass the results of Get-EventLog to Out-GridView and then on to a .csv file. I have this working, but I have to select all the records in the GridView window then click OK for each server.

So, I have the idea that I want to create a $sys variable outside the loop, go in, append the results to that variable for each server, and then exit the loop and pass $sys over to Grid-view.

My confusion comes regardinf variable declaration, type, appending and placement in the code...

I'm just learning PS now, so this may be a little basic for you :)

this code works...need to add in the variable idea in the right places:

#Drop the existing files
Remove-Item C:\system.csv

# SERVER LIST PROPERTIES
# Get computer list to check disk space. This is just a plain text file with the servers listed out.
$computers = Get-Content "C:\ServerList.txt"; 

#Declare $sys here ??

# QUERY COMPUTER SYSTEM EVENT LOG
foreach($computer in $computers)

{        
 if(Test-Connection $computer -Quiet -Count 1)
        {
   Try {
        # $sys = 
        Get-EventLog -ComputerName $computer -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-7) `
        | Select-Object -Property machineName, EntryType, EventID, Source, TimeGenerated, Message `
        | Out-GridView -PassThru | Export-Csv C:\System.csv -NoTypeInformation -Append;
       } 
   Catch 
       {
       Write-Verbose "Error $($error[0]) encountered when attempting to get events from  $computer"
       }
       } 
   else {
         Write-Verbose "Failed to connect to $computer"
        }


}
# $sys | Out-GridView....etc.

Thanks!

Kevin3NF

Community
  • 1
  • 1
Kevin3NF
  • 113
  • 1
  • 8
  • 1
    add `$sys = @()` outside the loop (just like you wanted to) – 4c74356b41 Dec 07 '16 at 14:17
  • 1
    and use `$sys += Get-EventLog` to add data to it – henrycarteruk Dec 07 '16 at 14:22
  • Is the grid view required, or do you just want to put things in a csv file? – Jim Moyle Dec 07 '16 at 14:31
  • @JimMoyle, GridView is the option that gives me the cleanest output of the full message from the Event log...Format-Table chops it off. Sadly, I need this yesterday. I have implemented the suggestions above...running a test now – Kevin3NF Dec 07 '16 at 14:33
  • 1
    Just don't use any formatting cmdlet's at all and output the data to the CSV file. – Mike Garuccio Dec 07 '16 at 14:59
  • I got what I needed from the first two comments...will try out Mike's later today for a cleaner approach. thanks – Kevin3NF Dec 07 '16 at 15:42
  • Yeah, Mike has the correct solution for you to just produce the CSV file. – Jim Moyle Dec 07 '16 at 16:00
  • 1
    @MikeGaruccio , your answer gave me the ultimate results I was looking for. This question was about syntax and placement of the variables...how do I close this out properly? You all helped :) – Kevin3NF Dec 07 '16 at 16:02

1 Answers1

0

Just to close this out, I used suggestions from mutiple comments:

$sys = @() (outside the loop)

$sys += Get-EventLog (inside the loop)

$sys | Export-Csv (after the loop to send to .csv)

I even blogged the whole thing, including all the various iterations of learning I went through: http://dallasdbas.com/getting-to-know-powershell-from-an-old-dba/

Thanks to all that helped. This gave me a framework I will continue to use on these servers as the needs arise.

Kevin3NF

Kevin3NF
  • 113
  • 1
  • 8