2015-04-08 101 views
1

我写了一个power shell脚本,用于监视IIS应用程序池的状态。使用当前的方法,每当任何泳池发生故障时,都会向我发出一个警报,告诉泳池已停止,然后再次启动泳池并发送电子邮件,指出泳池已启动。由于我有大约50台服务器,所以邮件数量大量增加,有时会导致垃圾邮件。有人可以帮助我,让脚本扫描池目录并将结果放在文本/ html文件中,并向我发送邮件中这些池已关闭的列表。请在下面找到脚本: -App_Pool_Alert Powershell脚本

###################################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 "[email protected]" -from "[email protected]" -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 "[email protected]" -from "[email protected]" -subject "Application Pool:- $ApplicationPoolName is Up on $CompName " -Body "$ApplicationPoolName is Up and running fine." -SmtpServer #############   

      } 

} 

}} 

##################################### End of Script ########################################################## 
+0

为什么不直接在IF语句中写入字符串数组,然后在最后如果它不是空的,在电子邮件正文中发送电子邮件? –

回答

1

这是我怎么会重写剧本:

###################################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 "[email protected]" -from "[email protected]" -subject "Application Pool Maitenance" -Body $result -SmtpServer $SMPTServer 
} 
##################################### End of Script ########################################################## 

首先定义一个$结果变量,在这种情况下只是一个字符串。

使用您的脚本块,您可以使用写入输出向管道输出写入任何内容。该输出从Ivoke-Command返回并添加到$ result变量中。

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

+0

感谢您发布答案。我测试了上面的逻辑和它的工作,但问题是结果是在一个邮件即将到来,即如果100个网站正在发送一个邮件列表全部100.当我试图写在文本文件中的输出脚本不写结果的远程计算机。任何与此有关的想法如何将输出写入文本文件,以便通过附件命令将它发送给邮件。 – Abhishek

+0

我使用Out-File -FilePath“C:\ server \”命令在foreach($ Application in $ ApplicationPools)函数执行后写入输出。 – Abhishek