VMware Cloud Community
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Simple one - trying to download files using Invoke-WebRequest

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

}

I get an error stating "Invoke-WebRequest : Cannot convert 'System.Object[]' to the type 'System.Uri' required by parameter 'Uri'. Specified method is not supported."
Anyone have any idea what I'm missing?
Many thanks,
-Mark
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
9 Replies
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
DCasota
Expert
Expert
Jump to solution

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 $_ }

LucD
Leadership
Leadership
Jump to solution

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

virtualhobbit
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
DCasota
Expert
Expert
Jump to solution

what do you get from ```$links.href | ForEach-Object { echo $_ }``` ?

Reply
0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos