Chapters/basic-controllers-scripts-and-menus/processcontroller.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")]
[ValidateScript({Test-Path $_})]
[string]$OutputPath = "c:\work",

[string]$SendTo
)

#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 -Property 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>
"@


$convertParams = @{
    Body = $fragments 
    PostContent = "<h6>$(Get-Date)</h6>" 
    Head = $head
}
$html = ConvertTo-Html @convertParams

#save results to a file
$file =  "$(Get-Date -UFormat '%Y%m%d_%H%M')_EventlogReport.htm"
$filename = Join-Path -Path $OutputPath -ChildPath $file
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 Cyan
    Send-MailMessage @mailParams
}

#end of script
$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> "@



$convertParams = @{

    Body = $fragments 

    PostContent = "<h6>$(Get-Date)</h6>" 

    Head = $head

}

$html = ConvertTo-Html @convertParams



#save results to a file

$file =  "$(Get-Date -UFormat '%Y%m%d_%H%M')_EventlogReport.htm"

$filename = Join-Path -Path $OutputPath -ChildPath $file

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 Cyan

    Send-MailMessage @mailParams

}



#end of script