Code Maverick
Code Maverick

Reputation: 20415

How do I create an application pool with specific user in IIS 8.5 using PowerShell?

The shortened code below works, but it winds up using the ApplicationPool identity, not the specific ones that I specify in the array.

Declarations:

#App pool array positions
$Name = 0
$User = 1
$Pwdx = 2

#Application array positions
$SiteNm = 0
$PhyPath = 1
$PoolNm = 2

#App Pool Settings
$Port="80"
$HostName="*"
$Environment="local"
$Runtime="v4.0"
$Pipeline="0" #Integrated
$Identity="3" #SpecificUser

#Define the App Pools
$Pools = @(3)*2
$Pools[0] = "SomeAppPool","FooBar\SomeUser","S0meUs3rP4ssw0RD"
$Pools[1] = "AnotherAppPool","FooBar\AnotherUser","An0th3rUs3rP4ssw0RD"

Execute Code:

Import-Module WebAdministration

#navigate to the app pools root
cd IIS:\AppPools\   

foreach($p in $Pools)
{ 
    $AppPoolName = $p[$Name]
    $AppPoolUser = $p[$User]
    $AppPoolPwd = $p[$Pwdx]

    $appPool = New-Item $AppPoolName
    $appPool | Set-ItemProperty -Name managedRuntimeVersion -Value $Runtime
    $appPool | Set-ItemProperty -Name managedPipelineMode -Value $Pipeline
    $appPool | Set-ItemProperty -Name processModel.identityType -Value $Identity
    $appPool | Set-ItemProperty -Name processModel.username -Value $AppPoolUser
    $appPool | Set-ItemProperty -Name processModel.password -Value $AppPoolPwd

    Write-Host "$AppPoolName Application Pool Created..."
}

Upvotes: 3

Views: 2630

Answers (1)

Jan Chrbolka
Jan Chrbolka

Reputation: 4464

Instead of using "Set-ItemProperty", access and set the AppPool property directly:

$appPool = New-Item $AppPoolName
$appPool.managedRuntimeVersion = $Runtime
$appPool.managedPipelineMode = $Pipeline
$appPool.processModel.identityType = $Identity
$appPool.processModel.username = $AppPoolUser
$appPool.processModel.password = $AppPoolPwd
$appPool | Set-Item
$appPool.Stop()
$appPool.Start()

EDIT: Did some testing and it appears that your code works just fine, except it does not set the user name. Why?

The actual parameter is "processModel.userName" (Notice the capital 'N'). So, it looks like

$appPool | Set-ItemProperty -Name processModel.userName -Value $AppPoolUser

is case sensitive, but

$appPool.processModel.username = $AppPoolUser

is not. Got to love PowerShell.

Upvotes: 5

Related Questions