title: Powershell: $LASTEXITCODE in a function
body: <p>Hi I'm noticing some odd behavior with the following code snippet</p>
  
  <pre><code>function test
  {
      $LASTEXITCODE = $null
      ping asdfs
      Write-Host "Last exitcode: $LASTEXITCODE"
  }
  
  test
  Write-Host "Last exitcode: $LASTEXITCODE"
  </code></pre>
  
  <p>The output from this is </p>
  
  <pre><code>Ping request could not find host asdfs. Please check the name and try again.
  Last exitcode: 
  Last exitcode: 1
  </code></pre>
  
  <p>Why is $LASTEXITCODE not set within the test() function?</p>
  
  <p>This is a generalization of a problem I'm having right now, when I call a Win32 .exe from within a function and the $LASTEXITCODE isn't returning the value I'm expecting from within a function</p>