############################################################################## ## This script searches for all Web Applications using a specific application pool 'Old Application Pool' and updates it's property in SharePoint configuration to use a 'New Application Pool' ## USAGE: ## REQUIREMENT: Ensure the new application pool to be used is already registered in SharePoint. This happens when you create a new Web Application with a new Application pool. ## SYNTAX: ChangeApplicationPool " ## NOTE: Application pool name is case sensitive. ############################################################################## # Define 2 parameters for this script param([string] $oldAPName, [string] $newAPName) # Check if both params are passed, else show Syntax. if ($newAPName -eq "") { "Syntax: ChangeApplicationPool " exit; } [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") $WebService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService ## Get reference for the new application pool within the list of SharePoint objects [Microsoft.SharePoint.Administration.SPApplicationPoolCollection] $appPools = $webService.ApplicationPools; $newAppPool = $NULL; foreach ($appPool in $appPools) { if ($appPool.Name -eq $newAPName) { [Microsoft.SharePoint.Administration.SPApplicationPool]$newAppPool = $appPool; } } ## If new application pool is not found, then give error and exit if($newAppPool -eq $NULL) { "ERROR: The New application pool to be assigned was not found within SharePoint!" "REQUIREMENT: Ensure the new application pool to be used is already registered in SharePoint. This happens when you create a new Web Application with a new Application pool." exit; } ## Now loop through all Web Applications to find a match of the old Application pool and update it with New App pool. [Microsoft.SharePoint.Administration.SPWebApplicationCollection] $webApps = $webService.WebApplications "Searching through all Web Applications for Old Application pool - " + $oldAPName + " and changing it to - " + $newAPName $count=0; foreach ($webApp in $webApps) { $webApp.DisplayName + " uses application pool " + $webApp.ApplicationPool.Name if($webApp.ApplicationPool.Name -eq $oldAPName) { $webApp.ApplicationPool = $newAppPool; ## Changed to new Application pool reference. $webApp.Update(); $webApp.ProvisionGlobally(); "............" + $webApp.DisplayName + " now uses the new application pool " + $webApp.ApplicationPool.Name $count = $count + 1; } } "Found and replaced " + $count + " web application(s)" if($count -eq 0) { "Syntax: ChangeApplicationPool " "Ensure the application pool name is an exact match" } ########################################################################################################################