首页 > 解决方案 > PowerShell StartProcess:无效句柄

问题描述

我正在尝试通过 powershell 在远程机器上安装 google chrome。这就是我正在尝试做的事情(我几乎只是从各个网站上的其他几篇帖子中拼凑出来的):

$Path = $env:TEMP; 

$Installer = "chrome_installer.exe";

(new-object System.Net.WebClient).DownloadFile('http://dl.google.com/chrome/install/375.126/chrome_installer.exe', "$Path\$Installer");

Start-Process -FilePath $Path\$Installer -Args "/silent /install" -Verb RunAs -Wait;

Remove-Item $Path\$Installer 

它在第四行失败: Start-Process -FilePath $Path\$Installer -Args "/silent /install" -Verb RunAs -Wait;

出现错误:

Start-Process : This command cannot be run due to the error: The handle is 
 invalid.
At line:1 char:2
+  Start-Process -FilePath $Path\$Installer -Args "/silent /install" -V ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOp 
   erationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C 
   ommands.StartProcessCommand

我对 PowerShell 非常缺乏经验,我很难弄清楚错误中的“句柄”是什么。任何帮助表示赞赏:)

编辑:try/catch { $_ | FL * -Force}在失败的命令周围,它给出了这个输出 :

PSMessageDetails      : 
Exception             : System.InvalidOperationException: This command cannot 
                        be run due to the error: The handle is invalid.
                           at System.Management.Automation.MshCommandRuntime.Th
                        rowTerminatingError(ErrorRecord errorRecord)
TargetObject          : 
CategoryInfo          : InvalidOperation: (:) [Start-Process], 
                        InvalidOperationException
FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands
                        .StartProcessCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 4
PipelineIterationInfo : {}

而是捕获 $_.Exception ,它给出:

Message        : This command cannot be run due to the error: The handle is 
             invalid.
Data           : {}
InnerException : 
TargetSite     : Void ThrowTerminatingError(System.Management.Automation.ErrorR
                 ecord)
StackTrace     :    at System.Management.Automation.MshCommandRuntime.ThrowTerm
                 inatingError(ErrorRecord errorRecord)
HelpLink       : 
Source         : System.Management.Automation
HResult        : -2146233079

标签: powershell

解决方案


海拔

脚本需要提升。要了解远程提升: https ://ss64.com/ps/syntax-elevate.html

如果您使用 Invoke-Command 在远程计算机上运行脚本或命令,那么即使本地会话是,它也不会运行提升。这是因为任何提升提示都将在远程计算机上的非交互式会话中发生,因此会失败。

如果您指定 CredSSP,则使用 E​​nter-PSSession 启动一个全新的会话将支持提升,这将启用用户凭据的委派:

New-PSSession ss64dom.com -Auth CredSSP -cred ss64dom\user64

区域标识符

该脚本可能会受到 Internet 区域标识符标记的阻碍。

来源:http ://woshub.com/how-windows-determines-that-the-file-has-been-downloaded-from-the-internet/

在 PowerShell 3.0 中,您可以使用以下命令在目录中显示带有 Zone.Identifier 流的文件列表:

Get-ChildItem -Recurse | Get-Item -Stream Zone.Identifier -ErrorAction SilentlyContinue | Select-Object FileName

该属性被删除如下:

Remove-Item .\install-file.exe -Stream Zone.Identifier

在 Windows PowerShell 4.0 中,您可以使用单独的 cmdlet 删除 Zone.Identifier:

Unblock-File install-file.exe

附录:如果没有找到备用流,Remove-Item 将引发错误。因此使用:

Remove-Item $Path\$Installer -Stream Zone.Identifier -ErrorAction SilentlyContinue


推荐阅读