リモートマシンにログインしているユーザーを取得する方法を探しています。彼らがローカルにログオンしているかリモートにログオンしているかを知りたいのですが、何よりも彼らのステータスを知らなければなりません。ネット上でVBで書かれた回答を見ましたが、c#で必要です。ここでmarkdmakの回答に示されているソリューションは良いスタートのように見えますが、VBであり、リモートセッションのみを検索します。私はこのコードを持っていますが、これは開始点になる可能性がありますが、LogonIdをユーザー名に結合して、そのステータスを確認したいと思います。
string fqdn = ""; // set!!!
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
// To connect to the remote computer using a different account, specify these values:
// these are needed in dev environment
options.Username = ConfigurationManager.AppSettings["KerberosImpersonationUser"];
options.Password = ConfigurationManager.AppSettings["KerberosImpersonationPassword"];
options.Authority = "ntlmdomain:" + ConfigurationManager.AppSettings["KerberosImpersonationDomain"];
ManagementScope scope = new ManagementScope("\\\\" + fqdn + "\\root\\CIMV2", options);
try
{
scope.Connect();
}
catch (Exception ex)
{
if (ex.Message.StartsWith("The RPC server is unavailable"))
{
// The Remote Procedure Call server is unavailable
// cannot check for logged on users
return false;
}
else
{
throw ex;
}
}
SelectQuery query = new SelectQuery("Select * from Win32_LogonSession");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection results = searcher.Get();
bool returnVal = false;
foreach (ManagementObject os in results)
{
try
{
if (os.GetPropertyValue("LogonId").ToString() != null && os.GetPropertyValue("LogonId").ToString() != "")
{
returnVal = true;
}
}
catch (NullReferenceException)
{
continue;
}
}
return returnVal;
}
私が本当に必要としているのは、リモートマシン上のすべてのユーザーとそのステータス(アクティブ、切断済み、ログオフなど)を取得する方法です。