设为首页 收藏本站
查看: 542|回复: 0

[经验分享] Get IIS Application Id & name

[复制链接]

尚未签到

发表于 2015-8-14 12:19:56 | 显示全部楼层 |阅读模式
  In this case it’s using IIS 7 so the .NET version isn’t selectable as it’s selected as part of the Application
Pool. For IIS 6 both .NET version and App Pool are available for IIS 5 only the .NET version is available.
So how do you get the ApplicationPools available, select and set one and create a new one? There are actually a number of ways (especially with IIS7) but the most widely supported by recent versions of IIS is still by using ADSI and DirectoryServices in
  .NET.

  I talked about using ADSI for IIS with .NET a long while back with some examples of how to get virtuals and set properties etc. there.
So, here’s some additional code to deal with Application Pools in my WebConfiguration class (note there are a few depencies in this code, but you should be able to glean the general
  idea):

  /// <summary>
/// Returns a list of all
   the Application Pools configured
/// </summary>
  /// <returns></returns>
public ApplicationPool[] GetApplicationPools()
  {           

    if (ServerType != WebServerTypes.IIS6 &&
   ServerType != WebServerTypes.IIS7)
      return null;
  
    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName
   + "/W3SVC/AppPools");

      if (root == null)         

          return null;
    List<ApplicationPool> Pools = new List<ApplicationPool>();
  foreach (DirectoryEntry Entry in root.Children)
    {    PropertyCollection Properties = Entry.Properties;      

     ApplicationPool Pool = new ApplicationPool();     

      Pool.Name = Entry.Name;         
    Pools.Add(Pool);
      }
    return Pools.ToArray();
  }
  ///<summary>

  /// Create a new Application Pool and return an instance of the entry
/// </summary>
  /// <param name="AppPoolName"></param>
/// <returns></returns>
  public DirectoryEntry CreateApplicationPool(string AppPoolName)
{
      if (this.ServerType != WebServerTypes.IIS6 &&      

   this.ServerType != WebServerTypes.IIS7)      

   return null;

      DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName
+ "/W3SVC/AppPools");
    if (root == null)
       return null;
    DirectoryEntry AppPool = root.Invoke("Create","IIsApplicationPool",AppPoolName) as DirectoryEntry;               
  
     AppPool.CommitChanges();  

     return AppPool;

  }
  

  /// <summary>
/// Returns an instance of an Application Pool
/// </summary>
  /// <param name="AppPoolName"></param>
/// <returns></returns>
  public DirectoryEntry GetApplicationPool(string AppPoolName)
{
      DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName);
    return root;
}
  
  /// <summary>
/// Retrieves an Adsi Node by its path. Abstracted for error handling/// </summary>
  /// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param>
/// <returns>node or null</returns>
  private DirectoryEntry GetDirectoryEntry(string Path)
{
      DirectoryEntry root = null;

      try
    {
           root = new DirectoryEntry(Path);

      }
    catch
      {
  this.SetError("Couldn't access node"); return null;
      }

      if (root == null)
    {
   this.SetError("Couldn't access node");

   return null;
    }
      return root;
}
  

AppPools are stored under:
  

  IIS://localhost/W3SVC/AppPools

  And you can access a specific pool through the Children collection. For ADSI paths a child looks like this:
IIS://localhost/W3SVC/AppPools/DefaultAppPool
  From there you get a DirectoryEntry object and you can fire away on the properties of the pool and set thing like the impersonating account and various health checks. I didn&#8217;t need to
look closely at this but I couldn&#8217;t find the MSDN documentation on IIsApplicationPool that shows the member properties. The MSDN documentation for IIS&#8217;s ADSI support (and even the IIS6 WMI support) is
just plain awful and scattered through many places. If anybody happens to find a link with the member properties post it here.

  Once you have an Application Pool it can be attached to a virtual directory/Application via its AppPoolId:
DirectoryEntry VDir = new DirectoryEntry("IIS://localhost/W3SVC/ROOT/WebStore");
VDir.Properties["AppPoolId"].Value = this.ApplicationPool;
  VDir.CommitChanges();

And there you have it. I&#8217;ve updated the Web Configuration Utility online and it includes the AppPool configuration code, and a recent update to the West Wind Web Store also includes this code as part of the configuration.  


using System.DirectoryServices;
using System;
public class IISAdmin
{
   public static void GetWebsiteID(string websiteName)
   {
      DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
     foreach(DirectoryEntry de in w3svc.Children)
     {
        if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
        {
           Console.Write(de.Name);
        }
     }
  }
  public static void Main()
  {
     GetWebsiteID("Default Web Site");
  }

-----------------------------------------------------------

  You are looking for ServerManager
(Microsoft.Web.Administration) which provides read and write
access to the IIS 7.0 configuration system.

  Iterate through Microsoft.Web.Administration.SiteCollection, get a
reference to your website using the Site Object and read the value of
the Name property.

// Snippet        
using (ServerManager serverManager = new ServerManager()) {
var sites = serverManager.Sites;
foreach (Site site in sites) {
         Console.WriteLine(site.Name); // This will return the WebSite name
}

  You can also use LINQ to query the ServerManager.Sites collection
(see example below)

// Start all stopped WebSites using the power of Linq :)
var sites = (from site in serverManager.Sites
            where site.State == ObjectState.Stopped
            orderby site.Name
            select site);
        foreach (Site site in sites) {
            site.Start();
        }

---------------------------------

using System;
using System.IO;
using System.DirectoryServices;
class Class
{
    static void Main(string[] args)
    {
        DirectoryEntry entry = FindVirtualDirectory("<Server>", "Default Web Site", "<WantedVirtualDir>");
        if (entry != null)
        {
            Console.WriteLine(entry.Properties["AppPoolId"].Value);
        }
    }
    static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir)
    {
        DirectoryEntry siteEntry = null;
        DirectoryEntry rootEntry = null;
        try
        {
            siteEntry = FindWebSite(server, website);
            if (siteEntry == null)
            {
                return null;
            }
            rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir");
            if (rootEntry == null)
            {
                return null;
            }
            return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir");
        }
        catch (DirectoryNotFoundException ex)
        {
            return null;
        }
        finally
        {
            if (siteEntry != null) siteEntry.Dispose();
            if (rootEntry != null) rootEntry.Dispose();
        }
    }
    static DirectoryEntry FindWebSite(string server, string friendlyName)
    {
        string path = String.Format("IIS://{0}/W3SVC", server);
        using (DirectoryEntry w3svc = new DirectoryEntry(path))
        {
            foreach (DirectoryEntry entry in w3svc.Children)
            {
                if (entry.SchemaClassName == "IIsWebServer" &&
                    entry.Properties["ServerComment"].Value.Equals(friendlyName))
                {
                    return entry;
                }
            }
        }
        return null;
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98942-1-1.html 上篇帖子: windows各个版本安装IIS的位置 下篇帖子: 如何在 vista 的 iis 7 上面配置 asp.net 1.1 开发环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表