I have a PowerShell script:
$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
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).
EDIT:
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.
2条答案
按热度按时间lndjwyie1#
Essentially the issue is what you've guessed at, a permissions issue related to the file operation. In my case, I was able to create a procedure that could access the file system. I was uncertain where exactly to modify the user account, so can't help you there. However, the common command to access the file system in a procedure is:
Then you just run that procedure in the sql server agent job in tandem with scripts.
rdrgkggo2#
I know this an old thread, but it happened yet again. This time on SQL 2019. Solution was to create a proxy to allow PowerShell to execute with proper permissions.