开发者

How to get list of processes using C# and Task Scheduler

I'm trying to get list of processes under Windows Server 2008 for administration purposes.

I wrote some program in C# similar to this:

foreach ( Process clsProcess in Process.GetProcesses() )
{
    //do something
}

Everything is working until I schedule my program to run from Task Scheduler. Appar开发者_开发问答ently, program cannot see running processes in that way (got empty list).

Any solution to get list of processes while running program by Task Scheduler?


I am running into the same issue and it has to do with in .NET 3.5 and 4. the System.Diagnostic.Process class is not supported on Windows Server 2008 core or Windows Server 2008 R2 core.
Process Class .NET

If you look at Platforms you will see the text that has caused me to loose the rest of my hair. I am still looking for a way to do this and will post when i have a solution..

Well it turns out the the System.ManagementObjectCollection Class is not supported on Server 2008 as well. This is an important part of getting a collection from a WMI query.

After doing some playing yesterday i was able to come up with this dirty solution but it works. below is a vbscript that i call and the method that calls the script.

VBScript to query process

process = Wscript.Arguments.Item(0)
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Processs = oWMI.ExecQuery("SELECT * FROM Win32_Process where Name='"& process &"'")
count = 0
ON ERROR RESUME NEXT
FOR EACH proc IN Processs
    count = count + 1
Next
IF Err.Number > 0 THEN
    count = -1
END IF
wscript.StdOut.Write count

C# Method to call script

 private bool IsProcessRunning()
    {
        int pcReturn = -1;
        bool blRunning = false;
        String strCmd = @"cscript";
        String arg = "//B //Nologo ProcessSearch.vbs YourProcess.exe";

        System.Diagnostics.ProcessStartInfo psi = new     System.Diagnostics.ProcessStartInfo(strCmd);
        psi.RedirectStandardOutput = true;
        psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        psi.UseShellExecute = false;
        psi.Arguments = arg;

        System.Diagnostics.Process procQuery;
        procQuery = System.Diagnostics.Process.Start(psi);
        String output = procQuery.StandardOutput.ReadToEnd();
        procQuery.WaitForExit();
        if (procQuery.HasExited)
        {
            bool isInt = Int32.TryParse(output, out pcReturn);
            if (!isInt)
            {
                pcReturn = -1;
            }
            else
            {
                if (pcReturn > 1)
                {
                    blRunning = true;
                }
            }
        }
        return blRunning;
    }


Alan, I'm definitely not an expert, but it sounds like user-permissions.

Apparently when setting up your task in Task Scheduler, you can choose which user it will run as, and subsequently which set of permissions the task has.

Perhaps this link will be of use.


I don't know the reason for that behavior. But perhaps the System.Management API will work in this case. Here's an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜