February 23, 2017

Script to Report on Recent Deployments

This Script can be used to automate reporting on application deployment. With the current settings it gathers deployment summary for the deployments that was created within the last 14 days and sends an email to the defined recipient(s).

All you have to do is to change the variables under #Mail and #Variables to match your domain settings and verifiy the import-module path.

After that you can set up a scheduled task that runs this script with the frequency that you want.

#Mail
$smtpServer = "smtprelay.domain.com"
$mailinfo_from = "server@domain.com"
$mailinfo_to = "initials@domain.com", "initials2@domain.com"
$mailinfo_cc = ""
$subject = "Deployment Summary Report"
$logoURL = "<Img src='https://www.url.com/picture.gif' style='float:left' width='99' height='75' hspace=10>"

#Variables
$ComputerName="sccmserver.domain.com"
$SiteCode = "SiteCode"
$ReportTitle="System Report"
$period = 14
$OutFile = "$PSScriptRoot\DeploymentReport.html"

Import-Module '\\$ComputerName\d$\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'

$head = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;margin-left:50px}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #8c0000;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd  { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<Title>$ReportTitle</Title>
"@

#Computer system
$cs = Get-WmiObject -Class Win32_Computersystem -ComputerName $computername
$csdata = $cs | Select Status,Manufacturer,Model,SystemType,Number*

#Deployment Summary
$timeSpan = (Get-Date).Adddays(-$period)
$GetDeployments = Get-WmiObject -Namespace root\sms\site_$SiteCode -Class SMS_DeploymentSummary -ComputerName $ComputerName | Where-Object {($_.FeatureType -eq "1") -AND ($_.CollectionName -like "*-install (device)") -AND ($_.CollectionName -like "*-install (device)") -AND ($_.ConvertToDateTime($_.CreationTime) -gt $timeSpan)}
$deploymentSummary = $GetDeployments | Select-Object CollectionName,@{Label="CreationTime";Expression={$_.ConvertToDateTime($_.CreationTime)}},@{Label="Deployment";Expression={$_.SoftwareName}},NumberSuccess,NumberInProgress,NumberOther,NumberErrors,FeatureType
cd dd1:
$deploymentSummary=$deploymentSummary | Where-Object {(Get-CMApplication -Name $_.Deployment).IsSuperseded -eq $false}

#Write results depending on parameter set
$footer="Report ran from {3} initiated by {1}\{2}" -f (Get-Date),$env:USERDOMAIN,$env:USERNAME,$env:COMPUTERNAME

#Prepare HTML code
$fragments = @()

#Insert a graphic header
$fragments += "$logoURL<br><br>"
$fragments += $deploymentsummary | ConvertTo-HTML -Fragment -PreContent "<H2>Deployment Summary last $period days</H2>"

Write $fragments | clip
cd c:\
ConvertTo-Html -Head $head -Title $ReportTitle -PreContent ($fragments | out-String) -PostContent "<br><br><font color='red'>Superseeded Deployments are not displayed even tho they have been created within the last $period days</font><br><br><I>$footer</I>" | Out-File $OutFile

$Body = Get-Content $OutFile

#If there are no deployments created within the treshhold dont send a mail
If ($deploymentSummary.Count -notlike 0){
    Send-MailMessage -SmtpServer $smtpServer -To $mailinfo_to -From $mailinfo_from -Subject $subject -Body "$Body" -BodyAsHtml -Attachments $OutFile
}
Get-Item $OutFile | Remove-Item -Force

Script to Report on Software Update Group compliance

This script will collect the status of the last created Software Update Group and send it as a mail to the specified recipient(s).

All you have to do is to change the variables under #Mail and #Variables to match your domain settings.

After that you can set up a scheduled task that runs this script with the frequency that you want.

Import-Module '\\sccmserver\d$\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'

#Mail
$smtpServer = "smtprelay.customer.com"
$mailinfo_from = "server1@customer.com"
$mailinfo_to = "initials@company.com"
$mailinfo_cc = ""
$subject = "Windows 10 Patch Compliance - "

#Variables
$tableheading = "Windows 10 Patch Compliance - "
$logoURL = "<Img src='https://www.url.com/picture.gif' style='float:left' width='99' height='75' hspace=10>"
[string]$SavePath = "c:\temp\Compliance Reports"
$year = (Get-Date).year
$month = Get-Date -Format MM
$SiteCode = "SiteCode"


$ReportTitle = "System Report"
[string]$reportVersion = "1.0"


$head = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;margin-left:50px}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #8c0000;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd  { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<Title>$ReportTitle</Title>
"@


#Access the SCCM Site
Set-Location $SiteCode:

#Finding the latest created Software Update Group

$SUG = Get-CMSoftwareUpdateGroup | Sort-Object { $_.datecreated } -Descending | Select -First 1

$updategroupname = $SUG.LocalizedDisplayName

#Getting all updates on that Software update Group

$Updates = (Get-CMSoftwareUpdate -UpdateGroupName $updategroupname) | Select-Object Vendor, ArticleID, BulletinID, @{E = { $_.LocalizedDisplayName }; L = "Titel" }, @{E = { $_.NumPresent }; L = "Installed" }, @{E = { $_.NumMissing }; L = "Required" }, @{E = { $_.NumNotApplicable }; L = "Not Required" } , @{E = { $_.NumUnknown }; L = "Unknown" }, @{E = { $_.NumTotal }; L = "Total" }, @{E = { [math]::Round((($_.NumPresent / ($_.NumPresent + $_.NumMissing)) * 100), 2) }; L = "% Compliant" }, @{E = { [math]::Round((($_.NumMissing / ($_.NumPresent + $_.NumMissing)) * 100), 2) }; L = "% Not Compliant" }, @{E = { [math]::Round((($_.NumUnknown / $_.NumTotal) * 100), 2) }; L = "% Unknown" }

#Write results depending on parameter set
$footer = "Report ran from {3} initiated by {1}\{2}" -f (Get-Date), $env:USERDOMAIN, $env:USERNAME, $env:COMPUTERNAME

#Prepare HTML code
$fragments = @()

#Insert a graphic header
$fragments += "$logoURL<br><br>"

$fragments += $Updates | ConvertTo-HTML -Fragment -PreContent "<H2>$tableheading $updategroupname</H2>"


write $fragments | clip
$outFile = ConvertTo-Html -Head $head -Title $ReportTitle -PreContent ($fragments | out-String) -PostContent "<br><I>$footer</I>"


#Checks savepath and export file.
IF ((test-path $SavePath) -eq $False) { md $SavePath }
$ExportFile = "$SavePath\Win 10 Compliance-$updategroupname.csv"
IF (Test-Path $ExportFile) { Remove-Item $ExportFile }


$Updates | export-csv $ExportFile -Append -Force -NoTypeInformation

$body = "" + $OutFile

Send-MailMessage -SmtpServer $smtpServer -From $mailinfo_from -To $mailinfo_to -Subject "$subject $updategroupname" -Body $body -BodyAsHtml -Attachments $ExportFile