Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SMSAgentSoftware/c2630ebf42bd22ea78ff062b92d3d549 to your computer and use it in GitHub Desktop.

Select an option

Save SMSAgentSoftware/c2630ebf42bd22ea78ff062b92d3d549 to your computer and use it in GitHub Desktop.
Generates an HTML-styled report with Delivery Optimization statistics for local or remote Windows 10 computers
<#
.Synopsis
Generates an HTML-styled report with Delivery Optimization statistics for local or remote Windows 10 computers
.DESCRIPTION
Uses the built-in Windows 10 cmdlets for delivery optimization to generate a DO report in html format. Can run against the local or remote computers. Optionally sends the report as an email.
.EXAMPLE
New-HTMLDeliveryOptimizationReport
Creates an HTML DO report for the local machine and invokes it.
.EXAMPLE
New-HTMLDeliveryOptimizationReport -ComputerName "PC001","PC002"
Creates an HTML DO report for 2 remote computers and invokes them.
.EXAMPLE
New-HTMLDeliveryOptimizationReport -ComputerName "PC001","PC002" -AsEmail
Creates an HTML DO report for 2 remote computers and sends it using the parameter defaults for the email.
#>
function New-HTMLDeliveryOptimizationReport
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(ParameterSetName='Email')]
[switch]$AsEmail,
[Parameter(Mandatory=$false,
ParameterSetName='Email')]
[string]$To = 'my@recipient.com',
[Parameter(Mandatory=$false,
ParameterSetName='Email')]
[string]$From = 'my@sender.com',
[Parameter(Mandatory=$false,
ParameterSetName='Email')]
[string]$Smtpserver = 'relay.smtpserver.com',
[Parameter(Mandatory=$false,
ParameterSetName='Email')]
[string]$Subject = "Delivery Optimization Report"
)
Begin
{
# Html CSS style
$Style = @"
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4286f4;
color: white;
}
</style>
"@
}
Process
{
Foreach ($Computer in $ComputerName)
{
# If local machine
If ($Computer -eq $env:COMPUTERNAME)
{
# Test for elevation
If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
}
Else
{
Write-Warning "Please run elevated for the local machine!"
Return
}
}
# Check that the system is online
If (Test-Connection -ComputerName $Computer -Count 2 -Quiet)
{
# Get Delivery Optimization Status
$DOStatus = Invoke-Command -ComputerName $Computer -ScriptBlock { Try{Get-DeliveryOptimizationStatus -Verbose -ErrorAction Stop}Catch{} }
If ($DOStatus.Count -ge 1 -and $DOStatus.GetType().Name -ne "String")
{
$Properties = $DOStatus[0] | Get-Member -MemberType Property | Select -ExpandProperty Name
$DOStatusHTML = $DOStatus |
ConvertTo-Html -Property $Properties -Head $Style -Pre "<h2>Delivery Optimization Status</h2>" -CssUri "http://www.w3schools.com/lib/w3.css" -Body "<h1>Delivery Optimization Report for $Computer</h1>" |
Out-String
}
ElseIf ($DOStatus.GetType().Name -eq "String")
{
$DOStatusHTML = ConvertTo-Html -Head $Style -Pre "<h2>Delivery Optimization Status</h2><p>$DOStatus</p>" -CssUri "http://www.w3schools.com/lib/w3.css" -Body "<h1>Delivery Optimization Report for $Computer</h1>" |
Out-String
$DOStatusHTML = $DOStatusHTML.Replace('<table>','')
$DOStatusHTML = $DOStatusHTML.Replace('</table>','')
}
# Get Delivery Optimization Snapshot
$DOPerfSnap = Invoke-Command -ComputerName $Computer -ScriptBlock { Try{Get-DeliveryOptimizationPerfSnap -Verbose -ErrorAction Stop}Catch{} }
If ($DOPerfSnap.Count -ge 1 -and $DOPerfSnap.GetType().Name -ne "String")
{
$DOPerfSnapTable = New-Object System.Data.DataTable
[void]$DOPerfSnapTable.Columns.Add("Property")
[void]$DOPerfSnapTable.Columns.Add("Value")
Foreach ($Item in $DOPerfSnap)
{
[void]$DOPerfSnapTable.Rows.Add($Item.Split()[0], $Item.Split()[-1])
}
$DOPerfSnapHTML = $DOPerfSnapTable |
ConvertTo-Html -Property "Property","Value" -Head $Style -Body "<h2>Delivery Optimization Performance Snapshot</h2>" -CssUri "http://www.w3schools.com/lib/w3.css" |
Out-String
}
ElseIf ($DOPerfSnap.GetType().Name -eq "String")
{
$DOPerfSnapHTML = ConvertTo-Html -Head $Style -Body "<h2>Delivery Optimization Performance Snapshot</h2><p>$DOPerfSnap</p>" -CssUri "http://www.w3schools.com/lib/w3.css" |
Out-String
$DOPerfSnapHTML = $DOPerfSnapHTML.Replace('<table>','')
$DOPerfSnapHTML = $DOPerfSnapHTML.Replace('</table>','')
}
# Get Delivery Optimization Snapshot This month
$DOPerfSnapTM = Invoke-Command -ComputerName $Computer -ScriptBlock { Try{Get-DeliveryOptimizationPerfSnapThisMonth -Verbose -ErrorAction Stop}Catch{} }
If ($DOPerfSnapTM.Count -ge 1 -and $DOPerfSnapTM.GetType().Name -ne "String")
{
$DOPerfSnapTMTable = New-Object System.Data.DataTable
[void]$DOPerfSnapTMTable.Columns.Add("Property")
[void]$DOPerfSnapTMTable.Columns.Add("Value")
Foreach ($Item in $DOPerfSnapTM)
{
[void]$DOPerfSnapTMTable.Rows.Add($Item.Split()[0], $Item.Split()[-1])
}
$DOPerfSnapTMHTML = $DOPerfSnapTMTable |
ConvertTo-Html -Property "Property","Value" -Head $Style -Body "<h2>Delivery Optimization Performance Snapshot This Month</h2>" -CssUri "http://www.w3schools.com/lib/w3.css" |
Out-String
}
ElseIf ($DOPerfSnapTM.GetType().Name -eq "String")
{
$DOPerfSnapTMHTML = ConvertTo-Html -Head $Style -Body "<h2>Delivery Optimization Performance Snapshot This Month</h2><p>$DOPerfSnapTM</p>" -CssUri "http://www.w3schools.com/lib/w3.css" |
Out-String
$DOPerfSnapTMHTML = $DOPerfSnapTMHTML.Replace('<table>','')
$DOPerfSnapTMHTML = $DOPerfSnapTMHTML.Replace('</table>','')
}
$HTML = $HTML + $DOStatusHTML + $DOPerfSnapHTML + $DOPerfSnapTMHTML
# Send the email or invokes the report
If ($HTML -ne $null)
{
If ($PSBoundParameters['AsEmail'])
{
$Subject = $Subject + " | $Computer | $((Get-Date).ToLongDateString())"
Send-MailMessage -To $To -From $From -SmtpServer $Smtpserver -Subject $Subject -Body $Html -BodyAsHtml
$Subject = "Delivery Optimization Report"
}
Else
{
$HTML | Out-file -FilePath "$env:TEMP\DOReport-$Computer.html" -Force
Invoke-Item "$env:TEMP\DOReport-$Computer.html"
}
}
Remove-Variable DOStatus -ErrorAction SilentlyContinue
Remove-Variable HTML -ErrorAction SilentlyContinue
}
}
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment