2011-02-01 71 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); 
}