PowerShell and IIS 7

If you were at IT Forum in Barcelona in November 2006 you will remember the key note speech in which some PowerShell cmdlets were demonstrated administering IIS 7.  Really good demo.  Sadly it doesn’t look like the cmdlets are going to make it into the final version.

A managed code (.NET) API is provided that gives access to the management functionality of IIS 7.  As it is .NET we can use this from PowerShell.  A few examples have already been published notably





These examples assume that you are working on the local machine.  This started me thinking about how I can do this remotely as I don’t want to have to keep copying the same script to multiple machines or keep logging into multiple machines.  PowerShell + remote machines means using V2.

First off install PowerShell V2 on to your IIS machine.  I’m using a Windows 2008 RC0 Web edition virtual machine.  We want to be able to load the appropriate assemblies into PowerShell on the remote machine and then run a number of commands.  This means we need to create a PowerShell runspace on the remote machine otherwise each command effectively will run in a fresh instance of PowerShell.

$rsweb1 = New-Runspace -Computername rc0web

Invoke-Expression -Runspace $rsweb1 { [system.reflection.assembly]::loadfrom("c:windowssystem32inetsrvmicrosoft.web.administration.dll") }

We then load the assembly into the runspace

Invoke-Expression -runspace $rsweb1 { $server2 = New-object microsoft.web.administration.servermanager }

We can then create the servermanager object

Invoke-Expression -runspace $rsweb1 { $server2 | get-member}
Invoke-Expression -runspace $rsweb1 { $server2 }
Invoke-Expression -runspace $rsweb1 { $server2.sites }

which will give us access to the properties and methods of that object.  I did try creating the servermanager object on the local machine but the serialisationdeserialisation that is involved in the transport means that the properties are returned but not the methods.

This gives us a method of administering IIS 7 using PowerShell in a remote scenario.   There is also an IIS 7 WMI provider which I will investigate later.  This whole idea of IIS 7 & PowerShell is getting interesting now and gives us another piece of the jigsaw for administering our environments with PowerShell which I will be talking about in February.  Looks like I’ve just found some really good demo material!!!

Technorati Tags: ,
This entry was posted in Powershell. Bookmark the permalink.

2 Responses to PowerShell and IIS 7

  1. Brandon says:

    I would be curious if you could use the .NET class for remote machine. Looking at the constructor for microsoft.web.administration.servermanager it looks like you may be able to add the config from a remote machine.More Class Info.http://msdn2.microsoft.com/en-us/library/microsoft.web.administration.servermanager.servermanager.aspx

  2. Richard says:

    It may well do.  The example I quote for Writing PowerShell cmdlets for IIS 7 appears to do that but it is using the IIS 7 WMI provider.  I looked at it and decided it was a step too far for my .NET knowledge (at the moment).  I’m still trying to test the boundaries of the PowerShell V2 remoting. 
    My take on the PowerShell & .NET scenario is to minimise the use of .NET code and try to do as much as possible with PowerShell on the principle of that is the way that most admins will approach problems.  Many admins I have spoken too have been frightened off PowerShell initially because a lot of examples that are around dive too deeply into .NET far too quickly.  Coming from a predominantly admin background (with some programming) I tend to follow the admin view point and minimise my use of .NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s