Some examples of scripting of IIS using ADSI, WMI from VB and C# including the new Microsoft.Web.Administration namespace

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:


 

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();  
         }  
     }  
 } 

发表评论