2011-02-01 70 views
8

我想在C#中启动/停止一个基于Windows的群集,下面是我目前使用的代码...当我得到TakeOffLine函数时,我得到一个“Not Found”来自System.Management.ManagementStatus.NotFound的异常。不确定究竟发现了什么?如果有(更换)更好的方法,请让我知道。从C#管理Windows群集

谢谢!

using System.Management; 
class App 
{ 
    public static void Main() 
    { 
     string clusterName = "clusterHex"; // cluster alias 
     string custerGroupResource = "clusterHex.internal.com"; // Cluster group name 

     ConnectionOptions options = new ConnectionOptions(); 
     options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy; 

     // Connect with the mscluster WMI namespace on the cluster named "MyCluster" 
     ManagementScope s = new ManagementScope("\\\\" + clusterName + 
      "\\root\\mscluster", options); 

     ManagementPath p = new ManagementPath("Mscluster_Clustergroup.Name='" + custerGroupResource + "'"); 

     using (ManagementObject clrg = new ManagementObject(s, p, null)) 
     { 
      // Take clustergroup off line and read its status property when done 
      TakeOffLine(clrg); 
      clrg.Get(); 
      Console.WriteLine(clrg["Status"]); 
      System.Threading.Thread.Sleep(3000); // Sleep for a while 
      // Bring back online and get status. 
      BringOnLine(clrg); 
      clrg.Get(); 
      Console.WriteLine(clrg["Status"]); 

     } 
    } 
    static void TakeOffLine(ManagementObject resourceGroup) 
    { 
     ManagementBaseObject outParams = 
     resourceGroup.InvokeMethod("Takeoffline", null, null); 
    } 
    static void BringOnLine(ManagementObject resourceGroup) 
    { 
     ManagementBaseObject outParams = 
     resourceGroup.InvokeMethod("Takeoffline", null, null); 
    } 
} 
+1

没有使用Powershell cmdlet的方法吗? – 2011-02-01 20:08:31

回答

3

看起来你在你的方法调用中缺少大小写。您必须使用TakeOffline根据msdn

static void TakeOffLine(ManagementObject resourceGroup) 
{ 
    ManagementBaseObject outParams = 
    resourceGroup.InvokeMethod("TakeOffline", null, null); 
}