我正试图在packer实例上使用powershell Invoke-WebRequest命令安装java,但是,我得到了以下错误;
Cannot index into a null array.
At line:1 char: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
几周前,我能够成功地运行它,但从昨天开始,我就出现了上述错误。有什么建议吗?
谢谢。
发生这种情况,因为网页上没有Download Java software for Windows
这样的字符串。由于正则表达式与任何内容都不匹配,因此Groups
成员不存在,并且在尝试索引到不存在的成员时会出现错误。
要么使用web浏览器的"查看源代码"命令,要么将内容保存在文本文件中,并使用类似的记事本进行查看
$cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
Set-Content -Path c:tempjavapage.txt -Value $cc
notepad c:tempjavapage.txt
页面加载一堆Javascript,生成浏览器上看到的实际页面。