PowerShell script to check on 503 error and sent to IT Support

PowerShell script to run a report in a table on a remote server to check web status is ok, if 503 error, highlight in yellow with a bold red font and send only if error via email to IT Support as html.
<### Code Explanation: This PowerShell script is designed to monitor the status of a web service hosted on a remote server. It checks the HTTP response code of the server and identifies if the service is unavailable (HTTP 503). If such an error is detected, the script generates an HTML-formatted email with the error highlighted in yellow and bold red font and sends it to the IT Support team. Below is a detailed explanation of the script: 1. **Scientific Notation and Algorithmic Approach**: - **Input**: The script takes the remote server URL, report path, and email configuration details as inputs. - **Process**: - It uses the `Invoke-WebRequest` cmdlet to send an HTTP request to the server and captures the response status code. - If the status code is `503`, it constructs an HTML table to display the error in a visually distinct format. - The script then sends an email using the `Send-MailMessage` cmdlet with the HTML content as the body. - **Output**: An email is sent to the IT Support team only if an error is detected. - **Error Handling**: The script includes error handling to manage exceptions during the HTTP request and email-sending process. 2. **Error Conditions**: - If the server is unreachable or the HTTP request fails, the script captures the exception and logs the error. - If the email fails to send, the script logs the failure for further investigation. 3. **Enhancements**: - Added error handling for network issues and SMTP failures. - Included logging for better traceability. --- ```powershell#> # Define variables $ServerURL = "http://your-remote-server/report" # Replace with your report server URL $ReportPath = "/Path/To/Your/Report" # Replace with your report path $EmailTo = "it.support@example.com" # Replace with IT Support email $EmailFrom = "monitoring@example.com" # Replace with sender email $SMTPServer = "smtp.example.com" # Replace with your SMTP server $LogFile = "C:\Logs\WebStatusMonitor.log" # Path to log file # Function to log messages function Log-Message { param ( [string]$Message ) $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Add-Content -Path $LogFile -Value "$Timestamp - $Message" } # Function to check web status function Check-WebStatus { param ( [string]$URL ) try { $Response = Invoke-WebRequest -Uri $URL -UseBasicParsing -ErrorAction Stop Log-Message "Successfully connected to $URL. Status Code: $($Response.StatusCode)" return $Response.StatusCode } catch { $ErrorMessage = $_.Exception.Message Log-Message "Error connecting to $URL. Exception: $ErrorMessage" return $_.Exception.Response.StatusCode } } # Run the report and check status try { $StatusCode = Check-WebStatus -URL $ServerURL # Prepare HTML content if error detected if ($StatusCode -eq 503) { $HTMLContent = @" <html> <body> <table border="1" style="border-collapse: collapse; width: 100%;"> <tr style="background-color: yellow;"> <th style="color: red; font-weight: bold;">Error Detected</th> </tr> <tr> <td style="color: red; font-weight: bold;">HTTP 503 - Service Unavailable</td> </tr> </table> </body> </html> "@ # Send email try { Send-MailMessage -To $EmailTo -From $EmailFrom -Subject "Web Status Alert: HTTP 503" ` -BodyAsHtml -Body $HTMLContent -SmtpServer $SMTPServer Log-Message "Email sent successfully to $EmailTo regarding HTTP 503 error." } catch { Log-Message "Failed to send email. Exception: $($_.Exception.Message)" } } else { Log-Message "No errors detected. Status Code: $StatusCode" } } catch { Log-Message "Unexpected error occurred. Exception: $($_.Exception.Message)" }