"Anders Bovin" <An*********@discussions.microsoft.comwrote in message
news:03**********************************@microsof t.com...
Hi,
I am trying to get the SessionId of processes on a remote machine using
System.Diagnostics.Process:
foreach (Process p in Process.GetProcesses("remotemachine"))
{
Console.WriteLine(p.SessionId.ToString());
}
SessionId is always 0 while Task Manager (on the remote machine) shows the
correct Session ID. Any ideas why?
Thanks,
Anders
Don't use this method, it's just a bag of bugs, use System.Management (WMI)
instead.
Following snip illustrates how you can get the Processes SessionId from a
remote server:
Check MSDN for more detail on WMI's Win32_Process class.
....
ConnectionOptions co = new ConnectionOptions();;
co.Username = "administrator"; //any account with appropriate privileges
co.Password = "pppppp";
string remMachine = "machineName";
ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\cimv2", co);
SelectQuery selectQuery = new SelectQuery("Select Name, SessionId
from Win32_Process");
using(ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, selectQuery))
{
foreach (ManagementObject proc in searcher.Get())
Console.WriteLine("{0}, {1}",proc["Name"].ToString(),
proc["SessionId"].ToString());
}
Willy.