0
A
回答
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服務器),無論哪種情況,都需要「加入」所有數據的會話類。
相關問題
- 1. Android遠程服務查詢
- 2. 從Linux終端查詢Web服務器
- 3. 如何從Mac OS終端連接到遠程mongo服務器
- 4. 使用遠程桌面服務/終端服務器安裝2008服務器
- 5. 慢查詢到遠程服務器
- 6. 查詢:rsync的 - 從遠程服務器
- 7. 發送查詢到遠程服務器
- 8. 從遠程服務查詢數據庫
- 9. 如何通過端口號遠程啓動和停止服務?
- 10. 如何在遠程服務器上執行查詢
- 11. 如何使用VB.NET查詢遠程服務器事件日誌?
- 12. 從客戶端終止.NET遠程異步服務調用
- 13. c#從遠程機器傳輸文件到終端服務器
- 14. 複製本地文件到遠程服務器在ubuntu終端
- 15. 通過終端服務器遠程調試
- 16. 在Visual Studio代碼終端中使用遠程服務器?
- 17. 用於Windows 8的遠程桌面/終端服務API Metro
- 18. 如何正常終止遠程服務進程?
- 19. SQL:執行查詢到動態遠程服務器
- 20. 如何使用sage sdata服務查詢銀行活動
- 21. 終端服務器
- 22. 遠程服務 - 關閉啓動後重新啓動活動
- 23. 從輪詢線程連接到遠程端口(服務器)
- 24. 如何添加/審查遠程MySQL訪問主機與終端
- 25. 如何檢查遠程Oracle服務器已啓動並運行
- 26. 終端服務命令「查詢會話」在C#
- 27. 如何檢查相機應用程序是否是服務的活動活動?
- 28. 啓動雜種服務器在終端
- 29. 遠程處理(服務器端)
- 30. 谷歌地圖遠程服務器端
哇,你提供了很多很棒的幫助和一些精彩的代碼,但是我找不到我需要的課程。我只想看看三種可用的TS連接中哪一種正在使用,以及標準服務器的用戶和他們的狀態。 – 2009-02-09 13:18:36
我在帖子中回覆(評論不夠大!) – StingyJack 2009-02-09 13:54:01