GetProcessesByName() 和 Windows Server 2003 计划任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:20:31  来源:igfitidea点击:

GetProcessesByName() and Windows Server 2003 scheduled task

windowspermissionsscheduled-tasks.net-1.1

提问by

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

有谁知道以下代码需要什么用户权限才能作为 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?

有任何想法吗?

回答by

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.

这是 System.Diagnostics 的 .NET Framework 1.1 实现所必需的。

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

我已将该用户添加到该组,一切正常。

回答by Kev

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'.

我刚刚敲了一个测试应用程序,它将 Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName) 返回的 Process[] 数组中的进程名称写入文件,它作为计划任务运行得很好......甚至在只是用户组成员的用户的身份(甚至不是“性能监视器用户”的成员。

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

它写入的文件夹被分配给 SYSTEM、Administrators 和计划任务用户的修改权限。

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?

是否有机会粘贴您的代码或至少一个足够小的片段来证明 exe 作为计划任务失败,以便我们帮助诊断问题?

Cheers
Kev

干杯
凯夫

回答by John Dyer

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

打开你的注册表,看看这个键是否存在:[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance]“禁用性能计数器”=dword:00000001

You can either set it to zero or deleted it.

您可以将其设置为零或将其删除。

回答by EggyBach

Taken from MSDN:

摘自MSDN

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

权限LinkDemand - 为直接呼叫者提供完全信任。该成员不能由部分信任的代码使用。