Lui*_*cia 73 powershell 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{
"Checking $($_.url.split("/")[-1]), we will skip it if it already exists in $($destination)"
if(!(test-path ($destination + $_.url.split("/")[-1]))){
"Downloading: " + $_.url
start-bitstransfer $_.url $destination
}
}
Run Code Online (Sandbox Code Playgroud)
失败了,错误:
无法解析响应内容,因为Internet Explorer引擎不可用,或者Internet Explorer的首次启动配置未完成.指定UseBasicParsing参数,然后重试.
我也试过这个
# --- settings ---
$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
# --- functions ---
function 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 = New-Object System.Uri($_.enclosure.url)
$name = $_.title
$extension = [System.IO.Path]::GetExtension($url.Segments[-1])
$fileName = $name + $extension
$invalidchars | foreach { $filename = $filename.Replace($_, ' ') }
$saveFileName = join-path $destinationDirectory $fileName
$tempFilename = $saveFilename + ".tmp"
$filename
if ((-not $overwrite) -and (Test-Path -path $saveFileName))
{
write-progress -activity "$fileName already downloaded" -status "$pagepercent% ($progress / $entries) complete" -percentcomplete $pagepercent
}
else
{
write-progress -activity "Downloading $fileName" -status "$pagepercent% ($progress / $entries) complete" -percentcomplete $pagepercent
$webClient.DownloadFile($url, $tempFilename)
rename-item $tempFilename $saveFileName
}
$pagepercent = [Math]::floor((++$progress)/$entries*100)
}
}
# --- do the actual work ---
[string]$feedUrl = PromptForInput "Enter feed URL" $feedUrl
[string]$mediaType = PromptForInput "Enter media type`r`n(options:Wmv,WmvHigh,mp4,mp4high,zune,mp3)" $mediaType
$feedUrl += $mediaType
[string]$destinationDirectory = PromptForInput "Enter destination directory" $destinationDirectory
# if dest dir doesn't exist, create it
if (!(Test-Path -path $destinationDirectory)) { New-Item $destinationDirectory -type directory }
DownloadEntries $feedUrl
Run Code Online (Sandbox Code Playgroud)
有太多的错误
Fra*_*sso 159
在您的调用Web请求中,只需使用该参数即可 -UseBasicParsing
例如,在您的脚本(第2行)中,您应该使用:
$rss = Invoke-WebRequest -Uri $url -UseBasicParsing
Run Code Online (Sandbox Code Playgroud)
Mat*_*yde 25
要在不修改脚本的情况下使其工作:
我在这里找到了一个解决方案:http://wahlnetwork.com/2015/11/17/solving-the-first-launch-configuration-error-with-powershells-invoke-webrequest-cmdlet/
错误可能会出现,因为IE还没有第一次启动,提出了下面的窗口.启动它并通过该屏幕,然后错误消息将不再出现.无需修改任何脚本.
uje*_*tor 15
您可以通过运行此 PowerShell 脚本来禁用需要运行 Internet Explorer 的首次启动配置,它将调整相应的注册表属性:
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2
Run Code Online (Sandbox Code Playgroud)
在此之后,WebClient 将正常工作
Eve*_*Eve 12
在 Windows 10 中,操作系统安装 Edge 后(根本不使用 IE,因为许多用户在全新安装 Windows 后更喜欢 Chrome),当尝试使用 localhost 从 localhost 运行脚本时
curl http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)
收到相同的错误消息——正如 Luis 提到的那样,然后是以下一条:
+ 卷曲 http://localhost:3000/ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息:未实现 编辑:(:) [Invoke-WebRequest],NotSuppor tedException + FullQualifiedErrorId : WebCmdletIED omNotSupportedException,Microsoft.Powe rShell.Commands.InvokeWebRequestComman d
如果你使用
Invoke-RestMethod http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)
该代码将按预期工作。
我没有尝试复制 Luis 的确切代码,但它仍然是一个对我有用的用例。
另外,因为这个问题是在 5 年前提出的,并且选择了最佳答案,所以我决定仍然让我的答案放在这里,仅供那些也可以在其他场景中使用它的读者使用(例如,在nodejs 中运行代码并打开第二个终端可以更快地测试它,而不是打开新的浏览器实例)
Pet*_*ard 11
可以肯定,因为Invoke-WebRequest命令依赖于Internet Explorer程序集,并且正在调用它以按默认行为解析结果.正如Matt建议的那样,您可以简单地启动IE并在首次启动时弹出的设置提示中进行选择.而你遇到的错误将会消失.
但是,只有将PowerShell脚本作为与启动IE的用户相同的Windows用户运行,才能实现此目的.IE设置存储在您当前的Windows配置文件下.因此,如果您像我一样在服务器上的调度程序中以SYSTEM用户身份运行您的任务,那么这将无效.
所以在这里你将不得不改变你的脚本并添加-UseBasicParsing参数,就像这个例子一样: $WebResponse = Invoke-WebRequest -Uri $url -TimeoutSec 1800 -ErrorAction:Stop -Method:Post -Headers $headers -UseBasicParsing
小智 6
在您的调用 Web 请求中,只需使用参数 -UseBasicParsing
例如在你的脚本(第2行)中你应该使用:
$rss = Invoke-WebRequest -UseBasicParsing
Run Code Online (Sandbox Code Playgroud)
根据文档,在未安装或配置 IE 的系统上此参数是必需的。
使用 HTML 内容的响应对象,无需文档对象模型 (DOM) 解析。当计算机上未安装 Internet Explorer(例如在 Windows Server 操作系统的服务器核心安装上)时,需要此参数。
Windows Server Core(使用 Windows Server 2022 Core 进行测试):
要求:Windows Server 语言和可选功能 ISO 映像
首先,安装Windows Core 的 FOD并重新启动:
Add-WindowsCapability -Online -Name ServerCore.AppCompatibility~~~~0.0.1.0;
shutdown /r /t 0;
Run Code Online (Sandbox Code Playgroud)
其次,通过安装 Windows Server 语言和可选功能 ISO 映像来安装IE 11 :
Add-WindowsPackage -Online -PackagePath "D:\LanguagesAndOptionalFeatures\Microsoft-Windows-InternetExplorer-Optional-Package~31bf3856ad364e35~amd64~~.cab";
Run Code Online (Sandbox Code Playgroud)
用于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"
Run Code Online (Sandbox Code Playgroud)
禁用阻止 Invoke-WebRequest 在 PS 上工作的首次启动配置
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2
Run Code Online (Sandbox Code Playgroud)
我需要让这项工作正常进行,因为我的任务是部署具有 AD 域服务角色的 Windows Server Core VM,并使用 StarWars PowerShell 模块,该模块在 StarWars 脚本中执行 API/下载时内部使用 Invoke-WebRequest,而不使用 -UseBasicParsing战争广告角色。希望这对其他人有帮助。
归档时间: |
|
查看次数: |
55132 次 |
最近记录: |