Geeks With Blogs
The Quandary Phase This code was generated by a tool.

Here’s a simple code snippet which shows how to create a new app pool and site in IIS via a Powershell script. It also checks for app pool and site existence before trying to create new items. It’s quite a nice example of why Powershell is so easy to work with- yes, you can achieve the same thing in a batch file, but this is just so much cleaner:

Import-Module WebAdministration
$iisAppPoolName = "my-test-app"
$iisAppPoolDotNetVersion = "v4.0"
$iisAppName = "my-test-app.test"
$directoryPath = "D:\SomeFolder"

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

#check if the app pool exists
if (!(Test-Path $iisAppPoolName -pathType container))
{
#create the app pool
$appPool = New-Item $iisAppPoolName
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
}

#navigate to the sites root
cd IIS:\Sites\

#check if the site exists
if (Test-Path $iisAppName -pathType container)
{
return
}

#create the site
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

Posted on Sunday, February 24, 2013 1:30 AM | Back to top


Comments on this post: Create IIS App Pool and Site With Windows Powershell

# re: Create IIS App Pool and Site With Windows Powershell
Requesting Gravatar...
Do I fail or in the first "if" (#check if the app pool exists) the variable to be checked is $iisAppPoolName?
Left by qono68 on Feb 28, 2013 3:44 AM

# re: Create IIS App Pool and Site With Windows Powershell
Requesting Gravatar...
Quite right- an unfortunate bit of copy-paste- I have corrected it, thanks.
Left by booler on Mar 11, 2013 5:50 AM

Comments have been closed on this topic.
Copyright © Adam Pooler | Powered by: GeeksWithBlogs.net