Automating IIS with PowerShell

Generic cmdlets like Select-Object and Get-Item are part of the standard PowerShell interface. Any module which includes a provider hierarchy must therefore support them. Once you are within the IIS hierarchy, you can then use low-level configuration cmdlets to manage specific IIS elements without having to use extended path names.

Using Task-Oriented Cmdlets

As well as the low-level configuration cmdlets, the IIS  PowerShell snap-in ships with a large number of cmdlets which  simplify common IIS maintenance tasks like creating, starting, stopping   and removing specific IIS elements. One set of these task-oriented cmdlets for  managing IIS sites, is as below:

  • Get-Website
  • New-Website
  • Start-Website
  • Stop-Website
  • Remove-Website

Unlike  low-level cmdlets  task-oriented cmdlets do not rely on the IIS namespace , and these cdmlets use static parameters to configure specific properties. For example, to create a  web site, you could use a command such as below:

New-Website –Name NewSite –Port 80 –HostHeader intra.example.local –PhysicalPath
“$env:systemdrive\inetpub\newsite” -Ssl

The above command creates a new site with the name NewSite, as uses the default port 80, and  the host header value intra.example.local to differentiate this site from any other sites that may use the same address and port 80. The  site will use the content files located in the \inetpub\newsite directory  and   will allow users to connect using SSL encryption by using the HTTPS: prefix in the URL.

Once  the site has been created, you can  use the  PowerShell interface to create new content. Switch to the IIS hierarchy with the command cd\sites\NewSite, and then use the below command to open a Notepad window containing a newly created default.html file:

notepad “$(Get–WebFilePath .)\default.html”

Enter some HTML and this can then be the default page for your site (depending on the IIS setting for default home page).

Using Low-Level Configuration Cmdlets

IIS 7.0, which was released in Windows Server 2008, marked a complete revision of the IIS architecture, with extensibility being a major part of the revision. The new architecture, carried over to IIS 7.5 in Windows Server 2008 R2, is now schema driven using XML configuration files. This new extensibility complicates developing a PowerShell management strategy, however. Cmdlets may have static parameters which allow them to manage the specific properties of an element, but if another developer creates an IIS extension that adds additional properties to the element, the existing cmdlets will not be able to manage the new properties.

Thus, the IIS PowerShell snap-in includes low-level configuration cmdlets that allows you to view and manage all the numerous IIS configuration settings, including any custom settings. One set of these low-level cmdlets, focusing on IIS configuration elements, is as below:

  • Add-WebConfiguration Adds a collection element to the existing IIS configuration
  • Clear-WebConfiguration Removes configuration settings from a specified location
  • Backup-WebConfiguration Creates a backup of the IIS configuration
  • Get-WebConfiguration Gets an IIS configuration element for a specified location
  • Restore-WebConfiguration Restores IIS configuration elements from an existing  backup
  • Set-WebConfiguration Sets the  IIS configuration element to a specified value
  • Select-WebConfiguration Returns the Web configuration objects

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>