最近做安装包是,需要在 .net 程序中调用 WMI 功能,创建 web 站点,虚拟目录和添加 host 头部信息,以前曾经用脚步做过类似的功能,用.net代码来做,倒是第一次。

查找了相关资料后,主要需要调用到  System.Management 命名空间下面的一些类来实现:itpob.net

创建 Web 站点,该函数返回站点的 ID (这个 ID 是什么意思可以参考下面的文章获取:http://www.itpob.net/home/link.p ... F12%2F08%2F346.aspx


CreateWebsite
  1. public static string CreateWebsite(string serverName, string appPoolName, string ip, string pathToRoot, string hostName, string domainName, int port)
  2.         {
  3.             ConnectionOptions options = new ConnectionOptions();
  4.             options.Authentication = AuthenticationLevel.Connect;
  5.             options.EnablePrivileges = true;
  6.             options.Impersonation = ImpersonationLevel.Impersonate;
  7.             ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISv2, serverName"), options);
  8.             scope.Connect();
  9.             ManagementObject oW3SVC = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);

  10.             ManagementBaseObject[] serverBindings = new ManagementBaseObject[1];
  11.             serverBindings[0] = CreateServerBinding(scope, string.Format("{0}.{1}", hostName, domainName), ip, port);
  12.             ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
  13.             inputParameters["ServerComment"] = string.Format("{0}.{1}", hostName, domainName);
  14.             inputParameters["ServerBindings"] = serverBindings;
  15.             inputParameters["PathOfRootVirtualDir"] = pathToRoot;
  16.             ManagementBaseObject outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);

  17.             string siteId = Convert.ToString(outParameter.Properties["ReturnValue"].Value).Replace("IIsWebServer='W3SVC/", "").Replace("'", "");
  18.             ManagementObject oWebVirtDir = new ManagementObject(scope,
  19.             new ManagementPath(string.Format(@"IIsWebVirtualDirSetting.Name='W3SVC/{0}/root'", siteId)), null);
  20.             oWebVirtDir.Properties["AppFriendlyName"].Value = string.Format("{0}.{1}", hostName, domainName);
  21.             oWebVirtDir.Properties["AccessRead"].Value = true;
  22.             oWebVirtDir.Properties["AuthFlags"].Value = 5; // Windows 集成验证
  23.             oWebVirtDir.Properties["AccessScript"].Value = true;
  24.             oWebVirtDir.Properties["AuthAnonymous"].Value = true;
  25.             oWebVirtDir.Properties["AppPoolId"].Value = appPoolName;
  26.             oWebVirtDir.Put();

  27.             ManagementObject site = new ManagementObject(scope, new ManagementPath(Convert.ToString(outParameter.Properties["ReturnValue"].Value)), null);
  28.             site.InvokeMethod("Start", null);

  29.             return siteId;
  30.         }
复制代码
创建虚拟目录
  1. AddVirtualFolder
  2.         public static void AddVirtualFolder(string serverName, string websiteId, string name, string path)
  3.         {
  4.             ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName));
  5.             scope.Connect();

  6.             string siteName = string.Format("W3SVC/{0}/Root/{1}", websiteId, name);

  7.             ManagementClass mc = new ManagementClass(scope,new ManagementPath("IIsWebVirtualDirSetting"), null);
  8.             ManagementObject oWebVirtDir = mc.CreateInstance();

  9.             oWebVirtDir.Properties["Name"].Value = siteName;
  10.             oWebVirtDir.Properties["Path"].Value = path;
  11.             oWebVirtDir.Properties["AuthFlags"].Value = 5; // Windows 集成验证
  12.             oWebVirtDir.Properties["EnableDefaultDoc"].Value = true;
  13.             // date, time, size, extension, longdate ;
  14.             oWebVirtDir.Properties["DirBrowseFlags"].Value = 0x4000003E;
  15.             oWebVirtDir.Properties["AccessFlags"].Value = 513; // read script
  16.             oWebVirtDir.Put();

  17.             ManagementObject mo = new ManagementObject(scope,new System.Management.ManagementPath("IIsWebVirtualDir='" +siteName + "'"), null);
  18.             ManagementBaseObject inputParameters = mo.GetMethodParameters("AppCreate2");
  19.             inputParameters["AppMode"] = 2;
  20.             mo.InvokeMethod("AppCreate2", inputParameters, null);
  21.             mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDirSetting='" + siteName + "'"), null);
  22.             mo.Properties["AppFriendlyName"].Value = name;
  23.             mo.Put();
  24.         }
复制代码
添加 host 头
  1.         public static void AddHostHeader(string serverName, string hostHeader, string ip, int port, string websiteID)
  2.         {
  3.             ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName));
  4.             scope.Connect();

  5.             string siteName = string.Format("'W3SVC/{0}'", websiteID);

  6.             ManagementObject mo = new ManagementObject(scope,new System.Management.ManagementPath("IIsWebServerSetting=" + siteName), null);
  7.             ManagementBaseObject[] websiteBindings =(ManagementBaseObject[])mo.Properties["ServerBindings"].Value;

  8.             ManagementObject mco = CreateServerBinding(scope, hostHeader, ip, port);

  9.             ManagementBaseObject[] newWebsiteBindings =new ManagementBaseObject[websiteBindings.Length + 1];
  10.             websiteBindings.CopyTo(newWebsiteBindings, 0);
  11.             newWebsiteBindings[newWebsiteBindings.Length - 1] = mco;

  12.             mo.Properties["ServerBindings"].Value = newWebsiteBindings;

  13.             mo.Put();
  14.         }

  15.         private static ManagementObject CreateServerBinding(ManagementScope scope, string hostName, string ip, int port)
  16.         {
  17.             ManagementClass mc = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
  18.             ManagementObject mco = mc.CreateInstance();

  19.             mco.Properties["Hostname"].Value = hostName;
  20.             mco.Properties["IP"].Value = ip;
  21.             mco.Properties["Port"].Value = port;
  22.             mco.Put();

  23.             return mco;
  24.         }
复制代码
文/ itpob
TOP