ChatGPT解决这个技术问题 Extra ChatGPT

无法解析响应内容,因为 Internet Explorer 引擎不可用,或者

我需要使用 powershell 下载频道 9 系列,但是我尝试过的脚本有错误:

此脚本 $url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high" $rss=invoke-webrequest -uri $url $destination="D:\Videos\OfficePnP" [xml]$rss .Content|foreach{ $_.SelectNodes("rss/channel/item/enclosure") }|foreach{ "检查 $($_.url.split("/")[-1]),如果它已经存在于 $($destination)" if(!(test-path ($destination + $_.url.split("/")[-1]))){ "Downloading: " + $_.url start -bitstransfer $_.url $destination } } failed with error:无法解析响应内容,因为 Internet Explorer 引擎不可用,或者 Internet Explorer 的首次启动配置未完成。指定 UseBasicParsing 参数并重试。我也试过这个#---设置--- $feedUrl = "https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high" $mediaType = "mp4high" $overwrite = $false $destinationDirectory = join -path ([Environment]::GetFolderPath("MyDocuments")) "OfficeDevPnP" # --- locals --- $webClient = New-Object System.Net.WebClient # --- 函数 --- 函数 PromptForInput ($prompt , $default) { $selection = read-host "$prompt`r`n(default: $default)" if ($selection) {$selection} else {$default} } function DownloadEntries { param ([string]$feedUrl ) $feed = [xml]$webClient.DownloadString($feedUrl) $progress = 0 $pagepercent = 0 $entries = $feed.rss.channel.item.Length $invalidChars = [System.IO.Path]::GetInvalidFileNameChars( ) $feed.rss.channel.item | foreach { $url = 新对象 System.Uri($_.enclosure.url) $name = $_.title $extension = [System.IO.Path]::GetExtension($url.Segments[-1]) $文件名 = $name + $extension $invalidchars | foreach { $filename = $filename.Replace($_, ' ') } $saveFileName = join-path $destinationDirectory $fileName $tempFilename = $saveFilename + ".tmp" $filename if ((-not $overwrite) -and (测试路径 -path $saveFileName)) { write-progress -activity "$fileName 已下载" -status "$pagepercent% ($progress / $entries) 完成" -percentcomplete $pagepercent } else { write-progress -activity "正在下载$fileName" -status "$pagepercent% ($progress / $entries) complete" -percentcomplete $pagepercent $webClient.DownloadFile($url, $tempFilename) rename-item $tempFilename $saveFileName } $pagepercent = [Math]::floor ((++$progress)/$entries*100) } } # --- 做实际工作 --- [string]$feedUrl = PromptForInput "输入提要 URL" $feedUrl [string]$mediaType = PromptForInput "输入媒体type`r`n(options:Wmv,WmvHigh,mp4,mp4high,zune,mp3)" $mediaType $feedUrl += $mediaType [string]$destinationDirectory = PromptForInput "输入目标目录" $destinationDirectory # 如果目标目录没有e xist,创建它 if (!(Test-Path -path $destinationDirectory)) { New-Item $destinationDirectory -type directory } DownloadEntries $feedUrl 错误太多 http://screencast.com/t/bgGd0s98Uc

您的问题的答案在错误消息中(使用 UseBasicParsing 参数)
公平地说,很容易在错误消息中看到“Internet Explorer”字样而忽略消息的其余部分,因为您对命令行实用程序依赖于 IE 的原因盲目愤怒。

s
synth3tk

在您的调用网络请求中,只需使用参数 -UseBasicParsing

例如,在您的脚本(第 2 行)中,您应该使用:

$rss = Invoke-WebRequest -Uri $url -UseBasicParsing

根据 the documentation,此参数在未安装或未配置 IE 的系统上是必需的:

对 HTML 内容使用响应对象,无需文档对象模型 (DOM) 解析。当计算机上未安装 Internet Explorer 时,此参数是必需的,例如在 Windows Server 操作系统的服务器核心安装上。


对于对 UseBasicParsing 正在做什么感兴趣的未来访问者:Uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system. 从此处复制:docs.microsoft.com/de-de/powershell/module/…
我在这台服务器上安装了 Internet Explorer,它已经设置好了。它仍然需要 -UseBasicParsing。不知道为什么。
DonRolling 它可能是运行脚本的不同用户上下文,然后系统发现 IE 未为该用户配置
M
Matt Hyde

要使其在不修改脚本的情况下工作:

我在这里找到了解决方案:http://wahlnetwork.com/2015/11/17/solving-the-first-launch-configuration-error-with-powershells-invoke-webrequest-cmdlet/

错误可能出现,因为 IE 尚未首次启动,弹出下面的窗口。启动它并通过该屏幕,然后错误消息将不再出现。无需修改任何脚本。

https://i.stack.imgur.com/GSsPq.png


为我工作,而无需将所有脚本从 Server 2008 服务器更新到 Server 2012 R2,以便使用 PowerShell 的一系列自动化流程。这使用了一个特定的帐户,所以我确实必须使用该帐户登录到服务器来运行它。
虽然这确实消除了错误,但通过 GUI 解决问题的需要并不是一个可接受的答案,因为 PowerShell 旨在用作自动化平台。此答案不适用于所有场景,例如,在 PowerShell 远程会话中。
Use a Group Policy Object (GPO) to handle the first run wizard:计算机配置 >政策 >管理模板 > Windows 组件 > IE浏览器。将“阻止运行首次运行向导”策略设置为已启用,然后选择适合您的选项。
我在 kubectly 代理上遇到了这个问题,并且在我使用 curl 时解决了这个问题。谢谢!
如果您将应用程序放在某种 Auto Scaling 组中,这种情况也将不起作用,因为正在启动的新实例将需要每天/每周/等等。最好使用 -UseBasicParsing
u
ujeenator

您可以通过运行此 PowerShell 脚本来禁用需要运行 Internet Explorer 的首次启动配置,它将调整相应的注册表属性:

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2

在此之后,WebClient 将毫无问题地工作


不幸的是,这仅适用于 GUI 版本,但不适用于 Windows Server Core。
@Luke 好的,我会尝试寻找 Windows Server Core 的解决方案
P
Peter Stjernholm Meldgaard

这是肯定的,因为 Invoke-WebRequest 命令依赖于 Internet Explorer 程序集,并且正在调用它以按照默认行为解析结果。正如马特建议的那样,您可以简单地启动 IE 并在首次启动时弹出的设置提示中进行选择。您遇到的错误将消失。

但这只有在您以与启动 IE 的 Windows 用户相同的 Windows 用户身份运行您的 powershell 脚本时才有可能。 IE 设置存储在您当前的 Windows 配置文件下。因此,如果您像我一样以 SYSTEM 用户身份在服务器上的调度程序中运行您的任务,这将不起作用。

因此,您必须在此处更改脚本并添加 -UseBasicParsing 参数,如下例所示:$WebResponse = Invoke-WebRequest -Uri $url -TimeoutSec 1800 -ErrorAction:Stop -Method:Post -Headers $headers -UseBasicParsing


E
Eve

在 Windows 10 中,在操作系统安装 Edge 之后(并且根本没有使用 IE——因为许多用户只是在全新安装 Windows 后更喜欢 Chrome),当尝试使用 localhost 从本地运行脚本时

curl http://localhost:3000/

收到相同的错误消息 - 正如 Luis 提到的那样,然后是这个:

+ curl http://localhost:3000/
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotImplement  
        ed: (:) [Invoke-WebRequest], NotSuppor  
      tedException
        + FullyQualifiedErrorId : WebCmdletIED  
       omNotSupportedException,Microsoft.Powe  
       rShell.Commands.InvokeWebRequestComman  
       d

如果你改用

Invoke-RestMethod  http://localhost:3000/

该代码将按预期工作。

我没有尝试复制 Luis 的确切代码,但它仍然是一个对我有用的用例。

另外,因为这个问题是在 5 多年前提出的并且选择了最佳答案,所以我决定仍然在这里为读者提供我的答案,这些读者也可以在其他场景中使用它(例如在 nodejs 中运行代码并打开第二个终端来更快地测试它,而不是打开一个新的浏览器实例)


s
scott_lotus

在您的调用网络请求中,只需使用参数 -UseBasicParsing

例如,在您的脚本(第 2 行)中,您应该使用:

$rss = Invoke-WebRequest -UseBasicParsing

根据文档,此参数在未安装或配置 IE 的系统上是必需的。

对 HTML 内容使用响应对象,无需文档对象模型 (DOM) 解析。当计算机上未安装 Internet Explorer 时,此参数是必需的,例如在 Windows Server 操作系统的服务器核心安装上。


e
eragon2262

我也遇到过这个问题,虽然 -UseBasicParsing 对某些人有用,但如果你真的需要与 dom 交互,它就不起作用。尝试使用组策略来阻止初始配置窗口出现,powershell 将不再阻止您。看这里https://wahlnetwork.com/2015/11/17/solving-the-first-launch-configuration-error-with-powershells-invoke-webrequest-cmdlet/

找到此页面后,我只花了几分钟,一旦设置了 GP,powershell 将允许您通过。


M
Mikey Joel

Windows Server Core(使用 Windows Server 2022 Core 测试):

要求:Windows Server 语言和可选功能 ISO 映像

首先,安装 FOD for Windows Core 并重新启动:

Add-WindowsCapability -Online -Name ServerCore.AppCompatibility~~~~0.0.1.0;

shutdown /r /t 0;

其次,通过安装 Windows Server Languages and Optional Features ISO 映像来安装 IE 11

Add-WindowsPackage -Online -PackagePath "D:\LanguagesAndOptionalFeatures\Microsoft-Windows-InternetExplorer-Optional-Package~31bf3856ad364e35~amd64~~.cab";

使用 Get-PSDrive 确认驱动器号。

或者

按照 Microsoft 的建议安装 Microsoft Edge:

Invoke-WebRequest -UseBasicParsing -Uri "https://c2rsetup.officeapps.live.com/c2r/downloadEdge.aspx?platform=Default&source=EdgeStablePage&Channel=Stable&language=en"  -OutFile "MicrosoftEdgeSetup.exe"

禁用阻止 Invoke-WebRequest 在 PS 上工作的首次启动配置

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2

我需要完成这项工作,因为我的任务是部署具有 AD 域服务角色的 Windows Server Core VM,并使用 StarWars PowerShell 模块,该模块在内部使用 Invoke-WebRequest 而没有 -UseBasicParsing 在它的 API/Downloads 脚本中用于 Star战争广告角色。希望这对其他人有帮助。


r
rayzinnz

另一种解决方法:更新注册表。就我而言,我无法更改 GPO,并且 -UseBasicParsing 会破坏对网站的部分访问权限。另外我有一个没有登录权限的服务用户,所以我无法以用户身份登录并运行 GUI。修理,

以普通用户身份登录,运行 IE 安装程序。然后导出这个注册表项: HKEY_USERS\S-1-5-21-....\SOFTWARE\Microsoft\Internet Explorer 在保存的.reg文件中,将用户sid替换为服务账户sid 导入.reg文件

在文件中


G
GChuf

就我而言,我只需将 curl 命令更改为 Invoke-RestMethod,错误就消失了。


F
FarrukhMalik

只需打开 Internet Explorer,它会显示一个设置弹出窗口,单击使用推荐的设置,确定并关闭它。现在一切都好。