Morning gang.
I'm struggling to download files from a web server when it matches a pattern. Here's what I'm using:
$intranetLocation="intranet.hobbitcloud.com"
$horizonVersion="*7.10*"
$URI = (Invoke-WebRequest -URI "http://$intranetLocation").Links.href | Where-Object {$_ -like $horizonVersion}
ForEach ($file in $URI)
{
echo $file
Invoke-WebRequest -URI $URI -Outfile $file -PassThru
}
Does it work when you place the $intranetLocation in front?
$links.href | ForEach-Object { Invoke-WebRequest -Uri "http://$intranetLocation/$_" }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So I'm working the problem, and I know it's because I'm passing the wrong variable (a System.Array instead of a System.Object).
I got this by running:
$URI.GetType()
The journey continues...
-Mark
According to https://stackoverflow.com/questions/45874395/download-links-from-page-with-powershell "Invoke-WebRequest –Uri $Links.HRef - you can see in the help -Uri <Uri> that means it accepts a single one."
Their solution was to use $links.href | foreach-object { invoke-webrequest -Uri $_ }
I assume that $URI is an array after the 1st Invoke-WebRequest, and on the 2nd Invoke-WebRequest you are then passing an array instead of a single URI.
Shouldn't you be using the loop variable $file in te 2nd Invoke-WebRequest?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, pretty much. I think that's where I'm going wrong.
The SO suggestion above looks good. I'm now going with:
$url="http://intranet.hobbitcloud.com"
$links = ((Invoke-WebRequest –Uri $url).Links | Where-Object {$_.href -like “*7.10*”} )
$links.href | ForEach-Object { Invoke-WebRequest -Uri $_ }
But I'm getting:
Invoke-WebRequest : No such device or address
At line:1 char:32
+ $links.href | ForEach-Object { Invoke-WebRequest -Uri $_ }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: GET, Reques\u2026PowerShell/6.2.3
}:HttpRequestMessage) [Invoke-WebRequest], HttpRequestException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Invoke-WebRequest : No such device or address
Meh.
Are you sure those HRef are valid URI?
Perhaps display them on screen before calling Invoke-WebRequest
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
what do you get from ```$links.href | ForEach-Object { echo $_ }``` ?
No this is where it's going wrong.
The href are just the files names - not the full link. So Invoke-WebRequest doesn't know where they are 😞
Needs more work.
Does it work when you place the $intranetLocation in front?
$links.href | ForEach-Object { Invoke-WebRequest -Uri "http://$intranetLocation/$_" }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Boom! It does. I had to add the -Outfile param, but yes... all good.
Complete and working solution:
$url="http://intranet.hobbitcloud.com"
$links = ((Invoke-WebRequest –Uri $url).Links | Where-Object {$_.href -like “*7.10*”} )
$links.href | ForEach-Object { Invoke-WebRequest -Uri "$url/$_" -Outfile $_ }
Many thanks LucD and DCasota!
-Mark