Search code examples
powershelliis-8

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


I am creating app pools via power shell script.

  1. Even after looking around i couldn't find how to set the "Start Application Pool immediately" to true. How can i do this?

  2. Also, if the userName/password are provided then i want to set it otherwise it should be Application Pool Identity. Am i doing it correctly?

Here is the function

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

Update 1: Check github for sample scripts after i got my question answered.


Solution

  • You had a typo in your fourth if().
    Other than that your code works as expected.

    • It starts the application pool immediately. This can be verified from the CLI and IIS Management GUI.
    • If the user/password params are correct in that the user exists and the password is accurate, the pool's identity will be set to those params. This can be verified in the IIS Management GUI.

    I'd recommend specifying the modules your code depends on via a #Requires statement.

    #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