Recycle Web App Pool using PowerShell Function

Today we will go through process for creating PowerShell function which will recycle Web App Pool remotely. For those who are working with Web Applications this might be helpful.

To recycle/restart single Web App Pool we can use one of the commands available in WebAdministration moduleRestart-WebAppPool:

	
$Recycle = Invoke-Command $Server -ScriptBlock{param($AppPool)Restart-WebAppPool -name "$using:AppPool"} -ArgumentList $AppPool -Credential $Cred
	

First step which you have to do is adding your Web App Pools in param sections [ValidateSet(“.NET v2.0″,”.NET v2.0 Classic”,”.NET v4.5″,”.NET v4.5 Classic”):

	
[Parameter(Position=1, Mandatory = $True, HelpMessage='Please select Web App Pool name', ValueFromPipeline = $true)]
[ValidateSet(".NET v2.0",".NET v2.0 Classic",".NET v4.5",".NET v4.5 Classic")][string]
$AppPools
	

Then while executing Recycle-WebAppPool function script will display them in pop-up list:

Recycling Web App Pools
Recycling Web App Pools

Usage:

Recycle-WebAppPool -Servers DC01 -AppPools '.NET v4.5'
Recycle-WebAppPool -Servers DC01 -AppPools '.NET v4.5','.NET v4.5 Classic'
Recycle-WebAppPool -Servers DC01,DC02 -AppPools '.NET v4.5'
Recycle-WebAppPool -Servers (GC "C:\Temp\servers.txt) -AppPools '.NET v4.5'

Final script:


Function Recycle-WebAppPool {
    [CmdletBinding()]        
 
    # Parameters used in this function
    param
    (
        [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide server name", ValueFromPipeline = $true)] 
        $Servers,
 
        [Parameter(Position=1, Mandatory = $True, HelpMessage='Please provide select Web App Pool name', ValueFromPipeline = $true)]
        [ValidateSet(".NET v2.0",".NET v2.0 Classic",".NET v4.5",".NET v4.5 Classic")][string]
        $AppPools
    ) 

    # Checking module
    Try
    {
        Import-Module WebAdministration
    }
    Catch
    {
        $_.Exception.Message
        Break
    }
        
        # Credentials        
        $Cred = Get-Credential $env:USERNAME      
        
        # Array for final results
        $APPArray = @()

        # Looping each server
        ForEach($Server in $Servers)
        {
            Write-Host "Processing $Server" -ForegroundColor yellow

            # Looping each App Pool
            ForEach( $AppPool in $AppPools )
            {
                # Checking App Pool by name                
                $AppItem = (Invoke-Command $Server -ScriptBlock{param($AppPool) Get-WebAppPoolState | Where-Object {$_.ItemXPath -match "$using:AppPool"} } -ArgumentList $AppPool -Credential $Cred)[0] 
                                
                If($AppItem -ne $Null)
                {
                    Try
                    {
                        # Recycle App Pool
                        $Recycle = Invoke-Command $Server -ScriptBlock{param($AppPool)Restart-WebAppPool -name "$using:AppPool"} -ArgumentList $AppPool -Credential $Cred
                    }
                    Catch
                    {
                        Write-Host "Error: " -NoNewline -ForegroundColor yellow
                        Write-Host $_.Exception.Message
                        Contiune
                    }
                
                    $Name = ($AppItem.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1').Trim()
                    $Status = ($AppItem.Value).trim()
    
                    # Create a custom object 
                    $APPObject = New-Object PSCustomObject
                    $APPObject | Add-Member -MemberType NoteProperty -Name "Server name" -Value $server
                    $APPObject | Add-Member Noteproperty "$($name)" -Value "$($status)"

                 }      
            }
     
        # Add custom object to our array
        $APPArray  += $APPObject
                        
        }

    # Display results
    Write-Host "`nFinal results:" -ForegroundColor Green
    $APPArray | Format-Table -AutoSize 

}

                                    

                            

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.