如何让 Windows 软件在脚本中以不同的用户身份运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/98134/
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:14:34  来源:igfitidea点击:

How can I make Windows software run as a different user within a script?

windowsbuild-processscriptingwise

提问by Luke

I'm using a build script that calls Wise to create some install files. The problem is that the Wise license only allows it to be run under one particular user account, which is not the same account that my build script will run under. I know Windows has the runascommand but this won't work for an automated script as there is no way to enter the password via the command line.

我正在使用一个调用 Wise 的构建脚本来创建一些安装文件。问题是 Wise 许可证只允许它在一个特定的用户帐户下运行,这与我的构建脚本运行的帐户不同。我知道 Windows 有runas命令,但这不适用于自动脚本,因为无法通过命令行输入密码。

回答by Jeffrey Vanneste

I recommend taking a look at CPAU.

我建议看看CPAU

Command line tool for starting process in alternate security context. Basically this is a runas replacement. Also allows you to create job files and encode the id, password, and command line in a file so it can be used by normal users.

用于在备用安全上下文中启动进程的命令行工具。基本上这是一个 runas 替代品。还允许您创建作业文件并将 id、密码和命令行编码到文件中,以便普通用户可以使用。

You can use it like this (examples):

您可以像这样使用它(示例):

CPAU -u user [-p password] -ex "WhatToRun" [switches]

Or you can create a ".job" file which will have the user and password encoded inside of it. This way you can avoid having to put the password for the user inside your build script.

或者您可以创建一个“.job”文件,该文件将在其中编码用户和密码。这样您就可以避免将用户的密码放入构建脚本中。

回答by jfs

It's a bit of a workaround solution, but you can create a scheduled task that runs as your user account, and have it run regularly, maybe once every minute. Yes, you'll have to wait for it to run then.

这是一个有点变通的解决方案,但您可以创建一个以您的用户帐户运行的计划任务,并让它定期运行,也许每分钟运行一次。是的,你必须等待它运行。

This task can then look for some data files to process, and do the real work only if they are there.

然后,此任务可以查找一些要处理的数据文件,并且只有在它们存在时才执行真正的工作。

回答by Asmor

This might help, it's a class I've used in another project to let people make their own accounts; everyone had to have access to the program, but the same account couldn't be allowed to have access to the LDAP stuff, so the program uses this class to run it as a different user.

这可能会有所帮助,这是我在另一个项目中使用的一个类,用于让人们创建自己的帐户;每个人都必须有权访问该程序,但不允许同一帐户访问 LDAP 内容,因此该程序使用此类以不同用户身份运行它。

http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx

http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx