Remote Process.GetProcesses and SessionId

G

Guest

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
 
W

Willy Denoyette [MVP]

Anders Bovin said:
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.
 
G

Guest

OK! Will do.

Thanks Willy

Willy Denoyette said:
Anders Bovin said:
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.
 
W

Willy Denoyette [MVP]

Anders Bovin said:
OK! Will do.

Thanks Willy

Note that there is nothing wrong with this method when you have to query the
processes running on a local machine, the bugs I've mentioned are related to
the GetProcesses("remotemachine") method. This method uses an internal API
"NtQuerySystemInformation"
http://msdn2.microsoft.com/en-us/library/ms724509.aspx which by itself
should never be used in production code, as as an *added bonus* the
Framework code added a couple of bugs in the part that marshals the
(undocumented part of) _SYSTEM_PROCESS_INFORMATION structure.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top