In this article we are going to discuss how we can host the application as a website in IIS as well as creation of Virtual Directory under that website.
Generally we do all these activities manually, for that we need to take access of the server via RDP which is risky and not a good practice. Instead of this we can create PowerShell file (.ps1) and add all code there which will take care of all these hosting and deployment activities.
Lets see it step by step.
There is New-Website command to create the new website in IIS. Using the below parameters you can create it.
- IIS:\>New-WebSite -Name DemoSite -Port 80 -HostHeader DemoSite -PhysicalPath "$env:systemdrive\inetpub\demosite"
Now to create new Virtual Directory you need to do few more steps as we are going to point it to a folder.
We are creating "V1" folder which is going to point to virtual directory.
- # create new folder in wwwroot for Virtual Directory
- New-Item -Path "$env:systemdrive\inetpub\demosite" -Name "V1" -ItemType "directory"
- # Copy all folders and files to member folder inside wwwroot
- Copy-Item -Path "$env:systemdrive\Codepath\*" -Destination "$env:systemdrive\inetpub\demosite\V1" -Recurse
- # New Virtual Directory
- New-WebVirtualDirectory -Site "Default Web Site" -Name "VD1" -PhysicalPath "$env:systemdrive\inetpub\demosite\V1"
- # Convert it to Application
- ConvertTo-WebApplication "IIS:\Sites\Default Web Site\VD1"
- # Create NEw App Pool
- New-WebAppPool "VD1"
- $memberCredentials = "domain\username:password"
- $memberUserID, $memberPassword = $memberCredentials.Split(':')
- $app_pool_name = "VD1"
- Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.identityType -Value SpecificUser
- Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.userName -Value $memberUserID
- Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.password -Value $memberPassword
- # Set AppPool to Virtual Directory/Application
- Set-ItemProperty "IIS:\Sites\Default Web Site\VD1" -name applicationPool -value "VD1"
--Happy Coding--

Join the conversation! Your thoughts help the community grow.