How to check Web Application Pools status and restart IIS remotely on multiple servers?

During work in large scale ADFS environment there is often situation when you are forced to quickly restart IIS on multiple machines. Of course you won’t spend whole day doing this manually. In this article I would like to share one of my scripts which will help to determine the status of Web Application Pools and then restart IIS if needed.

On the beginning of this script I added some lines to expand the window size a little bit:

            
	# Window size
	$pshost = Get-Host              
	$pswindow = $pshost.UI.RawUI    #

	$newsize = $pswindow.BufferSize # Get the UI's current Buffer Size.
	$newsize.width = 170            # Set the new buffer's width to 150 columns.
	$pswindow.buffersize = $newsize # Set the new Buffer Size as active.

	$newsize = $pswindow.windowsize # Get the UI's current Window Size.
	$newsize.width = 170            # Set the new Window Width to 150 columns.
	$pswindow.windowsize = $newsize # Set the new Window Size as active.
            

For this script you need to prepare CSV file with server names. I added “Server” header in first row in servers column:

Server list in CSV format. IIS Restart.
Server list in CSV format. IIS Restart.
$List = ( Import-Csv "D:\Temp\Servers.csv" )
$Servers = $List | Out-GridView -Title  "Select Servers (Holding CTRL Button)" -PassThru 

ForEach($Server in $Servers."Server"){

.....

}

To get status of Web Application Pools from remote server you can use this simple command:


Invoke-Command -ComputerName $Server -ScriptBlock{ Get-WebAppPoolState }

For restarting IIS we will use again invoke-command to run IISReset:


Invoke-Command -ComputerName $i -ScriptBlock { iisreset }

Final script:


	# Window size
	$pshost = Get-Host              
	$pswindow = $pshost.UI.RawUI    #

	$newsize = $pswindow.BufferSize # Get the UI's current Buffer Size.
	$newsize.width = 170            # Set the new buffer's width to 150 columns.
	$pswindow.buffersize = $newsize # Set the new Buffer Size as active.

	$newsize = $pswindow.windowsize # Get the UI's current Window Size.
	$newsize.width = 170            # Set the new Window Width to 150 columns.
	$pswindow.windowsize = $newsize # Set the new Window Size as active.



# Script title 
Write-Host "`n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------`n"
Write-Host "                                              App Pools check script: " -ForegroundColor Green -NoNewline
Write-Host "Please select servers from pop-up list `n"
Write-Host "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------`n"
Pause

$List = ( Import-Csv "D:\Temp\Servers.csv" )
       
    # Opening new window to select servers
    $Servers = $List | Out-GridView -Title  "Select Servers (Holding CTRL Button)" -PassThru 
   
    # If options have not been selected write warning in host
    If(!$Servers)
    {
        Write-Warning "Servers have not been selected"
    }
    Else
    {
        $APPArray = @()
        
        ForEach($Server in $Servers."Server")
        {
            Write-Host "Processing $Server" -ForegroundColor Yellow -NoNewline
        
            # Getting server application pools
            $AppPools = Invoke-Command -ComputerName $Server -ScriptBlock{ Get-WebAppPoolState }
		           
            # Creating PS Object and adding Server name
            $APPObject = New-Object PSObject
            $APPObject | Add-Member Noteproperty "Server" -Value $Server

            # Loop for each application pool
            ForEach($Item in $AppPools)
            {                
                $Name = ($Item.ItemXPath -Replace '(?:.*?)name=''([^'']*)(?:.*)', '$1').Trim()
                $Name = '"' + "$Name" + '"'
                $Status = ($item.Value).Trim()

                # Adding status for each application pools   
                $APPObject | Add-Member Noteproperty "$($Name)" -Value "$($Status)"
            } 
                                               
        # Add custom object to our array
        $APPArray += $APPObject
        $APPObject | Out-String

	}	
    }

If($APPArray)
{
    # Confirm if you want to open results in new window
    Write-Host -NoNewline "`nDo you want to open results in pop-up window and select servers for IIS reset? (Y/N): "
    $Response = Read-Host
    Write-Host " "

        # If response was different that Y script will end
        If ( $Response -ne "Y" )
        { 
            Write-Warning "Script ends"       
        }
        Else
        {
            # Select servers for IIS Reset
            $IISReset = $APPArray | Out-GridView -Title  "Select Servers for IIS reset (Holding CTRL Button)" -PassThru 
                
                If(!$IISReset )
                {
                    Write-Warning "Servers have not been selected"
                }
                Else
                {
                   ForEach($i in $IISReset."Server")
                   {
                        Write-Warning " Restaring ISS on $i"
                        
                        # Restarting IIS
                        $IIS = Invoke-Command -ComputerName $i -ScriptBlock { iisreset }
                        $IIS | Where-Object {$_ -like "*restarted"}
                   }
                }
        }
}
pause

For more information about Get-WebAppPoolState command please refer to link.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.