使用Power Shell创建应用程序池时,将“立即启动应用程序池”设置为true,并设置用户名密码

时间:2020-05-01 09:03:43

标签: powershell iis-8

我正在通过Power Shell脚本创建应用程序池。

  1. 即使环顾四周,我也找不到如何将“立即启动应用程序池”设置为true。我怎样才能做到这一点?

  2. 此外,如果提供了用户名/密码,那么我想设置它,否则应为应用程序池标识。我做得对吗?

这是函数

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

更新1: 我的问题得到回答后,请检查github中的示例脚本。

1 个答案:

答案 0 :(得分:1)

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

  • 它立即启动应用程序池。可以从CLI和IIS管理GUI对此进行验证。
  • 如果用户/密码参数正确,因为用户存在并且密码正确,则池的标识将设置为这些参数。可以在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
相关问题