DirectoryServices 로 IIS 관리하기

김경민·2022년 7월 7일
1

Cloud Native, DevOps

목록 보기
5/10
post-thumbnail

Creating Application Pools Using System.DirectoryServices

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
  class Program
  {
    static void Main(string[] args)
    {


...


CreateAppPool("IIS://Localhost/W3SVC/AppPools", "MyAppPool");


...


CreateVDir("IIS://Localhost/W3SVC/1/Root", "MyVDir", "D:\\Inetpub\\Wwwroot");


...


AssignVDirToAppPool("IIS://Localhost/W3SVC/1/Root/MyVDir", "MyAppPool");


...


}


...


static void CreateAppPool(string metabasePath, string appPoolName)
{
  //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
  //    for example "IIS://localhost/W3SVC/AppPools" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);

  try
  {
    if (metabasePath.EndsWith("/W3SVC/AppPools"))
    {
      DirectoryEntry newpool;
      DirectoryEntry apppools = new DirectoryEntry(metabasePath);
      newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
      newpool.CommitChanges();
      Console.WriteLine(" Done.");
    }
    else
      Console.WriteLine(" Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.");
  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
  }
}


...


static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
  //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
  //    for example "IIS://localhost/W3SVC/1/Root" 
  //  vDirName is of the form "<name>", for example, "MyNewVDir"
  //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
  Console.WriteLine("\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:",
      metabasePath, vDirName, physicalPath);

  try
  {
    DirectoryEntry site = new DirectoryEntry(metabasePath);
    string className = site.SchemaClassName.ToString();
    if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
    {
      DirectoryEntries vdirs = site.Children;
      DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
      newVDir.Properties["Path"][0] = physicalPath;
      newVDir.Properties["AccessScript"][0] = true;
      // These properties are necessary for an application to be created.
      newVDir.Properties["AppFriendlyName"][0] = vDirName;
      newVDir.Properties["AppIsolated"][0] = "1";
      newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));

      newVDir.CommitChanges();

      Console.WriteLine(" Done.");
    }
    else
      Console.WriteLine(" Failed. A virtual directory can only be created in a site or virtual directory node.");
  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in CreateVDir with the following exception: \n{0}", ex.Message);
  }
}


...


static void AssignVDirToAppPool(string metabasePath, string appPoolName)
{
  //  metabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
  //    for example "IIS://localhost/W3SVC/1/Root/MyVDir" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  Console.WriteLine("\nAssigning application {0} to the application pool named {1}:", metabasePath, appPoolName);

  try
  {
    DirectoryEntry vDir = new DirectoryEntry(metabasePath);
    string className = vDir.SchemaClassName.ToString();
    if (className.EndsWith("VirtualDir"))
    {
      object[] param = { 0, appPoolName, true };
      vDir.Invoke("AppCreate3", param);
      vDir.Properties["AppIsolated"][0] = "2";
      Console.WriteLine(" Done.");
    }
    else
      Console.WriteLine(" Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools");
  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in AssignVDirToAppPool with the following exception: \n{0}", ex.Message);
  }
}


...


  }
}

0개의 댓글