$smtpFromAddress = ""
$smtpToAddresses = @(
''
)
$SmtpServer = "smtp"
## DO NOT EDIT BELOW THIS LINE ##
$ReportName = "App Portal - Computer Name Detection Method Report"
function Get-FlexeraAppPortalNameResolution {
[CmdletBinding()]
param(
[System.IO.FileInfo[]]$Path
,
[string]$ClientIP
)
function ConvertFrom-IISLog {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path
,
[switch]$ShowFields
)
process {
foreach ($PathItem In $Path) {
$Contents = Get-Content -Path $PathItem -Raw
$FieldHeaders = (([regex]::Split($Contents, [string]'^#Fields:', "Multiline")[1] -split '\n')[0]).Trim() -split '\s+' | ForEach-Object { $_.Trim() }
# $FieldHeaders = ($Contents | Select-String -Pattern '^#Fields:') -split '\s+' | Select-Object -Skip 1
if ($ShowFields) {
$FieldHeaders
return
}
Import-Csv -Delimiter ' ' -Header $FieldHeaders -Path $PathItem | Where-Object { $_.date -notmatch '^#' } | ForEach-Object {
$Item = $_
foreach ($FieldHeader In $FieldHeaders) {
if ($Item.$FieldHeader -eq '-') {
$Item.$FieldHeader = $null
}
}
$Item | Add-Member -MemberType ScriptProperty -Name 'TimeCreated' -Value { ([System.DateTime]"$($this.date) $($this.time)").ToLocalTime() }
$Item
}
}
}
}
filter filterByClientIP {
$Object = $_
if ($ClientIP) {
$Object | Where-Object 'c-ip' -eq $ClientIP
}
else {
$_
}
}
$regexPattern = '(Processor|Loader)\.aspx'
foreach ($LogFile In $Path) {
Write-Verbose "Path: $($LogFile)"
if ( ($Path | Select-String -Pattern $regexPattern).Count -eq 0 ) {
Write-Warning "Log does not contain search criteria. ($($LogFile))"
return
}
$LogFile | ConvertFrom-IISLog | Where-Object { $_.'cs-uri-stem' -match $regexPattern } | filterByClientIP
}
}
function Main {
if (!$Path) {
$Path = Get-ChildItem -Path "$env:SystemDrive\inetpub\logs\LogFiles\W3SVC1\*.log" | Select-Object -Last 1 -Skip 1
}
$FlexeraAppPortalNameResolution = Get-FlexeraAppPortalNameResolution -Path $Path | Sort-Object -Property @{Expression = {[datetime]"$($_.date) $($_.time)"}; Ascending = $true}
$FlexeraAppPortalNameResolution | Add-Member -MemberType NoteProperty -Name 'ap-timetaken' -Value $null
$FlexeraAppPortalNameResolution | Add-Member -MemberType NoteProperty -Name 'ap-method' -Value $null
$FlexeraAppPortalNameResolution = $FlexeraAppPortalNameResolution | Select-Object -ExcludeProperty 'cs(User-Agent)' -Property * | Group-Object -Property 'c-ip' | ForEach-Object {
$_.Group
}
$PreviousEntry = $null
$OutputObject = $FlexeraAppPortalNameResolution | ForEach-Object {
$Entry = $_
if ([System.String]::IsNullOrEmpty($Entry.'cs-username')) { return } # do not include entries with no user name
if ($Entry.'c-ip' -in @('')) { return } # do not include entries for monitoring server ips
if ($Entry.'c-ip' -in @('127.0.0.1')) { return } # do not include entries for localhost
if ($Entry.'cs-username' -in @('')) { return } # do not include entries for testers
$Entry.'ap-method' = [regex]::Match([string]($Entry.'cs-uri-query'), '^d=(?<apmethod>\w+)&').Groups['apmethod'].Value
if (($Entry.'cs-uri-query') -match '^d=' -and ([System.String]::IsNullOrEmpty($Entry.'ap-method'))) { $Entry.'ap-method' = 'blank' }
if ($Entry.'cs-uri-stem' -match [regex]::Escape('Processor.aspx') -and ( $Entry.'cs-username' -eq $PreviousEntry.'cs-username' )) {
$Entry.'ap-timetaken' = New-TimeSpan -Start ([datetime]"$($PreviousEntry.date) $($PreviousEntry.time)") -End ([datetime]"$($Entry.date) $($Entry.time)")
}
$PreviousEntry = $Entry
$Entry
}
$OutputObject
}
$HTMLHead = @'
<style type="text/css">
BODY{
background-color: #D8E9E8;
font-family: verdana;
color: #1A64A1;
font-size: 8pt;
cell-padding: 2pt;
}
TABLE{
font-size: 11px;
border-width: 1px;
border-style: solid;
border-color: #D6D6D6;
border-collapse: collapse;
border-spacing: 10px;
}
TH{
font-size: 18px;
color: white;
border-width: 1px;
padding: 4px;
border-style: solid;
border-color: #D6D6D6;
background-color: #1A64A1;
}
TD{
color: #1A64A1;
border-width: 1px;
padding: 4px;
border-style: solid;
border-color: #D6D6D6;
background-color: white;
}
</style>
'@
$HTMLPreContent = @"
This report runs as a scheduled task, once a day at 7 AM, from $($env:COMPUTERNAME).
<h1>$ReportName</h1>
"@
$JobDetail = Main
$JobDetailCount = $JobDetail | Where-Object { [System.String]::IsNullOrEmpty($_.'ap-method') -eq $false } | Measure-Object | Select-Object -ExpandProperty Count
$JobDetail_HTMLFragment = $JobDetail | ConvertTo-HTML -Fragment -PreContent "<h2>Results [$($JobDetailCount)]</h2>" | Out-String
$HTMLBody = ConvertTo-Html -Head $HTMLHead -PreContent $HTMLPreContent -PostContent $JobDetail_HTMLFragment | Out-String
$IADsObject = ([adsisearcher]"(&(objectclass=group)(proxyAddresses=smtp:$($smtpFromAddress)))").FindOne()
$smtpFrom = "$($IADsObject.Properties.name[0]) <$($smtpFromAddress)>"
$MailMessage_Splat = @{
From = $smtpFrom
To = @()
BodyAsHtml = $true
Body = $HTMLBody
SmtpServer = $SmtpServer
Subject = $ReportName
}
foreach ($SendTo In $smtpToAddresses) {
$IADsObject = ([adsisearcher]"(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(proxyAddresses=smtp:$($SendTo)))").FindOne()
if ( $IADsObject.Count -eq 1 ) {
$MailMessage_Splat.To += "$($IADsObject.Properties.name[0]) <$($SendTo)>"
Write-Host "Sending to '$SendTo'."
} else {
Write-Host "Not sending to '$SendTo'. User not found." -ForegroundColor Yellow
}
}
Send-MailMessage @MailMessage_Splat