FeaturedPower ShellSCCMScripts

Check Patches Installed and send email

$RemoteComputers = get-content "C:\test\dpservers.txt"
ForEach ($Computer in $RemoteComputers)
{
      Try
          {
              Invoke-Command -Computername $Computer -ScriptBlock {
             
$head=@”
<style>
@charset “UTF-8”;

table
{
font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;
border-collapse:collapse;
}
td
{
font-size:1em;
border:1px solid #98bf21;
padding:5px 5px 5px 5px;
}
th
{
font-size:1.1em;
text-align:center;
padding-top:5px;
padding-bottom:5px;
padding-right:7px;
padding-left:7px;
background-color:#A7C942;
color:#ffffff;
}
name tr
{
color:#F00000;
background-color:#EAF2D3;
}
</style>
“@

             $Session = New-Object -ComObject “Microsoft.Update.Session”

$Searcher = $Session.CreateUpdateSearcher()

$historyCount = $Searcher.GetTotalHistoryCount()
$htmlbody = $Searcher.QueryHistory(0, $historyCount) |Where{$_.Date -gt (Get-Date).AddDays(-20)} | Where{$_.ClientApplicationID -eq ‘CcmExec’} | Select-Object Date,Title,@{name=”Operation”; expression={switch($_.operation){
    1 {“Installation”};
    2 {“Uninstallation”};
    3 {“Other”}}}},
    @{name=”Status”; expression={switch($_.resultcode){
    1 {“In Progress”};
    2 {“Succeeded”};
    3 {“Succeeded With Errors”};
    4 {“Failed”};
    5 {“Aborted”}}}} | ConvertTo-HTML -Head $head -PreContent “<H2>Pilot Machine $env:computername – Patch Status Report</H2>” | out-string

Send-MailMessage -SmtpServer “mailhost.domain.com” -Body $htmlbody -BodyAsHtml -From “email address” -To @(‘email address') -Subject “$env:computername Patch Report”

               } -ErrorAction Stop
          }
      Catch
          {
              Add-Content Unavailable-Computers.txt $Computer
          }
}

 

Show More

Related Articles

Back to top button