Tuesday, May 26, 2009

IIS Administration using ADSI and .NET DirectoryServices

The System.DirectoryServices namespace provides access to the Active Directory. These classes make it possible to do the following Web-site administration tasks programmatically:


• Verifying that a virtual directory exists
• Creating and deleting virtual directories
• Setting and updating any of the properties on IIS
• Invoking any of the IIS-specific methods

Getting Virtual Directories


public StringCollection GetIISVirtualDirectories(string provider)
{
provider = "IIS://192.168.1.150/W3SVC/1/Root";
DirectoryEntries dirEntries = null;
try
{
dirEntry = new DirectoryEntry(provider);
dirEntry.RefreshCache();
dirEntries = dirEntry.Children;
strCollection = new StringCollection();
foreach(DirectoryEntry objDir in dirEntries)
{
strCollection.Add(objDir.Name);
}
dirEntry.Dispose();
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error getting virtual director: " + ex);
return null;
}
return strCollection;
}





Metabase
In physical terms, the metabase is a combination of the MetaBase.xml and MBSchema.xml files and the in-memory metabase. The IIS configuration information is stored in the MetaBase.xml file, while the metabase schema is stored in the MBSchema.xml file. When IIS is started, these files are read by the storage layer and then written to the in-memory metabase through Admin Base Objects (ABO). All configuration settings that are made using the resources in this list are written to the in-memory metabase through Admin Base Objects (ABOs). The in-memory metabase is a copy of the MetaBase.xml and MBSchema.xml files in the IIS file cache.

IIS ADSI Object Hierarchy

The IIS ADSI objects are organized in a hierarchical structure that mirrors the structures defined in the IIS metabase configuration file. Objects contain other objects to create the object structure. Most objects inherit characteristics or properties from the parent object, but child objects can contain unique characteristics implemented by extending the schema. You can use this object hierarchy to access configuration settings for specific IIS elements.

Creating Virtual Directories



public int createVirtualDirectory(string ipaddress,string strRootPath, string schema , string sitename , string path)
{
int success = 0;
DirectoryEntry dirRoot = new DirectoryEntry("IIS://" + ipaddress + strRootPath.Trim());
//Check whether the directory already exists on the server
if(DirectoryEntry.Exists("IIS://" + ipaddress + strRootPath + "/" +sitename))
return 0;//Return 0 to display error message
try
{
//If not exists, add a new virtual directoty
dirRoot.RefreshCache();
DirectoryEntry dirNew = dirRoot.Children.Add (sitename,schema);
dirNew.Properties["Path"].Insert(0,path);
dirNew.CommitChanges();
dirRoot.CommitChanges();
//If it is web virtaul directory
if(schema == "IIsWebVirtualDir")
{
//dirNew.Invoke("AppCreate",true);
dirNew.Invoke("AppCreate",new object[]{true});
dirNew.CommitChanges();
dirRoot.CommitChanges();
dirNew.Close();
dirRoot.Close();
}
success = 1;
}
catch
{
throw new IISPropertiesException("Unable to create the virtual directory");
}
return success;
}


public class IISPropertiesException : Exception
{
///
/// Constructs a new IISPropertiesException class
///

/// Speicifes the error description
public IISPropertiesException(string description): base(description){}
public override string ToString()
{
return "IISProperties Exception : " + Message + " " + StackTrace;
}
}






Delete a virtaul directory

public void deleteVirtualDirectory(string ipaddress, string provider, string sitename, string schema)
{
try
{
DirectoryEntry delDir = new DirectoryEntry("IIS://" + ipaddress + provider);
DirectoryEntry removeDir = new DirectoryEntry("IIS://" + ipaddress + provider +"/"+ sitename);
delDir.RefreshCache();
delDir.Children.Remove(removeDir);
delDir.CommitChanges();
delDir.Close();

}
catch(Exception e)
{
//throw new IISPropertiesException("Unable to delete the virtual directory ");
System.Windows.Forms.MessageBox.Show("Error deleting virtual directory : " + e);
}
}

No comments: