SQL Server代理未执行Python脚本中运行第三方exe文件的部分

SQL Server代理未执行Python脚本中运行第三方exe文件的部分,python,powershell,sql-server-agent,Python,Powershell,Sql Server Agent,我正在尝试从SQLServer代理中的powershell脚本运行python脚本 我能够执行python脚本(Task1-Task2)的大部分作业,但最后一部分(Task3)除外,它运行一个名为SQBConverter(来自RedGate)的第三方exe文件,该文件将文件从SQB格式转换为BAK格式 当我手动直接运行运行python脚本的powershell脚本时,没有问题 我将“登录身份”从默认(“本地系统”)修改为我自己的(JDoh),它在SQL Server代理中执行powershell

我正在尝试从SQLServer代理中的powershell脚本运行python脚本

我能够执行python脚本(Task1-Task2)的大部分作业,但最后一部分(Task3)除外,它运行一个名为SQBConverter(来自RedGate)的第三方exe文件,该文件将文件从SQB格式转换为BAK格式

当我手动直接运行运行python脚本的powershell脚本时,没有问题

我将“登录身份”从默认(“本地系统”)修改为我自己的(JDoh),它在SQL Server代理中执行powershell,但它只执行该任务,除非它将文件从SQB转换为BAK格式(Task3)

如果不更改为my own(JDoh),它甚至不会执行python脚本的任何作业

我认为powershell脚本端没有任何问题,因为当我将“登录身份”更改为“本地系统”时,它仍然会触发python脚本。它不显示错误,但显示为SQL Server代理作业已完成。但是,它根本不在python脚本中运行任何任务

因此,我猜测这可能与SQL Server代理无法触发/运行SQBConverter exe文件有关

下面是完整的python代码(ConvertToBAK.py),让您了解整个逻辑。它执行所有操作,直到从SQB转换到BAK为止(任务3:最后两行)

这是SQL Server代理步骤的屏幕截图:


我查看了SQBConverter exe文件本身的属性,并对列出的所有用户授予了完全控制权。

我通过修改Python代码的最后一行使其正常工作

发件人:

到(绝对路径):

import os
from os import path
import datetime
from datetime import timedelta
import glob
import shutil
import re
import time, sys

today = datetime.date.today()
yesterday = today - timedelta(days = 1)
yesterday = str(yesterday)

nonhyphen_yesterday = yesterday.replace('-','')
revised_yesterday = "LOG_us_xxxx_multi_replica_" + nonhyphen_yesterday 

src = "Z:\\TestPCC\\FTP"
dst = "Z:\\TestPCC\\Yesterday"
password = "Password"
path = "Z:\\TestPCC\\FTP"
now = time.time()

### Task1:  To delete old files (5 days or older)
for f in os.listdir(path):
  f = os.path.join(path, f)
  if os.stat(f).st_mtime < now - 5 * 86400:
    if os.path.isfile(f):
      os.remove(os.path.join(path, f))

filelist = glob.glob(os.path.join(dst, "*"))
for f in filelist:
    os.remove(f)

### Task2: To move all files from one folder to other folder location
src_files = os.listdir(src)
src_files1 = [g for g in os.listdir(src) if re.match(revised_yesterday, g)]

for file_name in src_files1:
    full_file_name = os.path.join(src, file_name)
    if os.path.isfile(full_file_name):
        shutil.copy(full_file_name, dst)   

### Task3: Convert from SQB format to BAK format (running SQBConverter.exe)     
for f in glob.glob(r'Z:\\TestPCC\\Yesterday\\*.SQB'):  
    os.system( f'SQBConverter "{f}" "{f[:-4]}.bak" {password}' )
$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 
os.system( f'SQBConverter "{f}" "{f[:-4]}.bak" {password}' )
os.system( f'Z:\\TestPCC\\SQBConverter.exe "{f}" "{f[:-4]}.bak" {password}' )