windows 如何使用 Powershell 'invoke-expression' 以不同用户身份运行可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3980260/
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
How to user Powershell 'invoke-expression' to run an executable as a different user
提问by Douglas Anderson
I have a c++ service application that launches a Powershell script (powershell.exe script.ps1). In the script an executable is run but needs to be run with different user credentials. Is there a way to do this in Powershell with the invoke-expression cmdlet or some other way?
我有一个启动 Powershell 脚本 (powershell.exe script.ps1) 的 C++ 服务应用程序。在脚本中运行了一个可执行文件,但需要使用不同的用户凭据运行。有没有办法在 Powershell 中使用 invoke-expression cmdlet 或其他方式执行此操作?
回答by Keith Hill
Try Start-Process
e.g.:
尝试Start-Process
例如:
Start-Process cmd.exe -arg "/k whoami.exe" -Credential (Get-Credential)
Of course, for your script you will need to create a credential programmatically rather then using the Get-Credential
which prompts for username/password.
当然,对于您的脚本,您需要以编程方式创建凭据,而不是使用Get-Credential
which 提示输入用户名/密码。