Chris Crowe
Ramblings of an
IIS MVP ( MVP Since 1997 )

posts - 202, comments - 2, trackbacks - 9, articles -
1

Home :: Contact :: Syndication  :: Fish
Blog :: Rotty
Blog :: Photos ::
Login

Article Categories

Internet Information
Server

Windows Sharepoint
Services

Archives

July, 2006 (1)

June, 2006 (17)

May, 2006 (20)

April, 2006 (12)

March, 2006 (9)

February, 2006
(4)

January, 2006
(3)

December, 2005
(2)

November, 2005
(4)

October, 2005
(5)

September, 2005
(37)

August, 2005
(82)

July, 2005 (6)

Post Categories

ASP.Net

c#

c++

Excel

Exchange

Front Page 2003

FTP User Editor

HTML / CSS / DHTML

IIS

IIS - Log Parser

IIS / FTP

IIS / Tools /
Administration

IIS / Tools /
Authentication

IIS / Tools /
Compression

IIS / Tools / Crash
& Hang

IIS / Tools / ISAPI
Filters

IIS / Tools / Log
Files

IIS / Tools /
Scripts

IIS / Tools /
Security

IIS / Tools / SSL

Me (Chris Crowe)

Misc

Oulook Express

SQL Server

Visual Studio

Windows

Windows Powershell

Windows Sharepoint
Services

Wine Cellar

WMI

Blogs - IIS Team

Bill
Staples

Bobby
Videna

Brett Hill

Carlos
Aguilar Mares

Chris
Adams

David
Cox

David Wang

Kanwaljeet Singla

Rayner D'
Souza

Robert
McMurray

Suditi
Lahiri

Tobin
Titus

Blogs : IIS MVPs

Bernard Cheah

Ken Schaefer

Kristofer Gafvert

Blogs : Other Blogs

Carlos Segura
Sanz

Chris Adams (IIS
Program Manager)

IIS Geek Blog

Kent J Chen

Paul Fallon's Blog

Peter Jones (.NET MVP)

Scott Guthrie

SharePoint d'EROL

IIS Microsoft

Brett Hill (IIS
Evangelist)

David Wang

IIS Digest

IIS
Hints and Tips

IIS Support Center

IIS Technet Site

IIS Newsgroups

iis adsi admin

iis ftp

iis general

iis security

iis smtp & nntp

Web Interface to Newsgroups

IIS Tools/Docs

IIS 6 Docs ( TechNet )

IIS 6 FAQ

IIS 6 Migration Tool

IIS 6 Res Kit ( Book )

IIS 6 Res Kit ( Tools )

IIS Web Sites

Microsoft IIS Home

Microsoft
Search Engine (IIS)

http://www.iisanswers.com/

http://www.iisfaq.com/

http://www.iis-resources.com/

http://www.iisseven.com/

http://www.iistoolshed.com/

My Microsoft Certifications

Dec 15, 1998

Dec 20, 2000

Jan 31, 2001

Jul 22, 2002

Nov 1,
2004

My Microsoft MVP Awards

1997

1998

1999

2000

2001

2002

2003

2004

2005

mvp.support.microsoft.com

Some examples
of scripting of IIS using ADSI, WMI from VB and C# including the new
Microsoft.Web.Administration namespace
Posted on Tuesday, July
04, 2006 3:18 AM

Below are some simple examples of how to talk to IIS via ADSI, WMI and
the new IIS 7 only .NET Managed Provider.

The IIS ADSI Provider

The IIS ADSI provider exposes COM Automation objects that you can use
within command-line scripts, ASP pages, or custom applications to change
IIS configuration values stored in the IIS metabase. IIS ADSI objects
can be accessed and manipulated by any language that supports
automation, such as Microsoft® Visual Basic® Scripting Edition
(VBScript), Microsoft JScript®, Perl, Microsoft Active Server Pages
(ASP), Visual Basic, Java, or Microsoft C++.

The IIS WMI Provider

Windows Management Instrumentation (WMI) is a technology that allows
administrators to programmatically manage and configure the Windows
operating system and Windows applications. Beginning with IIS 6.0, IIS
includes a WMI provider that exposes programming interfaces that can be
used to query and configure the IIS metabase.

The Microsoft.Web.Administration namespace in IIS 7

This is a new .NET managed namespace that will allow you to
talk to IIS 7 and configure all aspects of the server. This API is
designed to be simple to code against in an “intellisense-driven” sort
of way. At the root level a class called ServerManager exposes all the
functionality you will need. At this time it appears that this will not
be able to be used from Windows XP but by the time Vista & Windows
Server are released this may have been ported to run on a Windows XP
client.

For more details on these technologies see the following resources:

Active Directory Service Interfaces Overview
http://www.microsoft.com/windows2000/techinfo/howitworks/activedirectory/adsilinks.asp

WMI - Windows Management Instrumentation
http://www.microsoft.com/whdc/system/pnppwr/wmi/default.mspx

Administering IIS Programmatically (IIS 6.0)
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/9041b0a5-c314-46d9-8f56-01506687f357.mspx

Pre-release documentation on
Microsoft.Web.Administration
http://windowssdk.msdn.microsoft.com/en-us/library/microsoft.web.administration.aspx

C# -Using System.DirectoryServices and ADSI

using System;
using System.Collections.Generic;
using System.Text;
// Must be specified and a reference added to System.DirectoryServices
using System.DirectoryServices;

namespace Console_EnumSites_ADSI_CSharp
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC");
foreach (DirectoryEntry site in entry.Children)
{
if (site.SchemaClassName == "IIsWebServer")
{
string ServerComment = site.Properties["ServerComment"].Value.ToString();
Console.WriteLine(ServerComment + " (" + site.Name + ")");
}
}
}
}
}

VB -Using ADSI

on error resume next

dim websrv, site
dim webinfo

set websrv = getobject("IIS://Localhost/W3SVC")
if (err <> 0) then
else
err.Clear
for each site in websrv
if (site.classname = "IIsWebServer") then
WScript.echo Site.ServerComment & " (" & Site.Name & ")"
end if
next
end if

C# -Using System.Managment and WMI

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace Console_EnumSites_WMI_CSharp
{
class Program
{
static void Main(string[] args)
{
ManagementScope oms = new ManagementScope(@"\\.\root\MicrosoftIISv2");
oms.Connect();

ObjectQuery oQuery = new ObjectQuery("select * from IISWebServerSetting");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oms, oQuery);
foreach (ManagementObject oreturn in oSearcher.Get())
{
Console.WriteLine(oreturn["ServerComment"]+" ("+oreturn["Name"]+")");
}
Console.Read();
}
}
}

C# -Using Microsoft.Web.Administration ( II7 Only )

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;

namespace Console_EnumSites
{
class Program
{
static void Main(string[] args)
{
ServerManager iisManager = new ServerManager();

Console.WriteLine("Display IIS Sites in IIS 7");
Console.WriteLine("".PadLeft(40,'-'));
foreach(Site site in iisManager.Sites)
{
Console.WriteLine("{0} {1}", site.Id.ToString().PadRight(12), site.Name);
}
Console.Read();
}
}
}

Post Comment

Title

Please enter a
title

Name

Please enter your
name

Url

Comment  Please enter a
comment

Remember Me