title: Powershell running invoke-command on several servers using -Asjob but log completion log locally
body: <p>I am trying to run a powershell script that installs some software on a bunch of remote servers.
  I am using the -Asjob option to run them synchronously.
  I'm also using for loop to run the remote commands on each server, but i want to write a &quot;Done&quot; log file <strong>locally where i am running the script</strong> to notify me exactly when each server completes the commands.</p>
  <p>This is the sample code i am testing, and the script runs fine, but the &quot;Done&quot; log files gets generated immediately, and not as each server finishes.</p>
  <pre><code>$VerbosePreference = 'Continue'
  $servers = Get-Content -Path f:\temp\servers.txt
  
  foreach($server in $servers) {
      Write-Verbose &quot;Start batch file as a job on $server&quot;
      Start-Sleep -Seconds 3
      Invoke-Command -ComputerName $server -ScriptBlock {
      echo testfile1 &gt; f:\temp\testfile1.txt
      Start-Sleep -Seconds 20
      echo testfile2 &gt; f:\temp\testfile2.txt
      Start-Sleep -Seconds 20
      echo testfile3 &gt; f:\temp\testfile3.txt
      echo DONE} &gt; f:\temp\$server.done.txt -Asjob
  } 
  </code></pre>
  <p>thanks</p>