powershell - PowerShell Invoke-WebRequest 链接问题
问题描述
我正在尝试创建一个脚本,它将解析特定 exe 下载链接的 URL 并下载它。我这样做是因为下载链接经常更改。代码下方:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$path = "https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14"
$definitionPath = (Invoke-WebRequest $path).Links |
Where-Object{$_.InnerText -like "*core15sdsv5i64.exe" -and $_.InnerText -notlike "*.jdb"} |
Select-Object -ExpandProperty href
$Output = "C:\temp\virus_definition.exe"
$start_time = Get-Date
Invoke-WebRequest -Uri $definitionPath -OutFile $Output
我收到一个错误,告诉我参数“$definitionPath”为空或 null。任何想法我该如何解决?
谢谢。
解决方案
您必须明确选择您正在执行的属性,但它不能为空,因此您必须捕捉这种可能性。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$path = "https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14"
($definitionPath = (Invoke-WebRequest $path).Links)
<#
# Results
innerHTML : Cookie Policy.
innerText : Cookie Policy.
outerHTML : <a class="banner-policy-link" aria-label=" Cookie Policy." href="https://www.broadcom.com/company/legal/cookie-policy"> Cookie Policy.</a>
outerText : Cookie Policy.
tagName : A
class : banner-policy-link
aria-label : Cookie Policy.
href : https://www.broadcom.com/company/legal/cookie-policy
innerHTML : More Information
innerText : More Information
outerHTML : <a aria-label="More Information" onclick="Optanon.TriggerGoogleAnalyticsEvent('OneTrust Cookie Consent', 'Preferences Cookie Policy');"
href="https://cookiepedia.co.uk/giving-consent-to-cookies" target="_blank">More Information</a>
outerText : More Information
tagName : A
aria-label : More Information
onclick : Optanon.TriggerGoogleAnalyticsEvent('OneTrust Cookie Consent', 'Preferences Cookie Policy');
href : https://cookiepedia.co.uk/giving-consent-to-cookies
target : _blank
innerHTML : <div title="powered by OneTrust" id="optanon-popup-bottom-logo" style='background:
url("https://cdn.cookielaw.org/skins/5.9.0/default_flat_bottom_two_button_black/v2/images/cookie-collective-top-bottom.png"); width: 155px;
height: 35px;' alt="OneTrust Website"></div>
innerText :
outerHTML : <a href="https://onetrust.com/poweredbyonetrust" target="_blank" rel="noopener"><div title="powered by OneTrust" id="optanon-popup-bottom-logo"
style='background: url("https://cdn.cookielaw.org/skins/5.9.0/default_flat_bottom_two_button_black/v2/images/cookie-collective-top-bottom.png");
width: 155px; height: 35px;' alt="OneTrust Website"></div></a>
outerText :
tagName : A
href : https://onetrust.com/poweredbyonetrust
target : _blank
rel : noopener
#>
Try
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$path = "https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14"
($definitionPath = (Invoke-WebRequest $path).Links) |
Where-Object{$_.InnerText -like "*core15sdsv5i64.exe" -and $_.InnerText -notlike "*.jdb"} |
Select-Object -ExpandProperty href
}
Catch {Write-Warning -Message 'The target resource is not found or is blank'}
Finally {Write-Warning -Message 'Some other issue occurred'}
<#
# Results
Some other issue occurred
#>
就个人而言,我会对此进行重构以获得更直接的参考
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$path = "https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14"
($definitionPath = (Invoke-WebRequest $path).Links).href
<#
https://www.broadcom.com/company/legal/cookie-policy
https://cookiepedia.co.uk/giving-consent-to-cookies
https://onetrust.com/poweredbyonetrust
#>
($definitionPath = (Invoke-WebRequest $path).Links).href |
Select-String -Pattern 'cookie'
<#
https://www.broadcom.com/company/legal/cookie-policy
https://cookiepedia.co.uk/giving-consent-to-cookies
#>
推荐阅读
- c - 尝试为 Android 构建 PyQt5 应用程序并获得链接错误:找不到 -lc++
- android - TextField 文本字段在 QML 上不返回值 | QT 5.12
- c++ - 如果变量未初始化,Visual Studio 2017 不会生成任何警告?
- postgresql - PostgreSQL计算其他列具有相同值的ID值
- c# - 如何使用带有 xyz 坐标(RD_NEW)的 .csv 文件并在 Unity 中实现?
- android - OnBackPressedCallback 中的 NavController 出现问题
- android-studio - 我无法从颤振项目构建 APK
- asp.net-mvc - add-migration 'InitialModel' 显示数据库访问错误
- node.js - ip 模块返回不同的 ip 地址
- mysql - 相同的 SQL 语句在两台服务器上执行不同——按日期排序