Chapters/scripting-at-scale/GetEventlogs-Start.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"
)

#get log data
Write-Host "Gathering $($EntryType -join ",") entries from $logname after $after from $($computername -join ",")"  -ForegroundColor cyan

$logParams = @{
    Computername = $Computername
    EntryType    = $EntryType
    Logname      = $Logname
    After        = $After
}

$data = Get-EventLog @logParams

#create html report
$fragments = @()
$fragments += "<H1>Summary from $After</H1>"
$fragments += "<H2>Count by server</H2>"
$fragments += $data | Group-Object -Property Machinename |
Sort-Object -property Count -Descending | Select-Object -property Count, Name |
ConvertTo-Html -As table -Fragment
$fragments += "<H2>Count by source</H2>"
$fragments += $data | Group-Object -Property source |
Sort-Object Count -Descending | Select-Object -property Count, Name |
ConvertTo-Html -As table -Fragment

$fragments += "<H2>Detail</H2>"
$fragments += $data | Select-Object -property Machinename, TimeGenerated, Source, EntryType, Message |
ConvertTo-Html -as Table -Fragment

$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

#end of scriptragments += $data | Group-Object -Property source |

Sort-Object Count -Descending | Select-Object -property Count, Name |

ConvertTo-Html -As table -Fragment



$fragments += "<H2>Detail</H2>"

$fragments += $data | Select-Object -property Machinename, TimeGenerated, Source, EntryType, Message |

ConvertTo-Html -as Table -Fragment



$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



#end of script