Tuesday, July 19, 2016

Powershell - Find all the application pools that are stopped in IIS and restart

This script is used to loop through all the application pools and will restart the application pools that are stopped

  1. function RestartApplicationPool  
  2. {  
  3.     Clear-Host  
  4.   
  5.     $webapps = Get-WebApplication  
  6.     [string] $list = @()  
  7.       
  8.     foreach ($WebAppPools in get-childitem IIS:\AppPools\)  
  9.     {  
  10.         $name = "IIS:\AppPools\" + $WebAppPools.name  
  11.         $item = @{}  
  12.    
  13.         $item.WebAppName = $WebAppPools.name  
  14.         $item.State = (Get-WebAppPoolState -Name $WebAppPools.name).Value  
  15.   
  16.         if($item.State -eq 'Stopped')  
  17.         {  
  18.             Start-WebAppPool -Name $WebAppPools.name  
  19.             #$obj = New-Object PSObject -Property $item  
  20.             $list += $WebAppPools.name  
  21.             $list+=" and "  
  22.               
  23.         }  
  24.     }  
  25.     if($list.Length -gt 0)  
  26.     {  
  27.         $list = $list.Remove( $list.Length - 4, 4 )  
  28.     }  
  29.   
  30.     if($list.Length -gt 0) {  
  31.         Write-Host "Successfully restared the following " $list -ForegroundColor Green  
  32.     }  
  33.     else{  
  34.         Write-Warning "No application pools found to restart"  
  35.     }  
  36. }  
  37.   
  38. RestartApplicationPool  

This is how it will display the result, displays the Application pool that it starts


No comments:

Post a Comment

Popular Posts