【问题标题】:PowerShell Invoke-WebRequest trouble with linksPowerShell Invoke-WebRequest 链接问题
【发布时间】:2020-03-10 10:16:56
【问题描述】:

我正在尝试创建一个脚本,该脚本将解析特定 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。 有什么想法可以解决吗?

谢谢。

【问题讨论】:

    标签: powershell invoke-webrequest


    【解决方案1】:

    你必须明确地选择你想要的属性,你正在做的,但它不能是空的,所以你必须捕捉到这种可能性。

    [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
    #>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 2020-02-28
      • 2021-07-22
      • 2012-07-26
      相关资源
      最近更新 更多