powershell scripts to attched apppool to existing site

$config = Get-Content Set-DefaultApplicationPoolConfig.xml
$defaultAppPoolName = $config.WebApplications.defaultAppPoolName
 
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") 
 
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService 
 
Write-Host "Checking presence default application pool $defaultAppPoolName..." -NoNewline
 
[Microsoft.SharePoint.Administration.SPApplicationPool] $newAppPool = $service.ApplicationPools[$defaultAppPoolName] 
 
## The Default Application Pool MUST exist!
if($newAppPool -eq $NULL) 
{ 
    Write-Host ""
    Write-Host -ForegroundColor Red "The default application pool '$defaultAppPoolName' does not exist." 
    Write-Host -ForegroundColor Red "Please ensure the application pool '$defaultAppPoolName' is already registered in SharePoint." 
    exit
}
else
{
    Write-Host -ForegroundColor Green "[OK]"
}
 
foreach( $webAppConfig in $config.WebApplications.WebApplication )
{
    $url = $webAppConfig.url
    Write-Host "Updating $url..." -NoNewline 
 
    $webApp = Get-SPWebApplication $url -ErrorAction SilentlyContinue
     
    if( $webApp -eq $null )
    {
        Write-Host -ForegroundColor Yellow " [Web application does not exist]"
        continue
    }
 
    $currentAppPool = $webApp.ApplicationPool
    if($currentAppPool.Name -eq $defaultAppPoolName) 
    { 
        Write-Host -ForegroundColor Green " [Change not needed]"
    }
    else
    {
        $webApp.ApplicationPool = $newAppPool
        $webApp.Update()
        $webApp.ProvisionGlobally()
        Write-Host -ForegroundColor Green " [Done]"
    }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source