【问题标题】:PowerShell error: "The handle is invalid" when using another user's credentialsPowerShell 错误:使用其他用户的凭据时出现“句柄无效”
【发布时间】: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


【解决方案1】:

您需要以管理员身份打开 Powershell。只需右键单击 Powershell,然后单击以管理员身份运行。它应该可以正常工作,您不会看到The handle is invalid error

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多