Options

Configure Application Pool for a Sepcific User on IIS7

Emma AEmma A Posts: 42
Script to change an application Pool to be run as a specific user

Brief notes on how to use this script:
    Add $appPoolId variable to the Deplopyment Manager project and give it the name of the application pool you want to change e.g. applicationPoolName or $(RedGateCreateWebSiteApplicationPool) if you are creating the application pool so they have the same value. Add $appPoolUsername variable which is the name and domain of the person you want to run as e.g. Domain\Username Add $appPoolPassword - the password for the username you specified above.
#variable $appPoolId - the applicationpoolIdentity you want to change
#variable $appPoolUsername - the name and domain of the person you want to run as e.g. Domain\Username
#variable $appPoolPassword - the password for the username you specified above.

# Search for Microsoft.Web.Administration.dll in file system
$directoryToSearch = $env:systemroot
Write-Host "Searching for Microsoft.Web.Administration.dll in '$directoryToSearch'..."
$webDllDirectory = Get-ChildItem -Path $directoryToSearch -Filter Microsoft.Web.Administration.dll -Recurse -ErrorAction 'silentlycontinue' `
               | where { $_.DirectoryName.Contains("SysWOW64") -or $_.DirectoryName.Contains($env:windir + "\assembly")} `
               | select -First 1

if ($webDllDirectory -eq $null)
{
   throw "Could not find 'Microsoft.Web.Administration.dll'. It is required to start/stop '$websiteToStartOrStop' website."
}

# Load the dll (console output of the command is deleted)
[System.Reflection.Assembly]::LoadFrom($webDllDirectory.DirectoryName.toString() + "\Microsoft.Web.Administration.dll") | Out-Null
$serverManager = new-object Microsoft.Web.Administration.ServerManager

# From all sites, select the site we want to start
$appPool = $serverManager.ApplicationPools[$appPoolId] 
if ($appPool -eq $null) {
   # Fail the script and exit if cannot find the applicationpool
   throw "Could not find application pool with name: $appPool"
}

#Update the application pool to run as a specific user.
$appPool.ProcessModel.IdentityType="3"; #identityType 3 is SpecificUser
$appPool.ProcessModel.UserName = $appPoolUsername;
$appPool.ProcessModel.Password = $appPoolPassword;

$serverManager.CommitChanges();

Sign In or Register to comment.