0

我正在尝试创建一个脚本,它将解析特定 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。任何想法我该如何解决?

谢谢。

4

1 回答 1

0

您必须明确选择您正在执行的属性,但它不能为空,因此您必须捕捉这种可能性。

[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
#>
于 2020-03-10T15:52:40.330 回答