tags:

views:

471

answers:

3

Hey all,

I am trying to connect to IIS programmatically. I find there are a ton of examples online, but I can't seem to get any to work and have tried quite a few variations

Every time I try the following code the object that is returned has this error for each property: ..."threw an exception of type 'System.Runtime.InteropServices.COMException'"

      using System.DirectoryServices;

  String serverName = "serverName";

  DirectoryEntry IIS = new DirectoryEntry("IIS://" + serverName + "/W3SVC");
  IIS = new DirectoryEntry("IIS://" + serverName + "/W3SVC", "administrator", "mypassword");
  IIS = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT", "administrator", "mypassword");

I am using Windows Directory user accounts and I have a bunch of sites running on IIS. I am trying this code on a windows xp development machine trying to connect to a windows 2008 Server with IIS 7. Anyone know what I am doing wrong?

A: 

Your account may not have launch permissions on the COM object wrapping the IIS calls. You may need to try adding yourself to the admin group on the box hosting IIS to get this to work.

Gurdas Nijor
As you can see from the code above I am using the Windows administrator account in 2 of my connection attempts. My personal account has admin rights as well. Is this what you are referring to or are you suggesting something else?
Mario
A: 

Make sure you have the IIS6 management compatibility feature installed on the target server- you can't do remote management via ADSI on IIS7 without it.

nitzmahone
That is already installed.
Mario
A: 

Make sure that IIS is installed on your client machine - your program will throw a System.Runtime.InteropServices.COMException if it isn't installed.

This counts when you are looking at IIS on a remote machine too, the machine running your app will need IIS too.

EDIT: Also, I've recently discovered an assembly specifically for connecting to and configuring IIS7 - Microsoft.Web.Administration. Might be worth looking at whether you have access to this (or can get access, it should be on the machine with IIS7 in any case) and see what it can do. I'm afraid I've not used it myself, so I can't tell you if it'll do what you want, but it's another option to look into.

Finally, there's the option of System.Management and WMI scripts.

Dim scope As New Management.ManagementScope("\\" & server & "\root\MicrosoftIISv2")
scope.Connect()

Dim query As New Management.ObjectQuery("select * from IISWebVirtualDirSetting")
Dim searcher As New Management.ManagementObjectSearcher(scope, query)
For Each obj As Management.ManagementObject In searcher.Get()
    DoSomethingWith(obj)
Next

The list of properties on obj is at http://msdn.microsoft.com/en-us/library/ms525005.aspx, there's also some more different queries you can run - just dig around on MSDN for more.

pete the pagan-gerbil