Writing stream obtained from Invoke-RestMethod to image file in PowerShell

I have an Asp.Net web API that can return a stream of an image to the client.

public HttpResponseMessage GetImage(string imageId)
{
    ...
    var response = new HttpResponseMessage();
    response.Content = new StreamContent(fileStream);
    return response;
}

      

With the help of a fiddler, I can see that the image was loaded successfully. I want to save it to a local image using PowerShell.

$result = Invoke-RestMethod ...

      

I got $result

it but don't know how to save it to a local drive, I tried the following ways:

# 1
$result | Out-File ".../test.png"

# 2: this exception said the $result is a string, cannot convert to Stream
$fs = New-Object IO.FileStream "...\test.png","OpenOrCreate","Write"
([System.IO.Stream]($result)).CopyTo($fs)

      

What's the correct way to save an image to my local machine using PowerShell?

+3


source to share


1 answer


Use the switch -OutFile

:

$url = "http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b"
Invoke-RestMethod $url -OutFile somefile.png

      



You can also use Invoke-WebRequest

instead Invoke-RestMethod

.

+6


source







All Articles