首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >App_Pool_Alert Powershell脚本

App_Pool_Alert Powershell脚本
EN

Stack Overflow用户
提问于 2015-04-08 07:26:53
回答 1查看 1.7K关注 0票数 1

我编写了一个power shell脚本,用于监视IIS应用程序池的状态。使用当前的方法,每当任何池下降时,它都会向我抛出一个警告,即池被停止,然后它再次启动池,发送电子邮件,声明池已关闭。因为我有大约50台服务器,所以大量邮件涌入,有时会导致垃圾邮件。有人能帮我在这里,以便脚本将扫描池目录,并将结果放在一个文本/html文件,并发送给我的列表,这些池在邮件中。请查找以下脚本:-

代码语言:javascript
复制
###################################Declear Servers in text file##############################################

$Servers = Get-Content C:\Users\Desktop\server.txt

################ Scans each server and import IIS web-administration module##################################

$Servers | ForEach-Object {
            Invoke-Command -ComputerName $_ -ScriptBlock {
            Import-Module WebAdministration
            cd IIS:/AppPools
            $CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name
            $ApplicationPools = dir
foreach ($item in $ApplicationPools)
{
    $ApplicationPoolName = $item.Name
    $ApplicationPoolStatus = Get-WebAppPoolState $ApplicationPoolName
    If($ApplicationPoolStatus.value -eq "Stopped")

         {
            send-mailmessage -to "vvv@gmail.com" -from "xxx@gmail.com" -subject "Application Pool:- $ApplicationPoolName is Down on $CompName " -Body "$ApplicationPoolName is down. Please check IIS/Event logs for RCA." -SmtpServer ###########          

            Start-WebAppPool -Name $ApplicationPoolName

            send-mailmessage -to "vvv@gmail.com" -from "xxx@gmail.com" -subject "Application Pool:- $ApplicationPoolName is Up on $CompName " -Body "$ApplicationPoolName is Up and running fine." -SmtpServer #############         

          }

}

}}

##################################### End of Script ##########################################################
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-08 23:52:53

以下是我重写脚本的方式:

代码语言:javascript
复制
###################################Declear Servers in text file##############################################

$Servers = Get-Content C:\server.txt

################ Scans each server and import IIS web-administration module##################################

$SMPTServer = "mail.server.com"
$result = "The following application pools were restarted:`n`n" # default output 

$Servers | ForEach-Object {
    $result += Invoke-Command -ComputerName $_ -ScriptBlock { # add output of scriptblock to $result
        Import-Module WebAdministration
        cd IIS:/AppPools
        $CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name
        $ApplicationPools = dir
        foreach ($item in $ApplicationPools)
        {
            $ApplicationPoolName = $item.Name
            $ApplicationPoolStatus = Get-WebAppPoolState $ApplicationPoolName
            If($ApplicationPoolStatus.value -eq "Stopped")
                {    
                    Write-Output "Server $CompName - Application pool $ApplicationPoolName is Down - Restarting`n" # any action is added to otput
                    Start-WebAppPool -Name $ApplicationPoolName
                }
        }

    }
}
if ($result -ne "The following application pools were restarted:`n`n") { # If any action was taken send mail with $result
    send-mailmessage -to "vvv@gmail.com" -from "xxx@gmail.com" -subject "Application Pool Maitenance" -Body $result -SmtpServer $SMPTServer
}
##################################### End of Script ##########################################################

首先定义一个$result变量,在本例中仅定义一个字符串。

使用scriptblock,您可以使用写输出将任何东西写入管道输出。此输出从返回并添加到$result变量中。

在脚本的末尾,检查$result变量是否已更改,并在需要时将其作为电子邮件正文发送。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29508468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档