Share via


Monitoring Application Pools Across N Servers using Powershell

Problem Scenario

One of my duties as a System Administrator for Biztalk environments is to check the status of the application across the servers hosting the IIS. The task is to make sure that after the patching, vulnerability fixing or AdHoc Server Restarts, the set of application pools that are supposed to be running are in that state. If a particular application pool in the list is stopped, it has to be started and if the application pool is started, then it has to be recycled. The process is simple enough when the number of servers is very less, what if the number of servers is large...say fifty servers!! The manual execution becomes very cumbersome and most of the times boring as it is a mundane task.

Solution

Powershell is one of the most important tools in the arsenal of an administrator and they should make full use of it. So the PowerShell task makes the boring task automated and we just need to run the script from one of the servers and the script monitors the app pools from the list. It checks the status of the application pool, if it is stopped, then it is started. If it is already running then the application pool is recycled. The script accepts a configuration XML which contains the details about the server and the list of application pools that are to be monitored as per the requirement.

The Config File used to store the data for the script looks like below.

<Servers>  
  <Server>  
    <Name>LocalHost</Name>  
    <AppPoolList>  
      <AppPool>TempTest</AppPool>  
      <AppPool>BAMAppPool</AppPool>  
    </AppPoolList>  
  </Server>  
</Servers>

The config file can be configured to include N number of servers. The PowerShell script that addresses the issue is as below.

Function CheckAppPoolStatus  
{  
    
    param  
    (  
        $strConfigListPath  
    )  
    
    if(Test-Path $strConfigListPath)  
    {  
        [xml]$Config= Get-Content "$strConfigListPath/Config.xml"
            
        Import-Module webadministration  
    
        foreach($Server in  $Config.Servers.Server)  
        {  
            if(Test-Connection $Server.Name)  
            {  
                foreach($appPool in  $Server.AppPoolList.AppPool)  
    
                {  
                    $Name= $appPool.ToString()  
                    $Status = Invoke-Command -ComputerName $Server.Name {param($paramater) Get-WebAppPoolState $paramater} -Args $Name  
                    Write-Host $Status.Value  
                        
                    if($Status.Value -eq "Stopped")   
                    {  
                        #Start the App Pool If it is in stopped state  
                        Invoke-Command -ComputerName $Server.Name {param($paramater) Start-WebAppPool $paramater} -Args $Name  
                        Write-Host "The AppPool : $appPool has been started successfully"
                           
                    }  
                    else
                    {  
                            
                        # Recycle the application Pool  
                        Invoke-Command -ComputerName $Server.Name { param($paramater) Restart-WebAppPool $paramater} -Args $Name  
                        Write-Host "The application Pool $appPool was recycled successfully"
    
                    }  
    
                }  
    
            }  
            else
            {  
                write-Host "Connection to the server failed."
            }  
        }  
            
    }  
    
    else
    {  
        Writre-Host "Provided File Path Does Not exist"
    }  
        
}  

We just need to make sure that the user has rights to execute the script across the configured servers.

Conclusion

The PowerShell scripts read from a configuration file which can be modified to include different application pools for different servers. If the application pools are in stopped state then the script starts them. If the application pools are running, then the script restarts them.

See Also

More information about the Web Administration module can be found at

Following are the cmdlets that are used in this script

Download

 The script can be found on technet gallery at below link.