如何使用 PowerShell 在 IIS 8.5 中创建具有特定用户的应用程序池?

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

下面的缩短代码有效,但它最终使用了 ApplicationPool 标识,而不是我在数组中指定的特定标识。

声明:

#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"

执行代码:

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..."
}

不使用 "Set-ItemProperty",而是直接访问和设置 AppPool 属性:

$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()

编辑:进行了一些测试,看起来您的代码工作正常,只是它没有设置用户名。为什么?

实参为"processModel.userName"(注意大写'N')。 所以,它看起来像

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

区分大小写,但

$appPool.processModel.username = $AppPoolUser

不是。爱上了 PowerShell。