在使用 power shell 创建应用程序池时将 "Start Application Pool Immediately" 设置为 true 并设置用户名密码

Set "Start Application Pool Immediately" to true and setting username password when creating app pool with power shell

我正在通过强大的 shell 脚本创建应用程序池。

  1. 即使环顾四周,我也找不到如何将 "Start Application Pool immediately" 设置为 true。我怎样才能做到这一点?

  2. 此外,如果提供了 userName/password,那么我想设置它,否则它应该是应用程序池标识。我做得对吗?

这是函数

Function Create-AppPools($appPoolName, $appPoolDotNetVersion, $managedPipelineMode, $startMode, $userName, $password) {
    if(!$appPoolName){
        return;
    }

    Write-Host " "

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

    #check if the app pool exists
    if (!(Test-Path $appPoolName -pathType container))
    {
        Write-Host "`t Creating AppPool: " + $appPoolName
        #create the app pool
        $appPool = New-Item $appPoolName
        if($appPoolDotNetVersion){
            $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $appPoolDotNetVersion
        }
        if(@managedPipelineMode){
            $appPool | Set-ItemProperty -Name "managedPipelineMode" -Value $managedPipelineMode
        }
        if($startMode){
            $appPool | Set-ItemProperty -Name "startMode" -Value $startMode
        }
        if($userName -and $password){
            $apppool | Set-ItemProperty -Name processmodel -value @{userName = $userName;password=$password;identitytype=3}
        }
        else{
            $apppool | Set-ItemProperty -Name "ProcessModel.IdentityType" -value  3
        }
        Write-Host "`t`t AppPool: " + $appPoolName + " created successfully" -ForegroundColor Green
    }
    else{
        Write-Host "`t AppPool " + $appPoolName + " already exists" -ForegroundColor Blue
    }
}

更新一: 在我的问题得到解答后,检查 github 中的示例脚本。

您在第四个 if() 中有错字。
除此之外,您的代码按预期工作。

  • 它立即启动应用程序池。这可以从 CLI 和 IIS 管理 GUI 验证。
  • 如果 user/password 参数正确且用户存在且密码准确,则池的身份将设置为这些参数。这可以在 IIS 管理 GUI 中验证。

我建议通过 #Requires 语句指定您的代码所依赖的模块。

#Requires -RunAsAdministrator
#Requires -Modules WebAdministration

Function Create-AppPools(
    $appPoolName = "TestPool2", 
    $appPoolDotNetVersion = "v4.0", 
    $managedPipelineMode = "Integrated", 
    $startMode = "OnDemand", 
    $userName, 
    $password
) {
    if (!$appPoolName) {
        return;
    }

    Write-Host " "

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

    #check if the app pool exists
    if (!(Test-Path $appPoolName -pathType container)) {
        Write-Host "`t Creating AppPool: " + $appPoolName
        #create the app pool
        $appPool = New-Item $appPoolName
        if ($appPoolDotNetVersion) {
            $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $appPoolDotNetVersion
        }
        if ($managedPipelineMode) {
            $appPool | Set-ItemProperty -Name "managedPipelineMode" -Value $managedPipelineMode
        }
        if ($startMode) {
            $appPool | Set-ItemProperty -Name "startMode" -Value $startMode
        }
        if ($userName -and $password) {
            $apppool | Set-ItemProperty -Name processmodel -value @{userName = $userName; password = $password; identitytype = 3 }
        }
        else {
            $apppool | Set-ItemProperty -Name "ProcessModel.IdentityType" -value  3
        }
        Write-Host "`t`t AppPool: " + $appPoolName + " created successfully" -ForegroundColor Green
    }
    else {
        Write-Host "`t AppPool " + $appPoolName + " already exists" -ForegroundColor Blue
    }
}

Create-AppPools