当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 监控 SharePoint 服务(例如定时服务)- 当它们出现故障时发送警报电子邮件!

作者:精品下载站 日期:2024-12-14 14:08:38 浏览:14 分类:玩电脑

监控 SharePoint 服务(例如定时服务)- 当它们出现故障时发送警报电子邮件!


继续我的文章:使用 PowerShell 脚本监控 SharePoint 应用程序池,如果 SharePoint 服务器上的某些服务出现故障,可能会导致 SharePoint 环境中断或服务中断!尽管 SCOM 等监控解决方案可以监控服务,但它们无法在服务停止时自动启动服务。那么,让我们借助 PowerShell 来解决这个问题!这是我的漂亮的 PowerShell 脚本,用于在停止时启动所需的服务并发送警报电子邮件。

[玩转系统] 监控 SharePoint 服务(例如定时服务)- 当它们出现故障时发送警报电子邮件!

该脚本不仅扫描服务可用性,还:

  • 检测服务状态(如果未处于启动状态)
  • 如果服务处于停止状态,则自动启动该服务
  • 向 SharePoint 管理团队(或任何配置人员!)发送警报电子邮件

用于监控服务和发送电子邮件通知的 PowerShell 脚本:


Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Configuration variables
$EmailFrom = "[email protected]"
$EmailTo =  "[email protected]" # Use commas for multiple addresses
$EmailSubject = "Service(s) went down in SharePoint Server"

#Services to Monitor
$ServicesToMonitor =  "IISADMIN", "SPTimerV4", "SPAdminV4", "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC"

#Get Outgoing Email Server of the SharePoint Farm
$SMTP= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | %{$_.outboundmailserviceinstance.server.address}

#Check the status of each service
Foreach($ServiceName in $ServicesToMonitor)
{
 #Get the service
 $Service = Get-Service -Name $ServiceName
 
 #Check the Service status
 if ($Service.Status -ne "Running")
 {
  Write-Host $Service.DisplayName Found Not running!
  
  Try
  {
     #Set the Error Action
     $ErrorActionPreference = "Stop"
   #Try to start the service
   Start-Service $ServiceName
  }
  catch
  {
    Write-Host "Attempt to start service failed. Find the Error Message below:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
  }
  finally
  {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
  }

 #Send out the Alert E-mail 
 $EmailBody = "Hi SharePoint Team, `n `n The Service: $($Service.DisplayName) was found in stopped state!. `n`nWe tried Re-starting it... Current State of the Service: $($Service.Status). `n`nPlease take necessary actions if its not started! `n `nThanks, `nSharePoint Monitoring Script."
  Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTP -usessl
 }
}

如何监控SharePoint场每台服务器上的服务?

只需将参数“-ComputerName $Server.Address”添加到“Get-Service”和“Start-Service”cmdlet 中即可。

所以,代码是这样的:


#Get all SharePoint Servers
$ServersColl = Get-SPServer | where { $_.role -ne "Invalid"}

Foreach($Server in $ServersColl)
{
 #Wrap the above code
}

在任何应用程序服务器(或任何其他服务器都可以!)的 Windows 任务计划程序中安排此 PowerShell 脚本,以定期扫描应用程序池状态,例如每 5 分钟一次!可以根据您的应用程序优先级调整运行间隔。这是关于使用 Windows 任务计划程序调度 PowerShell 脚本的另一篇文章:使用 Windows 任务计划程序为 PowerShell 脚本创建计划任务

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯