2015-04-08 120 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