Privacy Info

Tuesday, October 18, 2022

PowerShell Script - Recycle Application Pool



We sometimes require to recycle the application pool of the websites, to clear cache content or to restart website. Below script comes in handy to do this task. We need to specify user credentials, server name and application pool name. PowerShell script will do the recycling task.

Script

$Username="Username"
$Password="Password"

function RecycleAppPool($Server,$AppPool)
{           
        Write-Host "ServerName:$Server ApplicationPool:$AppPool"

        # Credentials    				
	$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
				
	# 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
                Write-Host "Recycle Successful"
            }
            catch
            {
                Write-Host "Error: " -NoNewline -ForegroundColor red
                Write-Host $_.Exception.Message
                Contiune
            }
	}
		    							           
}


RecycleAppPool  "ServerName" "AppPoolName"


You can modify script based on your requirement

No comments:

Post a Comment

Sitecore PowerShell Script - Remove item from Workflow

We came across scenario where we need to remove multiple items from workflow. If items would be less then there would not be any issue but i...