PowerCLI

 View Only
  • 1.  Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 10:33 AM

    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


  • 2.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 10:43 AM

    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



  • 3.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 10:49 AM

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



  • 4.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 10:58 AM

    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?



  • 5.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 11:03 AM

    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.



  • 6.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 11:07 AM

    Are you sure those HRef are valid URI?
    Perhaps display them on screen before calling Invoke-WebRequest



  • 7.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 11:13 AM

    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.



  • 8.  RE: Simple one - trying to download files using Invoke-WebRequest
    Best Answer

    Posted Dec 10, 2019 11:23 AM

    Does it work when you place the $intranetLocation in front?

    $links.href | ForEach-Object { Invoke-WebRequest -Uri "http://$intranetLocation/$_" }



  • 9.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 11:31 AM

    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



  • 10.  RE: Simple one - trying to download files using Invoke-WebRequest

    Posted Dec 10, 2019 11:13 AM

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