IIS Web Deployment using PowerShell

It's been a while I posted something on my blog, so thought of writing about IIS web deployments. I've been working with IIS on Windows for a long time and I have several products that go way back that run on IIS. Apparently many people are unaware that in recent versions of Windows - using Powershell - you can automate most of your IIS operations using a few simple Powershell Commandlet calls. It's as easy as creating a small PowerShell script file.

In this article, I am just giving you some tips about how you can make use of Powershell to automate your manual IIS operations. You can make use of WebAdministration PowerShell module to configure IIS via PowerShell scripts.


        Import-Module WebAdministration
    


Once WebAdministration module is installed it's easy to create new WebSites and Application Pools.

To create an Application Pool you can use the below command


            New-WebAppPool -Name "WebSiteApplicationPoolName"
        


if you want to check whether an application pool exists, you can even check that and remove the existing one and you can create a new one


        $iisWebappName="TestSite"
        if (Test-Path IIS:\AppPools\$iisWebappName) {
            Remove-WebAppPool -Name $iisWebappName
        }
        New-WebAppPool -Name $iisWebappName
        


if you want to create a WebSite


        # See if a website with this name already exists
        if (!(Get-Website -Name $iisWebsiteName)){

        #Create a new Website
        New-WebSite -Name $iisWebsiteName 
                    -Port 80 
                    -IPAddress * 
                    -HostHeader $websiteUrl 
                    -PhysicalPath $physicalPath
                    -ApplicationPool $iisWebsiteName
        Set-ItemProperty "IIS:\Sites\$iisWebsiteName" 
                    -Name Bindings 
                    -value @{protocol="http";bindingInformation="*:80:$websiteUrl"}
        }
        


Or sometimes you might need to create Web Application under Default Web Site,


        # See if a webapp with this name already exists
        if (!(Get-WebApplication -Site "Default Web Site" -Name $iisWebappName)) {
        #Create a new Webapp under Default Web Site
            New-WebApplication -Name $iisWebappName 
                               -Site 'Default Web Site' 
                               -PhysicalPath $physicalPath 
                               -ApplicationPool $iisWebappName
        }
        

if you want to Set the App Pool to the 4.0 runtime


        # Set the App Pool to the 4.0 runtime
        Set-ItemProperty IIS:\AppPools\$iisWebsiteName -Name managedRuntimeVersion -Value "v4.0"
        


likewise, we can automate a lot of operations using PowerShell and it's a one-time activity to create a script to do all these operations. The complete sample script is available in Github https://github.com/nidps/IIS-Deployment-using-Powershell

Comments

Popular Posts