Setting up IIS ASP.NET WebApplication using PowerShell

Setup ASP.NET web application hosted on IIS using PowerShell

PowerShell is the best option for automation on Windows platform. Regardless of which platform and how you organize Continous Deployment, you will always need to introduce certain level of customization during the deployment process.

Scripting with PowerShell can play main role with these kind of customizations. To setup an IIS ASP.NET WebApplication manually the following steps need to be completed:

  • Create Application Pool
  • Pick The proper .NET Framework version for the Application Pool
  • Create the new website on IIS with specific name
  • Point to physical location of the web application
  • Assign the domain and port bindings

In order to setup IIS ASP.NET web application, PowerShell script needs to do the same things from the list.

This is the PowerShell script that does all the steps mentioned above and sets up ASP.NET web application on local IIS

Import-Module WebAdministration

$iisAppPoolName = "temp"
$iisAppPoolDotNetVersion = "v4.0"

$iisWebsiteFolderPath = "C:\temp"
$iisWebsiteName = "temp"

$iisWebsiteBindings = @(
   @{protocol="http";bindingInformation="*:80:temp1.com"},
   @{protocol="http";bindingInformation="*:80:temp2.com"}
)

if (!(Test-Path IIS:\AppPools\$iisAppPoolName -pathType container))
{
New-Item IIS:\AppPools\$iisAppPoolName
Set-ItemProperty IIS:\AppPools\$iisAppPoolName -name "managedRuntimeVersion" -value $iisAppPoolDotNetVersion
}

if (!(Test-Path IIS:\Sites\$iisWebsiteName -pathType container))
{
New-Item IIS:\Sites\$iisWebsiteName -bindings $iisWebsiteBindings -physicalPath $iisWebsiteFolderPath
Set-ItemProperty IIS:\Sites\$iisWebsiteName -name applicationPool -value $iisAppPoolName
}


    

Additional web application might need to have some additional permissions for accessing specific locations on the file system for operations such as writing logs or specific configurations or to store uploaded documents.

This Powershell script does not do this, but you can combine web app creating script with this PowerShell script to set permissions to folders.

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

Comments for this article