-5

OK. I am new to powershell but this is killing me. The below code never returns either a status code or a status description. I hace even tried it with a try and catch. What am I doing wrong?

    $url = "https://download.microsoft.com/download/3/d/c/3dcc9642-d3a0-459c-86fd- 
    128f5a0c3cc5/Windows10Upgrade9252.exe"
    $output = "c:\Temp\upgrade.exe"
    $Result = Invoke-WebRequest -Uri $url -OutFile $output -ErrorAction Stop
    $StatusCode = $Result.StatusCode
    $StatusDescription = $Result.StatusDescription
    Write-Host "       Download Status Code: " $StatusCode
    Write-Host "Download Status Description: " $StatusDescription
Ziggy32
  • 11
  • 6
  • 1
    I've already answered this twice [here](https://stackoverflow.com/questions/65998880/struggling-with-invoke-webrequest-return-value) and [there](https://stackoverflow.com/questions/66000276/how-do-i-get-return-values-from-invoke-webrequest/66001025#66001025), but you then delete the question and even the account under which you posted the same question.. What's the deal here? – Theo Feb 05 '21 at 20:14
  • I hate to say this but no. It was never answered. It was danced around, ducked , bobbed and weaved but never answered. Just like this one. Alternate methods were give but the question, as it was asked, was not. – Ziggy32 Feb 06 '21 at 22:56
  • _never answered_ ?? I'm afraid you still have a lot to learn about `-PassThru` and when to use `-ErrorAction Stop`. Did you even try the code given? – Theo Feb 07 '21 at 14:38
  • Code given? Where????? – Ziggy32 Feb 08 '21 at 16:39
  • If you ever care to scroll down and look at the [answer](https://stackoverflow.com/a/66001025/9898643) **below** your question, you will find that code. – Theo Feb 09 '21 at 10:33
  • That isn't code. Nothing about it answers the question. – Ziggy32 Feb 18 '21 at 11:50
  • Have you read yor **own** comments under [Francesco's answer](https://stackoverflow.com/a/66069964/9898643) There you even doubt if `-PassThru` is the way to go. Please leave – Theo Feb 18 '21 at 13:20

1 Answers1

1

Invoke-WebRequest by default returns the response object.

If you use the -OutFile option, response is saved to the given file and the cmdlet doesn't return anything. Thus $Result is null and you don't get any status code.

If you want to save the response object and save the response into a file, you have to use the -PassThru switch. Om this case $Result is an object of type WebResponseObject.

For the Invoke-WebRequest cmdlet the -PassThru swicht is only valid when using the -OutFile options

See the Documentation


A Note: the -PassThru switch is a common pattern for cmdlet and is usually very well documented

Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
  • I appreciate the feedback but it doesn't really answer the question does it? I mean I am trying to figure why the above code doesn't show any responses. Is the code wrong? is the Write-Host wrong? – Ziggy32 Feb 06 '21 at 21:18
  • I does exactly respond to the question. Did you try it? If you don't use `-PassThru` you don't get the response back and thus you don't get the `StatusCode` Anyway I've reworded a bit the answer – Francesco Montesano Feb 06 '21 at 21:26
  • Sorry but all the above does is to tell me to put a -PassThru option. Nowhere does it explain how to get the results. I have googled it until my fingers hurt and the information is just not there. How about going a little further and show how to capture and display the results? – Ziggy32 Feb 13 '21 at 16:11