0

我想查询我们的服务器以查看是否有断开/空闲会话。我知道我可以使用“query.exe”,但我更喜欢从代码中更容易使用的东西。

WMI 将是我的首选。

谢谢。

4

2 回答 2

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

于 2009-02-09T12:47:51.600 回答
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 的示例代码。

于 2009-04-09T02:54:31.493 回答