Windows PowerShell错误:";句柄无效";使用其他用户时';国书

Windows PowerShell错误:";句柄无效";使用其他用户时';国书,windows,shell,powershell,scripting,credentials,Windows,Shell,Powershell,Scripting,Credentials,我正在尝试使用其他用户的凭据通过PowerShell 1.0运行可执行文件。我当前的脚本如下所示: $username = "adminuser" $password = "thepassword" $secstr = ConvertTo-SecureString -String $password -AsPlainText -Force $cred = New-Object -TypeName System.Management.Automation.PSCredential -Argumen

我正在尝试使用其他用户的凭据通过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
我收到的错误如下:

$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
关于我为什么得到
句柄无效错误
有什么想法吗


根据,我已尝试添加重定向,但相同的错误仍然存在。

您需要以管理员身份打开Powershell。只需右键单击Powershell,然后单击以管理员身份运行。它应该可以正常工作,您不会看到
句柄无效错误

您需要以管理员身份打开Powershell。只需右键单击Powershell,然后单击以管理员身份运行。它应该可以正常工作,并且您不会看到
句柄无效错误

为什么会向启动进程传递两个不同的可执行文件名?我有点惊讶这不是语法错误,文档中描述的语法似乎不允许。我想使用PowerShell以其他用户的身份运行其他可执行文件。但您已经在PowerShell中。启动进程是一个PowerShell命令。这是一个很好的观点。让我们去掉第二个PowerShell调用。在启动进程C:\Users\Public\myexecutable.exe-verb runas-Credential$cred
时,仍然会导致相同的错误。它是
-Credential
-verb
。您不能同时使用它们。为什么要传递两个不同的可执行文件名来启动进程?我有点惊讶这不是语法错误,文档中描述的语法似乎不允许。我想使用PowerShell以其他用户的身份运行其他可执行文件。但您已经在PowerShell中。启动进程是一个PowerShell命令。这是一个很好的观点。让我们去掉第二个PowerShell调用。在启动进程C:\Users\Public\myexecutable.exe-verb runas-Credential$cred
时,仍然会导致相同的错误。它是
-Credential
-verb
。你不能两者都用。