首页 > 解决方案 > 从 SQL Server 代理运行 Powershell 脚本(运行其他 exe 文件)

问题描述

我不确定为什么当我在 SQL Server 代理中运行时,这部分 Python 脚本不能完成这项工作。

这个 python 脚本来自“ConvertToBAK.py”。

for f in glob.glob(r'Z:\\TestPCC\\Yesterday\\*.SQB'):  
    os.system( f'SQBConverter "{f}" "{f[:-4]}.bak" {password}' )

所以,我运行 Powershell 来调用这个 Python 文件 (ConvertToBak.py)。

当我在 SQL Server 代理之外运行这个 Powershell 脚本时,一切正常。

我有以下 PowerShell 脚本。

$path = 'Z:\TestPCC'
$file = 'ConvertToBAK.py'

$cmd = $path+"\\"+$file  # This line of code will create the concatenate the path and file
Start-Process $cmd  # This line will execute the cmd 

SQBCoverter 是第三方(RedGate)应用程序的 exe 文件,它基本上将 SQB 文件格式转换为 BAK 文件格式。

使用相同的 Python 代码,SQL Server 代理完成其他任务(例如将文件从这里移动到那里),但它只是跳过/不执行此任务。

有什么我需要添加到 Powershell 或者运行第三方/外部 exe 文件(SQBConverter)的 SQL Server 代理是否存在权限问题?

顺便说一句,我的 SQL Server 代理登录是我自己的帐户,因为使用其他系统帐户,我无法执行 Python 代码中的一些任务。

在此处输入图像描述

我还检查了这个特定 exe 文件的权限问题,并为所有用户授予了完全权限。

标签: pythonpowershellsql-server-agent

解决方案


从您所说的来看,应该没问题,但是如果要确保以适当的用户权限启动脚本,您还可以将相应的凭据直接传递给Start-Process Cmdlet

#Use parameters to obtain username and password, avoid to hard-code them here.

$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))


Start-Process $cmd -Credential ($credentials)

推荐阅读