Chapters/scripting-at-scale/geteventlogs.ps1
#Requires -version 5.0 [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory)] [ValidateNotNullorEmpty()] [string[]]$Computername, [ValidateSet("Error","Warning","Information","SuccessAudit","FailureAudit")] [string[]]$EntryType = @("Error","Warning"), [ValidateSet("System","Application","Security", "Active Directory Web Services","DNS Server")] [string]$Logname = "System", [datetime]$After = (Get-Date).AddHours(-24), [Alias("path")] [string]$OutputPath = "c:\work", [string]$SendTo ) #get log data Write-Host "Gathering $($EntryType -join ",") entries from $logname after $after." -ForegroundColor cyan #invoke the command remotely foreach ($computer in $computername) { Write-Host "Querying $computer" -ForegroundColor yellow } $data = Invoke-Command { Get-EventLog -LogName $using:logname -After $using:after -EntryType $using:entrytype } -ComputerName $Computername if ($data) { Write-Host "Creating HTML report" -ForegroundColor Cyan #create html report $fragments = @() $fragments += "<H1>Summary from $After</H1>" $fragments += "<H2>Count by server</H2>" $fragments += $data | group -Property Machinename | Sort Count -Descending | Select Count,Name | ConvertTo-HTML -As table -Fragment $fragments += "<H2>Count by source</H2>" $fragments += $data | group -Property source | Sort Count -Descending | Select Count,Name | ConvertTo-HTML -As table -Fragment $fragments += "<H2>Detail</H2>" $fragments += $data | Select Machinename,TimeGenerated,Source,EntryType,Message | ConvertTo-html -as Table -Fragment # the here string needs to be left justified $head = @" <Title>Event Log Summary</Title> <style> h2 { width:95%; background-color:#7BA7C7; font-family:Tahoma; font-size:10pt; font-color:Black; } body { background-color:#FFFFFF; font-family:Tahoma; font-size:10pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> "@ $html = ConvertTo-Html -Body $fragments -PostContent "<h6>$(Get-Date)</h6>" -Head $head #save results to a file $filename = Join-path -Path $OutputPath -ChildPath "$(Get-Date -UFormat '%Y%m%d_%H%M')_EventlogReport.htm" Write-Host "Saving file to $filename" -ForegroundColor Cyan Set-content -Path $filename -Value $html -Encoding Ascii #email as an html message if ($SendTo) { $mailparams = @{ To = $SendTo Subject = "Event Log Report" Body = ($html| out-string) BodyAsHtml = $True } Write-Host "Sending email to $($mailparams.to)" -ForegroundColor green Send-MailMessage @mailParams } #if $sendto } #if data else { Write-Host "No matching event entries found." -ForegroundColor Magenta } |