title: Scheduled Task for robocopy PowerShell script does not finish - what's wrong?
body: <p>I'm working on a PowerShell script to automatically move some files to a remote server every 5 minutes.</p>
  
  <p>The logs are always nested two directories deep.  I don't care about those directories.  Both the folders and the files can be, but are not always, randomly named.  I have used the local temp directory to accomplish the de-nesting prior to shuttling the files over to <code>robocopy</code>.</p>
  
  <p>I've also created a new event log source for troubleshooting.  I'll cut those down after I can get this working.  I believe I'm running this from Task Scheduler correctly (as start a program <code>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</code> with arguments <code>-WindowStyle Hidden -File "C:\PathToScript\ThisIsTheScript.ps1"</code>).</p>
  
  <p>Here's how it should go when automated.</p>
  
  <ol>
  <li>Triggered by Task Scheduler</li>
  <li>Create temp log directory if needed</li>
  <li>De-nest and move logs to temp log directory</li>
  <li>Remove all empty directories from source</li>
  <li><code>robocopy</code> to destination share by UNC name</li>
  <li>Task Scheduler completes task</li>
  </ol>
  
  <p>The files remain in the temp log directory, do not copy to the destination server, and Task Scheduler never completes - it remains running.  However, all of the troubleshooting log entries are created, even if using <code>Start-Job</code> and <code>Wait-Job</code> around the <code>robocopy</code> line.</p>
  
  <p>Steps 5 and 6 are not happening when scheduled, only when run manually.  Why aren't the files copying, and why won't the task complete when run in Task Scheduler?</p>
  
  <pre><code># Source Variables
  $LogSource  = 'C:\Somewhat\Deeper\Directory\Log\Files\Dropped\Here'
  $TempLogDir = 'C:\SomeFilesBrieflyHere'
  
  # Destination variables
  $DestHost   = 'thisisthedestinationcomputername'
  $DestShare  = 'thisisthedestinationsharename$'
  
  # Operating Variables
  $Switches   = @("/s", "/MOVE", "/zb", "/R:0", "/W:0", "/MT:8", "/NFL", "/NDL", "/NP", "/IS", "/IT")
  $RoboDo     = @("$TempLogDir","\\$DestHost\$DestShare","$Switches")
  
  # Verify event log source exists and create it if it does not
  if (-not ([System.Diagnostics.EventLog]::SourceExists("PowerShell Script"))) {
      New-EventLog -LogName 'Application' -Source 'PowerShell Script'
      Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message 'Created new Event Log: Application Source type: PowerShell Script'
  }
  
  # Write log for init
  Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message 'The scheduled task to move logs to the log server has been triggered and will now execute.'
  
  # Verify Local LogDir exists
  if (!(Test-Path -Path $TempLogDir)) {
      New-Item -ItemType directory -Path $TempLogDir
  } 
  
  # De-nest logs to local log folder in preparation to move
  Get-ChildItem -Path "$LogSource" | Get-ChildItem | Get-ChildItem | Move-Item -Destination "$TempLogDir" -Force
  Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message "Files locally de-nested into $TempLogDir."
  
  # Remove all empty directories with unicorn glitter
  Get-ChildItem $LogSource -Recurse | Where {$_.PSIsContainer -and @(Get-ChildItem -LiteralPath:$_.FullName).Count -eq 0} | Remove-Item -Confirm:$false -Force
  Get-ChildItem $TempLogDir -Recurse | Where {$_.PSIsContainer -and @(Get-ChildItem -LiteralPath:$_.FullName).Count -eq 0} | Remove-Item -Confirm:$false -Force
  Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Mes
  
  # Begin Robocopy operation
  robocopy $TempLogDir \\$DestHost\$DestShare $Switches
  Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message "The scheduled task to move logs to the log server has been completed and the files were successfully copied to $DestHost."
  </code></pre>