[英]SQL Server: SQL Server Agent not executing PowerShell script even though a success is reported
I have a PowerShell script:我有一个 PowerShell 脚本:
$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.如果我在 PowerShell 终端中逐行执行它,它会执行它应该执行的操作。 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.
当我将其复制粘贴到 SQL Server 代理中的作业步骤并运行该作业时,报告成功,但我看不到任何应使用该脚本创建的文件。 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.
如果 SQL Server 而不是我使用 PowerShell 写入文件,访问将被拒绝。 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.
并且它被认为是成功的运行仅仅是因为 bcp 错误不被视为脚本执行错误。 The history of the job contained useful info.
作业的历史记录包含有用的信息。
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:
但是,在过程中访问文件系统的常用命令是:
EXEC xp_cmdshell 'DEL E:\folder\anotherfolder\*.txt'
Then you just run that procedure in the sql server agent job in tandem with scripts.然后,您只需在 sql server 代理作业中与脚本一起运行该过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.