工作之杂记--c# 使用wmi to start/stop IIS6 应用程序池和webserver

使用wmi to start/stop IIS6 应用程序池和webserver

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

namespace UseWMIManageIIS
{
    /// <summary>
    /// 建立链接到IIS6的实例,并获取appPool和website
    /// </summary>
    public class InternetInformationServer:WmiObjectBase
    {
        /// <summary>
        /// 创建一个IIS实例链接到本地IIS实例
        /// </summary>
        public InternetInformationServer() : base(@"\\.\root\MicrosoftIISV2") { }
        /// <summary>
        /// 创建一个InternetInformationServer类的实例连接到自定义目标
        /// </summary>
        /// <param name="target"></param>
        public InternetInformationServer(string target, string userName, string passWord)
            : base(string.Format(@"\\{0}\root\MicrosoftIISV2", target), userName, passWord)
        { }
        /// <summary>
        /// 获取线程池
        /// </summary>
        /// <param name="appName"></param>
        /// <returns></returns>
        public ApplicationPool GetApplicationPool(string appName)
        {
            ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM IIsApplicationPoolSetting WHERE Name='W3SVC/AppPools/{0}'", appName));
            foreach (ManagementObject item in this[query])
            {
                return new ApplicationPool(this.Scope, (string)item.Properties["Name"].Value);
            }
            return null;
        }
        /// <summary>
        /// 获取WebSite
        /// </summary>
        /// <param name="serverComment"></param>
        /// <returns></returns>
        public WebSite GetWebSite(string serverComment)
        {
            ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM IISWebServerSetting WHERE ServerComment ='{0}'", serverComment));
            foreach (ManagementObject item in this[query])
            {
                return new WebSite(this.Scope, item["ServerComment"].ToString(), item["Name"].ToString());
            }
            return null;
        }
        /// <summary>
        /// 迭代所有应用程序池
        /// </summary>
        public IEnumerator<ApplicationPool> AppPools
        {
            get {
                ObjectQuery query = new ObjectQuery("SELECT * FROM IIsApplicationPoolSetting");
                foreach (ManagementObject appPool in this[query])
                {
                    yield return new ApplicationPool(this.Scope, (string)appPool.Properties["Name"].Value);
                }
            }
        }
        /// <summary>
        /// 迭代所有website
        /// </summary>
        public IEnumerator<WebSite> Sites
        {
            get {
                ObjectQuery query = new ObjectQuery("SELECT * FROM IISWebServerSetting");
                foreach (ManagementObject site in this[query])
                {
                    yield return new WebSite(this.Scope, site["ServerComment"].ToString(), site["Name"].ToString());
                }
            }
        }
    }
}


class AppcationPool

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

namespace UseWMIManageIIS
{
    public class ApplicationPool:WmiObjectBase
    {
        protected readonly string IdentityQuery;
        public string Name { get; internal set; }
        public ApplicationPool(ManagementScope Scope,string name) : base(Scope) 
        {
            this.Name = name;
            this.IdentityQuery = string.Format("SELECT * FROM IIsApplicationPool WHERE Name='{0}'", Name);
        }
        /// <summary>
        /// Start AppPool
        /// </summary>
        public void Start()
        {
            AppPoolState state = CurrentState;
            if (state == AppPoolState.Stopped)
            {
                ObjectQuery query = new ObjectQuery(IdentityQuery);
                foreach (ManagementObject item in this[query])
                {
                    item.InvokeMethod("Start", new object[0]);
                }
            }
        }
        /// <summary>
        /// Stop AppPool
        /// </summary>
        public void Stop()
        {
            AppPoolState state = CurrentState;
            if (state == AppPoolState.Started)
            {
                ObjectQuery query = new ObjectQuery(IdentityQuery);
                foreach (ManagementObject item in this[query])
                {
                    item.InvokeMethod("Stop", null);
                }
            }
        }
        /// <summary>
        /// 线程池当前状态
        /// </summary>
        public AppPoolState CurrentState
        {
            get {
                ObjectQuery Query = new ObjectQuery(string.Format("SELECT AppPoolState FROM IIsApplicationPoolSetting WHERE Name='{0}'", this.Name));
                foreach (ManagementObject item in this[Query])
                {
                    int state = 5;
                    bool flag = int.TryParse(item.Properties["AppPoolState"].Value.ToString(), out state);
                    return (AppPoolState)state; 
                }
                throw new Exception("Can't determine AppPool's current state");
            }
        }
    }

    public enum AppPoolState
    {
        Starting = 1,
        Started = 2,
        Stopping = 3,
        Stopped = 4,
        Unknown = 5,
    }
}

class WmiObjectBase

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

namespace UseWMIManageIIS
{
    public abstract class WmiObjectBase
    {
        /// <summary>
        /// 供子类对象使用的管理作用域
        /// </summary>
        protected ManagementScope Scope { get; private set; }
        /// <summary>
        /// 创建链接作用域
        /// </summary>
        /// <param name="path"></param>
        protected WmiObjectBase(string path)
        {
            ConnectionOptions connection = new ConnectionOptions();//指定要進行 WMI 連接所需的所有設定。
            connection.Authentication = AuthenticationLevel.PacketPrivacy;//数据包保密性级别的COM身份验证
            this.Scope = new ManagementScope(path, connection);
            this.Scope.Connect();
        }
        /// <summary>
        /// 创建远程链接作用域
        /// </summary>
        /// <param name="path"></param>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        protected WmiObjectBase(string path, string userName, string passWord)
        {
            ConnectionOptions connection = new ConnectionOptions();
            connection.Authentication = AuthenticationLevel.PacketPrivacy;
            connection.Username = userName;
            connection.Password = passWord;
            this.Scope = new ManagementScope(path, connection);
            this.Scope.Connect();
        }
        /// <summary>
        /// 供子类使用现有的作用域链接
        /// </summary>
        /// <param name="Scope"></param>
        protected WmiObjectBase(ManagementScope Scope)
        {
            this.Scope = Scope;
            if (!this.Scope.IsConnected)
                this.Scope.Connect();
        }
        /// <summary>
        /// 检索索引器,通过ManagementObjectSearcher执行wmi查询,检索管理对象
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        protected ManagementObjectCollection this[ObjectQuery Query]
        {
            //通过 WMI 检索到的管理对象的不同集合,包括 ManagementObject 和 ManagementClass。
            get {
                return new ManagementObjectSearcher(this.Scope,Query).Get();
            }
        }
        /// <summary>
        /// 管理对象设置
        /// </summary>
        /// <param name="target"></param>
        /// <param name="properties">字典,包含属性key和值</param>
        protected void ApplySettings(ManagementObject target, Dictionary<string, object> properties)
        {
            foreach (var kv in properties)
            {
                target.Properties[kv.Key].Value = kv.Value;
            }
            target.Put();
        }
        /// <summary>
        /// 创建管理对象的简单方法
        /// </summary>
        /// <param name="path">对象的管理路径</param>
        /// <returns></returns>
        protected ManagementObject CreateManagementObject(string path)
        {
            ManagementClass template = new ManagementClass(this.Scope, new ManagementPath(path), null);
            return template.CreateInstance();
        }
        /// <summary>
        /// 让子类来获得现有对象的实例
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        protected ManagementObject GetInstance(string path)
        {
            return new ManagementObject(this.Scope, new ManagementPath(path), null);
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值