title: SQL Server: SQL Server Agent not executing PowerShell script even though a success is reported
body: <p>I have a PowerShell script:</p>
  
  <pre><code>$date = (Get-Date -Format "yyyy_MM_dd_HH_mm_ss")
  $file_name_headers= "path_string_"+$date+".csv"
  $file_name_data = "path_string_"+$date+".csv"
  bcp "SELECT COLUMN_NAME FROM db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'name1' AND TABLE_SCHEMA='dbo'" queryout $file_name_headers -S servername -t "," -T -c
  bcp "db.dbo.name1" out $file_name_data -S servername -t "," -T -c
  </code></pre>
  
  <p>If I execute it line by line in PowerShell terminal it does what it is supposed to do. When I copy-paste it to a job step in SQL Server Agent and run the job, a success is reported but I cannot see any files that should be created with the script. I have successfully run other Agent jobs (executing stored procedures).</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Permission issues. If the SQL Server rather than me uses the PowerShell to write to a file, the access is denied. I can use it to write to locations with sufficient permissions. And it was considered as a successful run simply because the bcp errors are not considered as script execution errors. The history of the job contained useful info.</p>