Tìm kiếm trang web

Giám sát tính khả dụng của trang web SharePoint và gửi email thông báo bằng PowerShell


Yêu cầu: Tập lệnh giám sát các trang web SharePoint và kích hoạt Email cảnh báo khi có sự cố!

Tập lệnh PowerShell để giám sát các trang SharePoint:

Hãy có PowerShell để giám sát các trang web SharePoint của chúng tôi và kích hoạt Email khi chúng ngừng hoạt động!


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "SharePointMonitor@crescent.com"
$EmailTo = @("salaudeen.rajack@crescent.com")
$EmailSubject = "Alert: SharePoint site is Down!"
 
#Get Outgoing Email Server of the SharePoint Farm
$SMTPServer= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | % {$_.outboundmailserviceinstance.server.address}

#Make a web request to check site status
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true

#Iterate through each web application
Foreach($WebApp in (Get-SPWebApplication))
{
    $EmailBody=[string]::Empty
    #Get the timestamp
    $TimeStamp = Get-Date -f "yyyy-MM-dd HH:mm:ss"

    try
    {
        $Page = $WebClient.DownloadString($WebApp.Url)
    }
    catch [Exception]
    {
        $EmailBody += "The SharePoint site: $($WebApp.Url) is unavailable. Please take necessary action!<br><br>"
        $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
        $EmailBody += "<b>Exception:</b> " + $_.Exception.message + "<br><br>"
        $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

        #Send an Alert Email message
        Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl
    }
} 

Đây là Email cảnh báo đang hoạt động:

Lên lịch cho tập lệnh này trong bộ lập lịch tác vụ Windows để chạy 10 phút một lần.

Cập nhật: Điều gì sẽ xảy ra nếu trang web tải bị lỗi? Ví dụ. Thông báo lỗi "Xin lỗi, đã xảy ra lỗi".

Đây là tập lệnh được cập nhật để kiểm tra xem trang web có tải thông báo lỗi hay không:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "SharePointMonitor@crescent.com"
$EmailTo = @("salaudeen.rajack@crescent.com")
$EmailSubject = "Alert: SharePoint site is Down!"
 
#Get Outgoing Email Server of the SharePoint Farm
$SMTPServer= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | % {$_.outboundmailserviceinstance.server.address}

#Make a web request to check site status
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true

#Iterate through each web application
Foreach($WebApp in (Get-SPWebApplication))
{
    $EmailBody=[string]::Empty
    #Get the timestamp
    $TimeStamp = Get-Date -f "yyyy-MM-dd HH:mm:ss"

    try
    {
        $Page = $WebClient.DownloadString($WebApp.Url)

        if($Page.Contains("Sorry, something went wrong"))
        {            
            $EmailBody += "The SharePoint site: $($WebApp.Url) encountered with an unexpected error. Please take necessary action!<br><br>"
            $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
            $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

            #Send an Alert Email message
            Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl    
        }
    }
    catch [Exception]
    {
        $EmailBody += "The SharePoint site: $($WebApp.Url) is unavailable. Please take necessary action!<br><br>"
        $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
        $EmailBody += "<b>Exception:</b> " + $_.Exception.message + "<br><br>"
        $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

        #Send an Alert Email message
        Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl
    }
}

Bạn có thể thêm một số thông báo lỗi có thể xảy ra như: “không thể kết nối với cơ sở dữ liệu cấu hình”, “Lỗi máy chủ”, v.v.

Bài viết liên quan