【发布时间】:2017-04-27 23:35:43
【问题描述】:
我正在尝试使用其他用户的凭据通过 PowerShell 1.0 运行可执行文件。我当前的脚本如下:
$username = "adminuser"
$password = "thepassword"
$secstr = ConvertTo-SecureString -String $password -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secstr
Start-Process powershell.exe -verb runas -Credential $cred -File C:\Users\Public\myexecutable.exe
我收到的错误如下:
Error: Start-Process : This command cannot be run due to the error: The handle is invalid.
At C:\Users\Public\privy2.ps1:8 char:1
+ Start-Process powershell.exe -Credential $cred
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
在没有凭据的情况下使用命令时,脚本可以运行并且可执行文件会运行:
Start-Process powershell.exe -verb runas -File C:\Users\Public\myexecutable.exe
关于我为什么收到The handle is invalid error 的任何想法?
根据.NET Process Start Process Error using credentials (The handle is invalid),我尝试添加重定向,但同样的错误仍然存在。
【问题讨论】:
-
为什么有两个不同的可执行文件名被传递给 Start-Process?我有点惊讶这不是语法错误,文档中描述的语法似乎不允许。
-
我想使用 PowerShell 以其他用户身份运行其他可执行文件。
-
但是您已经在 PowerShell中。 Start-Process 是一个 PowerShell 命令。
-
这是一个很好的观点。让我们去掉第二个 PowerShell 调用。留下
Start-Process C:\Users\Public\myexecutable.exe -verb runas -Credential $cred,仍然会导致同样的错误。 -
是
-Credential或-Verb。你不能同时使用它们。
标签: windows shell powershell scripting credentials