views:

3827

answers:

5

How do I create an Application Pool on IIS 6.0 using a Powershell script?

This is what I have come up with so far:

$appPool = [wmiclass] "root\MicrosoftIISv2:IIsApplicationPool"

Thanks

+1  A: 

It isn't the most obvious process, but here is what worked for me..

$AppPoolSettings = [wmiclass]'root\MicrosoftIISv2:IISApplicationPoolSetting'
$NewPool = $AppPoolSettings.CreateInstance()
$NewPool.Name = 'W3SVC/AppPools/MyAppPool'
$Result = $NewPool.Put()

You might get an error with the call to Put(), but calling it a second (or third) time should make it work. This is due to an issue with PowerShell V1 and WMI.

Steven Murawski
Thank you, works very well. I think I tried that, but was put off by that put error.
Robert Wagner
Glad it worked for you. That error is kind of distracting, but this is only V1 of PowerShell.
Steven Murawski
+3  A: 

Thought I might share the script I came up with. Thanks to goes to Steven and leon.

# Settings
$newApplication = "MaxSys.Services"
$poolUserName = "BRISBANE\svcMaxSysTest"
$poolPassword = "ThisisforT3sting"

$newVDirName = "W3SVC/1/ROOT/" + $newApplication
$newVDirPath = "C:\" + $newApplication
$newPoolName = $newApplication + "Pool"

#Switch the Website to .NET 2.0
C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -sn W3SVC/

# Create Application Pool
$appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting"
$newPool = $appPoolSettings.CreateInstance()
$newPool.Name = "W3SVC/AppPools/" + $newPoolName
$newPool.PeriodicRestartTime = 0
$newPool.IdleTimeout = 0
$newPool.MaxProcesses = 2
$newPool.WAMUsername = $poolUserName
$newPool.WAMUserPass = $poolPassword
$newPool.AppPoolIdentityType = 3
$newPool.Put()
# Do it again if it fails as there is a bug with Powershell/WMI
if (!$?) 
{
    $newPool.Put() 
}

# Create the virtual directory
mkdir $newVDirPath

$virtualDirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebVirtualDirSetting"
$newVDir = $virtualDirSettings.CreateInstance()
$newVDir.Name = $newVDirName
$newVDir.Path = $newVDirPath
$newVDir.EnableDefaultDoc = $False
$newVDir.Put()
# Do it a few times if it fails as there is a bug with Powershell/WMI
if (!$?) 
{
    $newVDir.Put() 
}

# Create the application on the virtual directory
$vdir = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IISWebVirtualDir" -filter "Name = '$newVDirName'"
$vdir.AppCreate3(2, $newPoolName)

# Updated the Friendly Name of the application
$newVDir.AppFriendlyName = $newApplication
$newVDir.Put()
Robert Wagner
@robert wagner - I would change your error handling to DO{ $newVDir.Put()} WHILE (!$?) Then you will not need to have the first Put(). The do/while loop will keep performing the put until there is no error. Just a suggestion.
Steven Murawski
It might get stuck in an infinite loop, but it's a thought.
Robert Wagner
you could always have a little counter, an only retry up to a maximum number of times.
jrista
A: 

I am unable to get the $newPool.Put() to work at all... I added the DO loop but the script crashes on the second try. Have you successfully used this script?

the error I am getting is:You cannot call a method on a null-valued expression.At :line:1 char:16+ $this.PSBase.Put <<<< ()
Try it a few more times. Perhaps it only works for me on the second try.
Robert Wagner
I get a true response for the (!$?), but it still crashes the script on the second try... Are there any innocuous settings I can pass to PutOptions to get the script to run?
Actually the $? doesn't evaluate to True until the script exits after crashing
Here is the whole script:$newApplication = "TestSite2"$poolUserName = "DEDF176\EKWeb"$poolPassword = "xxxxxxx"
$newVDirName = "W3SVC/1/ROOT/" + $newApplication$newVDirPath = "C:\" + $newApplication
$newPoolName = $newApplication + "Pool"# Create Application Pool$appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting"$newPool = $appPoolSettings.CreateInstance()$newPool.Name = "W3SVC/AppPools/" + $newPoolName$newPool.PeriodicRestartTime = 0
$newPool.IdleTimeout = 0$newPool.MaxProcesses = 2#$newPool.WAMUsername = $poolUserName
#$newPool.WAMUserPass = $poolPassword#$newPool.AppPoolIdentityType = 3DO { $newPool.Put() } WHILE (!$?)
A: 

All is well! I modified the code so there is an explicit call to the $newPool.Put() command after the initial error. Thanks for your help!

A: 

Thanks guys! Huge help.

Timmah