Wednesday, July 22, 2015

How to Create Web site and Application pool in IIS using PowerShell scripting?

In IIS Web Site/Application Pool creation task automation and configuration using Powershell scripting:

You can easily create Web application (web site) and new application pool in IIS with the use of PowerShell scripting. Here with I am sharing the code for your reference.

Before creating the application pool you need to verify the app pool name in the IIS. The App Pool name not available in the Application pool means you need to create new App pool otherwise no need to create. For that the below code will help you to check app pool name exist or not using the PowerShell scripting.

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

                #check if the app pool exists
                if (!(Test-Path $iisAppPoolName -pathType container))
                {
                        .....
                 }



The same way before creating any Application (web site) name in the IIS you need to check if already exist or not. Please find the below code for that.

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

                #check if the web site exists
                if (Test-Path $iisAppName -pathType container)
                {
                #New-WebApplication -Name $iisAppName -Site 'Default Web Site' -PhysicalPath $directoryPath -ApplicationPool $iisAppPoolName
                }



The below completed code will run in the Try Catch block in PowerShell for preventing the error message and catch the error messages. First I have checked the App pool name and if not exist I have created the new Application Pool name and set identity values.

Sample Code: 

#cls
echo 'Web Site Configuration Started...'

Import-Module WebAdministration
try
{
                $iisAppPoolName = "MyTestAppPool"
                $iisAppPoolDotNetVersion = "v2.0"
                $iisAppName = "MyReport"
                $UserName=""
                $Password=""              
                $identitytype=2                           #A4Lidentitytype(0=LocalService,1=LocalSystem,2=NetworkService,3=ApplicationPoolIdentity)



                #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
                             
                                #Configure Application Pool Identity to particular the username and password
                                if($UserName)
                                {
                                                Set-ItemProperty IIS:\AppPools\$iisAppPoolName -name processModel -value @{userName=$UserName;password=$Password;} #identitytype=3
                                }
                                else
                                {
#Configure Application Pool Identity
                                                                Set-ItemProperty IIS:\AppPools\$iisAppPoolName -name processModel -value @{identitytype=$identitytype}
                                }
                                write-host $iisAppPoolName " AppPool Created Successfully." -foregroundcolor green
                }

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

                #check if the web site exists
                if (Test-Path $iisAppName -pathType container)
                {
                #New-WebApplication -Name $iisAppName -Site 'Default Web Site' -PhysicalPath $directoryPath -ApplicationPool $iisAppPoolName
                }
}
catch [Exception]
{
    "Application failed:`n`n " + $_.Exception
}
finally
{
cd c:
}



The above code will check in the application pool the required app pool name available or not. If not available then it will create new one with user identity and verify the web site name.

No comments: