Brooke Taylor
Brooke Taylor

Reputation: 141

How to create app pool and virtual directory for IIS in Powershell

I'm trying to create a powershell script for creating application pools and virtual directories using IIS 7 or 8 (not sure which one I have, I'm using Windows Server 2012 R2)

Anyway here is the code I'm using to create an app pool called .Net v2.0

Import-Module WebAdministration 
New-Item –Path IIS:\AppPools\.Net v2.0

But I'm getting an error stating

PS IIS:\Sites> Import-Module WebAdministration New-Item –Path IIS:\AppPools.Net v2.0 Undefined object type .Net. Parameter name: nodeName At line:2 char:1 + New-Item –Path IIS:\AppPools.Net v2.0 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], ArgumentException + FullyQualifiedErrorId : NewItemDynamicParametersProviderException

I feel like it might be because of the space in between Net and v2.0

Also there is no physical path for this so I'm not sure how to create a script for multiple app pools all in one, some are using v2.0 and v4.0 and some are using Integrated and Classic for the pipeline and some are using physical paths where as some don't. If someone could point me in the right direction? I'm not using websites just app pools and virtual directories.

Thanks!

EDIT I was able to fix this problem by using

[system.reflection.assembly]::Loadwithpartialname("Microsoft.Web.Administration")
$servermgr = New-Object Microsoft.web.administration.servermanager
$servermgr.ApplicationPools.Add(".NETExample")
$servermgr.CommitChanges()     

Upvotes: 3

Views: 8000

Answers (2)

David Peden
David Peden

Reputation: 18464

Here's a more modern approach using PowerShell 7+ and IISAdministration:

  1. Load the module
Import-Module "IISAdministration"
  1. Create the following function
function New-AppPool($appPoolName) {
    $serverManager = Get-IISServerManager
    $appPool = $serverManager.ApplicationPools[$appPoolName]

    if ($null -eq $appPool) {
        $appPool = $serverManager.ApplicationPools.Add($appPoolName)
    }

    $appPool.AutoStart = $true
    $appPool.ManagedPipelineMode = "Integrated"
    $appPool.ManagedRuntimeVersion = "v4.0"
    $appPool.ProcessModel.IdentityType = [Microsoft.Web.Administration.ProcessModelIdentityType]::NetworkService
    $appPool.ProcessModel.IdleTimeout = [TimeSpan]::Zero
    $serverManager.CommitChanges()

    return $appPool
}
  1. Create the app pool
New-AppPool -appPoolName $appPoolName

Upvotes: 1

imjoseangel
imjoseangel

Reputation: 3936

Run first Import-Module WebAdministration that will allow you to create the Path under IIS:\

Then:

$iisAppPoolName = ".Net v2.0"
$iisAppPoolDotNetVersion = "v2.0"
$iisAppName = "Net 2"
$directoryPath = "E:\Net2"

cd IIS:\AppPools\

New-Item $iisAppPoolName

Check this site: Create IIS App Pool

Upvotes: 1

Related Questions