Red Gate forums :: View topic - Configure Application Pool for a Sepcific User on IIS7
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

Deployment Manager PowerShell Scripts forum

Configure Application Pool for a Sepcific User on IIS7

Search in Deployment Manager PowerShell Scripts forum
Post new topic   Reply to topic
Jump to:  
Author Message
Emma A



Joined: 30 Mar 2009
Posts: 40

PostPosted: Thu Sep 19, 2013 1:12 pm    Post subject: Configure Application Pool for a Sepcific User on IIS7 Reply with quote

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.


Code:

#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();

Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group