Saturday, July 2, 2016

Creating Web Applications in IIS using PowerShell

I am a big fan of PowerShell and have been looking for ways to script out common IIS Administration tasks using PowerShell and there have been a number of methods provided in the past that made it reasonably difficult to create Web Applications in IIS using PowerShell.

However, I came across an article recently that makes this task a piece of cake!  https://technet.microsoft.com/en-us/library/hh867877(v=wps.630).aspx

This resulted in me creating the following PowerShell script:


Param
(
[Parameter(Mandatory=$true)]
$AppPoolName = "DefaultAppPool",
[Parameter(Mandatory=$true)]
$WebAppName,
[Parameter(Mandatory=$true)]
$WebsiteName = "Default Web Site",
[Parameter(Mandatory=$true)]
$PhysicalPath
)
Clear-Host
#create the Physical Path if it does not already exist
$DirExists = Test-Path $PhysicalPath
if ($DirExists -ne $true)
{
New-Item -Path $PhysicalPath -ItemType Directory
}#if
#Create the new Web Application
New-WebApplication -Name $WebAppName -Site $WebsiteName -PhysicalPath $PhysicalPath -ApplicationPool $AppPoolName

This worked like a charm! Now I can easily automate nearly any IIS Administration task! Woo hoo!

No comments:

Post a Comment