0

I am trying to figure out how to get results from a Invoke-WebRequest to download a file from the internet. I wrote a function but it doesn't seem to get the results. StatusCode and StatusDescription never change even if it works:

function DownloadUpgradeTool()
{
##download upgrade tools from MS
Write-Host "Downloading Upgrade Tool from MS."
$url = "https://download.microsoft.com/download/3/d/c/3dcc9642-d3a0-459c-86fd-128f5a0c3cc5/Windows10Upgrade9252.exe"
$output = "c:\Temp\upgrade.exe"
$StatusCode = 1
$StatusDescription = "Error downloading file"
try
    {
    $response = Invoke-WebRequest -Uri $url -OutFile $output -ErrorAction Stop
    }
catch
    {
    $StatusCode = $_.Exception.Response.StatusCode.value__
    $StatusDescription = $_.Exception.Response.StatusDescription
    }
Write-Host "       Download Status Code: " $StatusCode
Write-Host "Download Status Description: " $StatusDescription
if ($StatusCode)
    {
    return $False
    }
else
    {
    return $True
    }
}

function RunUpgrade()
{
##run silent upgrade
##c:\Temp\upgrade.exe /quietinstall /skipeula /auto upgrade /copylogs c:\Temp
}
DuDa
  • 3,718
  • 4
  • 16
  • 36
  • you define the two $Vars in the function and NEVER send them or their content out ... so why do you expect to see that anywhere outside of the function? [*grin*] – Lee_Dailey Feb 01 '21 at 21:26
  • Wow, are you guys in the same classroom? See [this exact same question](https://stackoverflow.com/q/65998880/9898643) – Theo Feb 01 '21 at 21:42
  • If you notice there are two Write-Host statements that display their value. Neither displayed anything [grin] @Theo it is the exact same question. I got my accounts crossed and could never find your response again. Can you delete the first question if not that whole account? –  Feb 01 '21 at 22:22
  • I see, but.. You need to delete that [earlier question](https://stackoverflow.com/q/65998880/9898643) yourself. Posted the answer here aswell – Theo Feb 01 '21 at 22:29

2 Answers2

0

You get the status code when you evaluate the object that is returned by Invoke-WebRequest

$response = Invoke-WebRequest -URI $url -OutFile $output -ErrorAction Stop
Write-Host $response.StatusCode

$r = Invoke-WebRequest -URI https://stackoverflow.com/questions/20259251/
Write-Host $r.StatusCode

https://davidhamann.de/2019/04/12/powershell-invoke-webrequest-by-example/

Powershell implements the OOP (object-oriented programming) design paradigms and semantics i.e. when writing object-oriented code each class has to have a constructor (new()) and a destructor and it should have get() and set() methods to access (read and write) the fields (or attributes) of a class. In ps this is straighlty implemented

Return values of cmdlets are often objects (in the sense of OOP) and you can access the fields of the objects to gather the data ...

It is also possible to use object-oriented design patterns in ps scripting https://dfinke.github.io/powershell,%20design%20patterns/2018/04/13/PowerShell-And-Design-Patterns.html

ralf htp
  • 9,149
  • 4
  • 22
  • 34
0

You need to include -PassThru when you are using -OutFile if you want something in your $response variable.

The exact purpose of PassThru from the PowerShell docs.

Indicates that the cmdlet returns the results, in addition to writing them to a file. This parameter is valid only when the OutFile parameter is also used in the command.

For example,

$response = Invoke-WebRequest -Uri 'www.google.com' 
if ( $response.StatusCode -eq 200 ) 
{ 
   #This bit runs for HTTP success
} 

$response = Invoke-WebRequest -Uri 'www.google.com' -OutFile 'googleHome.html'
if ( $response.StatusCode -eq 200 ) 
{ 
   #This never runs as $response never has a value even though the googleHome.html file gets created
} 

$response = Invoke-WebRequest -Uri 'www.google.com' -OutFile 'googleHome.html' -PassThru
if ( $response.StatusCode -eq 200 ) 
{ 
   #This bit runs for HTTP success and the file gets created
} 
Code39
  • 166
  • 9