Host Website And Create Virtual Directory Using PowerShell

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.
  1. IIS:\>New-WebSite -Name DemoSite -Port 80 -HostHeader DemoSite -PhysicalPath "$env:systemdrive\inetpub\demosite"  
In the above line of code "DemoSite" is the name of the website which we are creating in IIS, Port is 80 (you can provide different as well) and Physical path is the path of code to which we are pointing the website.
 
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.
  1. # create new folder in wwwroot for Virtual Directory
  2. New-Item -Path "$env:systemdrive\inetpub\demosite" -Name "V1" -ItemType "directory"                                                           
In this step we are going to copy our code to the the folder (V1) so that we can point virtual directory to the folder
  1. # Copy all folders and files to member folder inside wwwroot   
  2. Copy-Item -Path "$env:systemdrive\Codepath\*" -Destination "$env:systemdrive\inetpub\demosite\V1" -Recurse  
Now create a new virtual directory under the "DemoSite" website which we created in IIS. Below is the code which will create it for you. We are creating virtual directory "VD1" which will getting pointed to "V1" folder inside demosite.
  1. # New Virtual Directory  
  2. New-WebVirtualDirectory -Site "Default Web Site" -Name "VD1" -PhysicalPath "$env:systemdrive\inetpub\demosite\V1"  
To access the Virtual directory as application we need to convert it to the web application otherwise it will act as a folder only. With the help of the below code we are converting it to a web application.
  1. # Convert it to Application  
  2. ConvertTo-WebApplication "IIS:\Sites\Default Web Site\VD1"  
Now the most important thing while hosting the application is apppool. We either need to attach existing apppool to the virtual directory or need to create new one. We are going to create a new one with the below code.
  1. # Create NEw App Pool  
  2. New-WebAppPool "VD1"  
Now when we are creating the app pool we need to set the identity of that app pool. We can set it with the below code.
  1. $memberCredentials = "domain\username:password" 
  2. $memberUserID, $memberPassword =  $memberCredentials.Split(':')  

  3. $app_pool_name = "VD1"  
  4. Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.identityType -Value SpecificUser   
  5.   
  6. Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.userName -Value $memberUserID  
  7.   
  8. Set-ItemProperty IIS:\AppPools\$app_pool_name -name processModel.password -Value $memberPassword  
Now we need to attach app pool to the virtual directory/web application which we created.
  1. # Set AppPool to Virtual Directory/Application  
  2. Set-ItemProperty "IIS:\Sites\Default Web Site\VD1" -name applicationPool -value "VD1"  
This way we can create website, virtual directory, app pool and set specific users to that app pool, andfinally set app pool to virtual directory/web application.
 
--Happy Coding--