Question:
I have a Asp.Net Web API which can return image stream to client.
1 2 3 4 5 6 7 8 |
public HttpResponseMessage GetImage(string imageId) { ... var response = new HttpResponseMessage(); response.Content = new StreamContent(fileStream); return response; } |
With fiddler, I can see the image was downloaded successfully. What I want to achieve is save it to local image with PowerShell.
1 2 |
$result = Invoke-RestMethod ... |
I got the $result
, but don’t know how to save it to local disk, I’ve tried these ways:
1 2 3 4 5 6 7 |
# 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 the right way to save the image to local with PowerShell?
Answer:
Use -OutFile
switch:
1 2 3 |
$url = "http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b" Invoke-RestMethod $url -OutFile somefile.png |
You could also use Invoke-WebRequest
instead of Invoke-RestMethod
.