title: Running Powershell Script from Task Scheduler when User is Logged Off
body: <p>I have a powershell script that I'm intending to run from a remote server. The purpose of the script is to do the following:</p>
  
  <ol>
  <li>Copy an Excel file from a mapped drive to the remote server</li>
  <li>Open the Excel file and run a macro that</li>
  <li>The macro copies an Access table that's on the remote server and pastes it into the Excel file, then does some manipulation of the data</li>
  <li>Saves the Excel file, closes it, and then copies it back to the mapped drive</li>
  </ol>
  
  <p>Right now, I'm testing it on my local machine, so it's copying the Excel file from the mapped drive to my C drive, then grabbing the Access table from a location on my local machine. It runs perfectly when I run it from Powershell. Here is the code:</p>
  
  <pre><code># collect excel process ids before and after new excel background process is 
  # started
  $priorExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
  $Excel = New-Object -ComObject Excel.Application
  $postExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
  
  #run program
  $folderPath = "my folder goes here"
  $filePath = "my folder gooes here\filename.xlsm"
  $tempPath = "C:\Users\Public"
  $tempFile = "C:\Users\Public\filename.xlsm"
  
  #copy file from I drive to remote desktop
  Copy-Item -Path $filePath -Destination $tempPath
  
  #create Excel variables
  $excel = new-object -comobject excel.application
  $excel.visible = $False
  $excelFiles = Get-ChildItem -Path $tempPath -Include *.xls, *xlsm -Recurse
  
  #open excel application and run routine
  Foreach($file in $excelFiles)
  {
      $workbook = $excel.workbooks.open($tempFile)
      $worksheet = $workbook.worksheets.item(1)
      $excel.Run("getAccessData")
      $workbook.save()
      $workbook.close()
  }
  
  #copy file from remote desktop back onto I drive
  Copy-Item -Path $tempFile -Destination $folderPath
  
  # try to gently quit
  $Excel.Quit()
  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
  
  # otherwise allow stop-process to clean up
  $postExcelProcesses | ? { $priorExcelProcesses -eq $null -or $priorExcelProcesses -notcontains $_ } | % { Stop-Process -Id $_ }
  </code></pre>
  
  <p>I need to have the script run once per day in the middle of the night, so I have been working on making it a scheduled task. I first set up the 'Action' with the following information:</p>
  
  <p><strong>Program/Script:</strong>
  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
  <strong>Add arguments (optional):</strong>
  -NoProfile -ExecutionPolicy Bypass -file "C:\Users\nelsonth\emsUpdateDesktop.ps1"</p>
  
  <p>Now, if I run this task while the security option is set to "Run only when user is logged on" and the "Hidden" checkbox selected, then the task runs perfectly. </p>
  
  <p>Since this is going to be running from a remote desktop in the middle of the night, I need the script to run while I am logged off. But when I selected "Run whether user is logged on or not" and "Run with highest privileges" the script no longer runs.</p>
  
  <p>I need this to work so can anyone help me troubleshoot this?</p>