title: How to get StatusCode from request in PowerShell
body: <p>I need to get success and/or error status code from a request using power shell. And i'm always getting a blank status.</p>
  
  <p>I have tried with Invoke-WebRequest and Invoke-RestMethod.
  I have sucess on the call but can't find a way of getting the status code.</p>
  
  <p>Here how is currently written:</p>
  
  <pre><code>$resource = "some url"
  $Logfile = "C:/path/log.log"
  
  function LogWrite
  {
      Param([string]$logstring)
  
      Add-content $logfile -value $logstring
  }
  
  Try
  {
      $Response = Invoke-WebRequest -Method Post -Uri $resource 
      Write-Output("Success.")
      LogWrite $Date
      LogWrite SuccessOnCall
      LogWrite  $Response.StatusCode
  }
  Catch
  {
      $ErrorMessage = $_.Exception.Message
      Write-Output($ErrorMessage)
      $FailedItem = $_.Exception
      Write-Output($FailedItem)
      LogWrite $Date
      LogWrite ErrorOnCall
      LogWrite $ErrorMessage
      Break
  }
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>LogWrite "StatusCode:" $Response.Exception.Response.StatusCode.value__ 
  
  </code></pre>
  
  <p>I used this question (and other links) : <a href="https://stackoverflow.com/questions/38622526/invoke-restmethod-how-do-i-get-the-return-code">Invoke-Restmethod: how do I get the return code?</a></p>
  
  <p>Trying to solve this, my log does write "SuccessOnCall" but the StatusCode is blank.</p>
  
  <p>Thank you.</p>