PowerShell Invoke-WebRequest trouble with links

Occasional Visitor

I'm trying to create a script which will parse an URL for a specific exe download link and download it. I'm doing this because the download link changes frequently. Below the code:






[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





I receive o error telling me that the argument "definitionPath" is empty or null. Any ideas how can i fix that?


1 Reply

@SalahTN the short answer is, it's null because your Where-Object filter is filtering out all of the potential links.


Longer answer:


Stepping through it, this:


(Invoke-WebRequest $path).Links


Returns three links. Two of the links are about the site's Cookie Policy and the third is OneTrust badge of some description, none of these match your filter for the exe file you're looking for.




What you'll find, if you call Invoke-WebRequest and look at the HTML of the page that's returned... most of the content you see in the browser is "inside" a script and PowerShell can't parse the links as it doesn't see them.


One thing you can try is the Selenium PowerShell Module. Note, to use it you do need the beta version, otherwise it won't work with newer versions of Chrome, and this means you may need to update PowerShellGet.


Selenium loads up a full Chrome (or FireFox, etc.) browser, which you can do headless so you don't see it pop up. You should be able to find the link you're looking for. I can't do the full code here, but you'll want to use Find-SeElement, you're looking for an 'a' tag with the text you're looking for.


Once you've found it, you can "click" the link to download it via the browser, or just grab the link and download as you're trying to do.


The first time you do a Selenium script, there's probably a learning curve to it, but it's a *super* powerful toll to add to your toolbox. Browser automation like this opens up a lot of possibilities, I use it for an old RMM tool that doesn't have a nice API.