Created
March 27, 2024 23:50
-
-
Save blakedrumm/0fb6241a2ae07ee89790569a34dddc70 to your computer and use it in GitHub Desktop.
This script allows you to gather the latest update for SCOM 2019 and output to a table for an email let you know if there are updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Blake Drumm ([email protected]) | |
# Date Created: March 27th, 2024 | |
# Website: https://blakedrumm.com/ | |
# Define the HTML content or URL | |
$url = "https://www.catalog.update.microsoft.com/Search.aspx?q=System+Center+2019+-+Operations+Manager" | |
try { | |
# Get the HTML content of the webpage | |
$htmlContent = Invoke-WebRequest -Uri $url | |
} catch { | |
Write-Error "Failed to retrieve the webpage. Error: $_" | |
exit | |
} | |
# Define regular expression pattern to extract table rows | |
$tableRowRegex = '<tr[^>]*>(.*?)<\/tr>' | |
# Find all table rows in the HTML content | |
$tableRows = [regex]::Matches($htmlContent.Content, $tableRowRegex, [System.Text.RegularExpressions.RegexOptions]::Singleline) | |
# Define date format for comparison | |
$dateFormat = "M/d/yyyy" | |
# Get the current date | |
$currentDate = Get-Date | |
# Check if any table rows were found | |
if ($tableRows.Count -eq 0) { | |
Write-Output "No table rows found in the HTML content." | |
exit | |
} | |
$final = @() | |
# Iterate through table rows | |
foreach ($row in $tableRows) { | |
$rowHtml = $row.Groups[1].Value | |
# Check if the row contains a date | |
if ($rowHtml -match '(?:0?[1-9]|1[0-2])\/(?:0?[1-9]|[12][0-9]|3[01])\/(?:20)\d\d') { | |
$dateString = $matches[0] | |
try { | |
$date = [datetime]::ParseExact($dateString, $dateFormat, $null) | |
$daysDifference = ($currentDate - $date).Days | |
# Check if the date is within the last 30 days | |
if ($daysDifference -le 30 -and $daysDifference -ge 0) { | |
$final += @" | |
<tr> | |
$rowHtml | |
</tr> | |
"@ | |
} | |
} catch { | |
Write-Warning "Failed to parse date: $dateString. Error: $_" | |
} | |
} | |
} | |
cls | |
# Email content | |
$emailFrom = "[email protected]" | |
$emailTo = "[email protected]" | |
$subject = "System Center 2019 - Operations Manager Updates" | |
$body = @" | |
<table border='1'><tr><th></th><th>Update Name</th><th>Products</th><th>Classification</th><th>Last Updated</th><th>Version</th><th>Size</th><th>Download</th></tr> | |
$final | |
</table> | |
<p>For more details, visit the <a href='$url'>Microsoft Update Catalog</a>.</p> | |
"@ | |
$smtpServer = "smtp.gmail.com" | |
$smtpPort = 587 | |
$smtpUser = $emailFrom | |
$smtpPassword = 'yourpasswordhere' | |
# Send the email | |
Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtpServer -port $smtpPort -Credential (New-Object System.Management.Automation.PSCredential ($smtpUser, (ConvertTo-SecureString $smtpPassword -AsPlainText -Force))) -UseSsl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment