views:

2011

answers:

6

Does anybody know what user privileges are needed for the following code needs to successfully execute as a scheduled task on Windows Server 2003:

System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)

When NOT running as scheduled task i.e. under a logged in user, as long as the user is a member of "Performance Monitor Users", this code will not throw an exception.

When running as a scheduled task under the same user account, it fails.

The only way I can get it to work is to run it as a member of the Local Administrator group.

Any ideas?

A: 

Taken from MSDN:

Permissions LinkDemand - for full trust for the immediate caller. This member cannot be used by partially trusted code.

Darksider
A: 

One issue that I have seen with reading the process name is that access to the performance counters can get disabled.

Crack open your registry and see if this key is there: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance] "Disable Performance Counters"=dword:00000001

You can either set it to zero or deleted it.

John Dyer
A: 

What user rights assignments have you given the account that is running as a scheduled task? You'll need to give the account in question 'Log on as a batch job' in your local security settings.

Update: Does your app write to any files and if so does the scheduled task user have enough rights?

I just knocked up a test app that writes the process names from the Process[] array returned by Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName) to a file and it works just fine as a scheduled task...even running under the identity of a user that is only a member of the Users group (not even a member of 'Performance Monitor Users'.

The folder it writes to is assigned modify rights to SYSTEM, Administrators and the scheduled task user.

Any chance of pasting your code or at least a small enough snippet that demonstrates the exe failing as a scheduled task so we can help diagnose the problem?

Cheers
Kev

Kev
This is already done automatically:In Windows 2000 Server, Windows 2000 Professional, Windows XP Professional and the Windows Server 2003 family, the Task Scheduler automatically grants this right as necessary.
thehowler
A: 

This is already done automatically: In Windows 2000 Server, Windows 2000 Professional, Windows XP Professional and the Windows Server 2003 family, the Task Scheduler automatically grants this right as necessary.

thehowler
A: 

I'm thinking that maybe the answer lies in here?

http://msdn.microsoft.com/en-us/netframework/aa569609.aspx

Bear in mind that I'm using .NET 1.1

thehowler
+1  A: 

My humblest apologies. The user I was using was NOT a member of "Performance Monitor Users" group.

This is necessary for .NET Framework 1.1 implementation of System.Diagnostics.

I have added the user to this group, and all is well.

thehowler