2009-02-09 24 views

回答

1

如果您使用的是.NET語言,則可以嘗試Cassia。在C#中,代碼將爲:

using System; 
using Cassia; 

namespace CassiaSample 
{ 
    public static class Program 
    { 
     public static void Main(string[] args) 
     { 
      ITerminalServicesManager manager = new TerminalServicesManager(); 
      using (ITerminalServer server = manager.GetRemoteServer("server-name")) 
      { 
       server.Open(); 
       foreach (ITerminalServicesSession session in server.GetSessions()) 
       { 
        if ((session.ConnectionState == ConnectionState.Disconnected) 
         || 
         (session.ConnectionState == ConnectionState.Active) 
         && (session.IdleTime > TimeSpan.FromMinutes(1))) 
        { 
         Console.WriteLine("Session {0} (User {1})", session.SessionId, session.UserName); 
        } 
       } 
      } 
     } 
    } 
} 

編輯:更新了示例代碼Cassia 2.0。

2

查找東西/生成WMI代碼和查詢,獲取WMI Code Creator。它將生成測試存根(C#,VB.NET,VBScript)並讓您測試查詢以確保它們返回所需的信息。

終端服務的東西在Win32_Terminal *和Win32_TS *類(有幾個,不知道哪一個是你需要的。)。

我也使用這個助手類(需要一點重構,多年沒有碰過它)來獲取管理對象和執行方法。

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

namespace MyWMI 
{ 
    public class WmiHelper 
    { 
     public static ManagementObjectCollection GetManagementObjectCollection(string ServerName, string WMIQuery) 
     { 
      //determine where the WMI root is that we will connect to. 
      string strNameSpace = "\\\\"; 

      ConnectionOptions connectionOptions = new ConnectionOptions(); 
      TimeSpan tsTimeout = new TimeSpan(0,0,5); 
      connectionOptions.Timeout = tsTimeout; 

      //if its not a local machine connection 
      if (ServerName.Trim().ToUpper() != Globals.HostName) 
      { 
       strNameSpace += ServerName; 
       connectionOptions.Username = Globals.WMIUserDomain + "\\" + Globals.WMIUserName; 
       connectionOptions.Password = Globals.WMIUserPass; 
      } 
      else 
      { //we are connecting to the local machine 
       strNameSpace += "."; 
      } 

      strNameSpace += "\\root\\cimv2"; 

      //create the scope and search 
      ManagementScope managementScope = new ManagementScope(strNameSpace, connectionOptions); 
      ObjectQuery objectQuery = new ObjectQuery(WMIQuery); 
      ManagementObjectSearcher searcher = new ManagementObjectSearcher(managementScope, objectQuery); 
      ManagementObjectCollection returnCollection; 
      try 
      { 
       returnCollection = searcher.Get(); 
      } 
      catch (ManagementException ex) 
      { 
       throw new SystemException("There was an error executing WMI Query. Source: " + ex.Source.ToString() + " Message: " + ex.Message); 
      } 

      //return the collection 
      return returnCollection; 

     } //eng GetManagementObjectCollection 

     public static bool InvokeWMIMethod(string ServerName, string WMIQueryToIsolateTheObject, string MethodName, object[] MethodParams) 
     { 

      //determine where the WMI root is that we will connect to. 
      string strNameSpace = "\\\\"; 

      ConnectionOptions connectionOptions = new ConnectionOptions(); 
      TimeSpan tsTimeout = new TimeSpan(0, 0, 5); 
      connectionOptions.Timeout = tsTimeout; 

      if (ServerName.Trim().ToUpper() != Globals.HostName) 
      { 
       strNameSpace += ServerName; 
       connectionOptions.Username = Globals.WMIUserDomain + "\\" + Globals.WMIUserName; 
       connectionOptions.Password = Globals.WMIUserPass; 
      } 
      else 
      { //we are connecting to the local machine 
       strNameSpace += "."; 
      } 

      strNameSpace += "\\root\\cimv2"; 

      ManagementScope managementScope = new ManagementScope(strNameSpace, connectionOptions); 
      ObjectQuery objectQuery = new ObjectQuery(WMIQueryToIsolateTheObject); 
      ManagementObjectSearcher searcher = new ManagementObjectSearcher(managementScope, objectQuery); 
      ManagementObjectCollection returnCollection = searcher.Get(); 

      if (returnCollection.Count != 1) 
      { 
       return false; 
      } 


      foreach (ManagementObject managementobject in returnCollection) 
      { 
       try 
       { 
        managementobject.InvokeMethod(MethodName, MethodParams); 
       } 
       catch 
       { 
        return false; 
       } 

      } //end foreach 
      return true; 
     } //end public static bool InvokeWMIMethod(string ServerName, string WMIQueryToGetTheObject, string MethodName, object[] MethodParams) 
    } 
} 

@First評論: 伊克......顯然,這是首先想到的更爲複雜。檢查這篇文章(http://www.codeproject.com/KB/system/logonsessions.aspx),在標題爲「」的內容中,內置WMI功能如何?「。如果使用XP,需要進行一些特殊處理,因爲它具有不同的WMI提供程序類(將WMI代碼創建程序更改爲指向遠程計算機 - 例如Win2K3服務器),無論哪種情況,都需要「加入」所有數據的會話類。

+0

哇,你提供了很多很棒的幫助和一些精彩的代碼,但是我找不到我需要的課程。我只想看看三種可用的TS連接中哪一種正在使用,以及標準服務器的用戶和他們的狀態。 – 2009-02-09 13:18:36

+0

我在帖子中回覆(評論不夠大!) – StingyJack 2009-02-09 13:54:01