英文:
Error:- Cannot index into a null array while installing java
问题
我正在尝试在 Packer 实例上使用 PowerShell 的 Invoke-WebRequest 命令安装 Java,然而,我遇到了以下错误;
```powershell
无法索引空数组。
在第1行第94个字符处:+ ... content | %{[regex]::matches($_, ''(?:<a title="Download Java software ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
命令:
[Net.ServicePointManager]::SecurityProtocol = 'tls12'
$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, ''(?:<a title="Download Java software for Windows .64-bit." href=")(.*)(?:">)').Groups[1].Value}
Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL
Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait
几周前我能够成功运行它,但是从昨天开始出现了上述错误。
有什么建议吗?
谢谢。
<details>
<summary>英文:</summary>
I am trying to install java using powershell Invoke-WebRequest command on packer instance however, I am getting below error;
Cannot index into a null array.
At line:1 char:94
- ... content | %{[regex]::matches($_, '(?:<a title="Download Java software ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (:) [], RuntimeException
- FullyQualifiedErrorId : NullArray
Command;
`[Net.ServicePointManager]::SecurityProtocol = "tls12"`
`$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, '(?:<a title="Download Java software for Windows .64-bit." href=")(.*)(?:">)').Groups[1].Value}`
`Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL`
`Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait`
Few weeks ago I was able to run it successfully but since yesterday getting the above error.
Any advise?
Thanks.
</details>
# 答案1
**得分**: 2
这是因为在网页上并不存在类似于“Download Java software for Windows”的字符串。由于正则表达式没有匹配到任何内容,“Groups”成员也不存在,所以当尝试索引一个不存在的成员时会出现错误。
你可以使用浏览器的“查看源代码”命令,或者将内容保存到文本文件中,然后像这样使用记事本查看:
$cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
Set-Content -Path c:\temp\javapage.txt -Value $cc
notepad c:\temp\javapage.txt
该页面加载了大量的Javascript代码,用于生成在浏览器中看到的实际页面。
<details>
<summary>英文:</summary>
This happens, as there is no such as string as `Download Java software for Windows` on the web page. Since the regex doesn't match anything, `Groups` member doesn't exist and you'll get an error about trying to index into a non-existing member.
Either use a web browser's View Source command, or save the content on a text file and view it with Notepad like so,
$cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
Set-Content -Path c:\temp\javapage.txt -Value $cc
notepad c:\temp\javapage.txt
The page loads a bunch of Javascript that generates the actual page seen on a browser.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论