如何远程查询终端服务活动?

时间:2009-02-09 12:43:25

标签: wmi terminal-services

我想查询我们的服务器以查看断开连接/空闲会话。我知道我可以使用'query.exe',但我更喜欢用代码处理的东西。

WMI是我的首选。

感谢。

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 comment: Ick ......显然这个比先想到的更复杂。请查看本文(http://www.codeproject.com/KB/system/logonsessions.aspx),标题为“内置WMI功能怎么样?”一节。如果使用XP,则需要一些特殊处理,因为它具有不同的WMI提供程序类(例如,将WMI代码创建者更改为指向远程计算机 - 例如Win2K3服务器),在任何一种情况下,您都需要“加入”所有数据会话类。

答案 1 :(得分: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的示例代码。