PowerShell StartProcess:无效句柄

时间:2018-06-05 18:55:52

标签: powershell

我正在尝试通过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 : {}

捕捉$ _。异常,它给出:

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

3 个答案:

答案 0 :(得分:1)

高程

脚本需要提升。阅读有关远程高程的信息: https://ss64.com/ps/syntax-elevate.html

  

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

     

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

     

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

区域标识符

互联网区域标识符标记可能会妨碍脚本。

来源: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中,您可以使用a删除Zone.Identifier   单独的cmdlet:

     

Unblock-File install-file.exe

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

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

答案 1 :(得分:0)

据我所知,归结为在Azure Web App环境中,您无权自由安装应用程序。

我认为环境管理受到限制,因此可以保证一定的服务水平。

您可以在此处详细了解:

https://docs.microsoft.com/en-us/azure/app-service/choose-web-site-cloud-service-vm

答案 2 :(得分:-2)

尝试双重引用您正在提供Start-Process命令的FilePath,或者使用$(Join-Path $Path $Installer)

现在你正在逃避$ for $ Installer,因此无法解析文件的路径。

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

# OR (even better I think)

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