简体   繁体   English

SQL Server:即使报告成功,SQL Server 代理也不执行 PowerShell 脚本

[英]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.

相关问题 SQL 服务器代理作业未调用 Powershell 脚本 - SQL Server Agent Job is not invoking Powershell Script 即使我在脚本中禁用了计划,SQL Server 代理作业仍在运行 - SQL Server Agent jobs running even though I disabled the schedule in script PowerShell 不从 SQL 服务器执行脚本 - PowerShell not executing script from SQL server 通过SQL Server代理作业使用WinSCP执行REMOTE Powershell脚本时出现问题 - Problem executing REMOTE powershell script using WinSCP via SQL Server Agent Job SQL Server代理未报告PowerShell脚本中未捕获的异常导致的失败 - SQL server agent not reporting failure from uncaught exception in PowerShell script 如何使用 output 和退出代码运行 SQL 服务器代理 Powershell 脚本 - How to run SQL Server Agent Powershell script with output and exit code 无法从SQL Server代理作业运行sharepoint Powershell脚本 - unable to run sharepoint powershell script from sql server agent job 通过SQL Server代理作业通过控制台应用程序执行SSRS报告 - Executing SSRS report via a console application though a SQL Server Agent job 在SQL Server代理中执行SSIS包时出错 - Error executing SSIS package in SQL Server agent 找不到SQL Server代理运行Powershell文件 - SQL Server Agent run powershell file not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM