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

[玩转系统] 使用 PowerShell 脚本监控 SharePoint 的应用程序池

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

使用 PowerShell 脚本监控 SharePoint 的应用程序池


如果 SharePoint 应用程序池停止,将导致您的 SharePoint 环境出现“服务不可用”中断!虽然 SCOM 可以监视 SharePoint IIS Web 应用程序的应用程序池状态,但它会发出一些警报,并且在停止时不会自动启动应用程序池。那么,让我们借助 PowerShell 来解决这个问题!

此脚本不仅扫描 SharePoint Web 前端服务器上的 IIS 应用程序池状态,而且还:

  • 如果应用程序池未处于“已启动”状态,则记录其状态
  • 如果 AppPool 处于停止状态,则自动启动
  • 向 SharePoint 管理团队(或任何配置人员!)发送警报电子邮件

用于监视 IIS 应用程序池状态的 PowerShell 脚本:

这是我的漂亮的 PowerShell 脚本,用于监视所有 SharePoint Web 前端服务器上的应用程序池。


Import-Module WebAdministration

#Array to hold Server names - Change it to YOUR SharePoint front end servers
$WFEServers =("HS-WFE01", "HS-WFE02", "HS-WFE03")

#Log file location
$LogFile = "D:\Scripts\AppPool-Log.txt"

#Loop through each server and Check Application Pool status
foreach ($Server in $WFEServers)
{
    $ServerMgr = [Microsoft.Web.Administration.ServerManager]::OpenRemote($Server)

    #Get all Application Pools which are not in Started State
    $AppPoolColl = $ServerMgr.ApplicationPools | Where-Object {$_.State -ne "Started"}

    foreach($AppPool in $AppPoolColl)
    {
        #Get the time to Log
        $now = Get-Date -f "yyyy-MM-dd HH:mm:ss"

        #Log to file
        "`n Found Application Pool: $($AppPool.name) in stopped state at the server : $($Server) on $($now)" >> $LogFile
        "Trying to Start the application Pool...">> $LogFile
        
        #Try Starting the application Pool
        $AppPool.Start()
        Start-Sleep -s 10
        "Application Pool's current Status: $($AppPool.State)" >> $LogFile

        #Send Alert-Mail message
        $emailFrom = "[email protected]"
        # Use commas for multiple addresses
        $emailTo = "[email protected]"
        $subject = "Application Pool: $($AppPool.Name) in stopped state in Server: $($Server) at $($now)"
        $body = "Hi SharePoint Team, `n `n The Application Pool $($AppPool.name) was in stopped state in server: $($server). `n`n We tried Re-starting it... Current State of the Application Pool: $($AppPool.State). `n`n Please take necessary actions if its not started !. `n `nThanks, `nSharePoint AppPool Monitoring Script."
        $smtpServer = "smtp.crescent.com" #IP or HOST Name of SMTP Server
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body) 
    } 
}

以下是来自 IIS AppPool 监视脚本的示例警报:

[玩转系统] 使用 PowerShell 脚本监控 SharePoint 的应用程序池

在任何应用程序服务器(或任何其他服务器都可以!)的 Windows 任务计划程序中安排此 PowerShell 脚本,以定期扫描应用程序池状态,例如每 5 分钟一次!运行间隔可以根据您的应用程序优先级进行调整。

这是关于使用 Windows 任务计划程序调度 PowerShell 脚本的另一篇文章:使用 Windows 任务计划程序为 PowerShell 脚本创建计划任务

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

取消回复欢迎 发表评论:

关灯