Monday, March 9, 2015

Using the IIS Configuration Editor to Generate Scripts

Many people who manage IIS do not know that you can easily figure out how to automate IIS operations using the IIS Configuration Editor which is available in IIS 7.5 and above.

Unfortunately, there are not a lot of articles on the Internet which provide much guidance on how to navigate and perform common operations in IIS using the Configuration Editor.

This article provides some insight into using the Configuration Editor, but does not provide you with performing common operations such as creating Application Pools, Sites and Applications.

This article provides some information about how to create Application Pools, but includes no screenshots so you are a bit lost about how to perform other operations using the IIS Configuration Editor:

Therefore, here are the detailed steps on how to accomplish this:

  1. Click on the Root Server Name in IIS Manager (the name of your Windows Server)
  2. In Features view, click on the Configuration Editor icon
  3. Click on the Section dropdownlist to select the section  you wish to edit such as applicationPools or sites
  4. If you select sites, you can then view the individual Web Sites and drill into them further to get into the Applications
  5. When you are ready to add a new Application, click on the Add button in the right hand pane
  6. Enter all of the details for your Application such as the Name, ApplicationPool etc.
  7. You will have to go into the Virtual Directory/Application level and specify a Physical Path for the Application.  NOTE: If you forget to do this, the Application will not appear in IIS.
  8. Close out of all of the dialogs you have edited except the main IIS Management Window
  9. In the right hand pane, select "Generate Script" 
  10. This will open a dialog allowing you to select which type of script you want to generate such as C#, PowerShell, JavaScript or Command Line (using AppCmd)
  11. You can then copy and paste this script to the editor of your choice and run it!
  12. Best of all, you can edit this script to your heart's content to customize it for your automation needs in the future!  Awesome!!

Below is a sample PowerShell script that can be used to create an ApplicationPool and Application:

$AppPoolName = "MyAppPool";
$WebSiteName = "MySite";
$ApplicationSiteName = "/MyApplication";
$ApplicationSiteNamePhysicalPath = "E:\Websites\MySite";
$RuntimeVersion = "v4.0";
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools" -name "." -value @{name=$AppPoolName;managedRuntimeVersion=$RuntimeVersion};
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='$WebSiteName']" -name "." -value @{path=$ApplicationSiteName;applicationPool=$AppPoolName};
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='$WebSiteName']/application[@path='$ApplicationSiteName']" -name "." -value @{path='/';physicalPath=$ApplicationSiteNamePhysicalPath};

No comments:

Post a Comment