title: Why do I get different output in Powershell from start-job vs just running the code?
body: <p>The script runs correctly when outside of Start-Job but when in a scriptblock I get incorrect results. Where am I going wrong?</p>
  
  <p>I need the Start-Job functionality since I have servers where the remote commands will hang (separate problem - WMI is borked) and I need to timeout and move to the next server.</p>
  
  <p>I've tried every variation I can find in Google and still don't have the results I'm looking for.</p>
  
  <p>I am really at my wits end with this as I don't understand what is happening... Help?</p>
  
  <p>Thanks!</p>
  
  <pre><code>$timeoutSeconds = 90
  
  ForEach($server in $servers) {
      #$ErrorActionPreference = "inquire"
      #$WarningPreference = "inquire"
  
      $ErrorActionPreference = "silentlycontinue"
      $WarningPreference = "silentlycontinue"
  
      write-host $SERVER
  
      $code = {
          param($SERVER,$LOGto,$outputPath)
  
          $ping = (Test-Connection -ComputerName $SERVER -Count 2 -Quiet )
          if($ping -eq $true)
          {
              $pingVerbose = (Test-Connection -ComputerName $SERVER -Count 1)
              $IP = $pingVerbose.IPV4Address.IPAddressToString
  
              $osname2 = (Get-WMIObject -computerName $SERVER win32_operatingsystem).name
              if($osname2 -match "|") 
              {
                  $osname,$osname1 = $osname2.Split('|')
              } else {
                  $osname = $osname2
              }
  
              $lastinstalled = (Get-HotFix -computerName $SERVER | where -property InstalledOn -ne $null)
              if($lastinstalled.InstalledOn)
              {
                  $lastinstalledOn1 = ($lastinstalled.InstalledOn | Sort-Object -Property InstalledOn )[-1]
                  $lastinstalledOn = $lastinstalledOn1
              }
  
              $lastQFE = (get-wmiobject -class win32_quickfixengineering -computerName $SERVER | where -property InstalledOn -ne $null)
              if($lastQFE.InstalledOn -ne $null)
              {
                  $lastQFEon = ($lastQFE.InstalledOn | Sort-Object -Property InstalledOn)[-1]
                  $lastQFEon = $lastQFEon
              }
  
              if(($lastinstalledOn) -or ($lastQFEon))
              {
                  if(($lastinstalledOn) -and ($lastinstalledOn -gt $lastQFEon)) 
                  {
                      $installedOn = $lastinstalledOn.tostring("MM/dd/yyyy")
                      $HotFixNumber = ($lastinstalled.HotFixID | Sort-Object -Property HotFixID)[-1]
                  } else {
                      $installedOn = $lastQFEon.tostring("MM/dd/yyyy")
                      $HotFixNumber = ($lastQFE.HotFixID | Sort-Object -Property HotFixID)[-1]
                  }
              } else {
                  $installedOn = ''
                  $HotFixNumber = ''
              }
          }
  
      #add entries to the log file
      ac $outputPath\$LOGto "$Server,$ip,$installedOn,$HotFixNumber,$ping,$osname "
      Write-Host "$Server,$ip,$installedOn,$HotFixNumber,$ping,$osname "
  
      }
  
      $runCode = Start-Job -ScriptBlock $code -ArgumentList $server,$LOGto,$outputPath
  
      if(Wait-Job $runCode -Timeout $timeoutSeconds) 
      {
          Receive-Job $runCode
      } else {
          Remove-Job -Force $runCode
          ac $($outputPath + "\error.txt")  "$Server"
      }
  }
  </code></pre>
  
  <p>When running in the scriptblock I receive the wrong date and KB.</p>
  
  <pre><code>SERVERNAME
  SERVERNAME,10.1.XX.XX,03/13/2015,KB3022777,True,Microsoft Windows Server 2012 R2 Standard 
  
  vs.
  
  SERVERNAME
  SERVERNAME,10.1.XX.XX,05/15/2017,KB4012213,True,Microsoft Windows Server 2012 R2 Standard
  </code></pre>