Automating IIS with PowerShell

As with most areas of Windows Server 2008 and 2008 R2 , Microsoft is emphasizing PowerShell as an important tool for managing IIS 7 and IIS 7.5. The IIS PowerShell snap-in provides many new cmdlets and enables admins to manage IIS properties in numerous different ways.

Select Windows PowerShell Modules from the Administrative Tools group and the system will load the modules included with Windows Server 2008 , including the WebAdministration module which provides the IIS functionality. You may also import the module manually from the Windows PowerShell prompt using the below command:

Import-Module WebAdministration

Once the IIS PowerShell snap-in is running, you can display all the cmdlets it contains using the below command:

Get-Command –pssnapin WebAdministration

The IIS PowerShell snap-in uses three types of cmdlets:

  • PowerShell provider cmdlets
  • Task-oriented cmdlets
  • Low-level configuration cmdlets

These cmdlet types relate to the three different methods of managing IIS from the   PowerShell prompt.

Using the IIS PowerShell Provider

The IIS PowerShell provider creates a hierarchical IIS namespace which admins can navigate similar to  a  standard directory structure. Type iis: and press Enter at the PowerShell prompt (with the WebAdministration module having been already imported) and the prompt changes to PS IIS:>  then typing the dir command displays, but the top level of the IIS namespace (not the file system) as below:

Name
----
AppPools
Sites
SslBindings

After moving to the Sites directory using  the cd Sites command, the dir command displays a list of the IIS sites on the server.

The Get-Item cmdlet allows you to show selected sites in the same format. By piping results of the Get-Item cmdlet to the Select-Object cmdlet, you can see all properties of a selected site.

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
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!