VBS打开数据库连接通过任务计划通过批处理失败,尝试以任务计划用户而非数据库用户的身份连接

时间:2019-03-18 05:16:33

标签: batch-file vbscript database-connection scheduled-tasks

我有一个任务计划,该计划运行一个批处理文件,该文件调用一个VBS脚本以建立与数据库的连接。

VBS脚本以指定的用户(“用户ID = my_db_user;密码= my_db_pswd;”)打开与数据库的连接。问题是通过任务计划程序运行时,VBS代码中的用户标识和密码被完全忽略,它尝试以任务计划用户“ MYDOMAIN \ MY_TASK_SCH_USER”的身份打开数据库,但数据库中没有此类用户,我们也不想创建一个。

为什么请?

例如:任务计划设置类似于: * 一般 **运行任务时,请使用以下用户帐户:“ MYDOMAIN \ MY_TASK_SCH_USER”(与“作者”不同) **无论用户是否登录运行 **以最高特权运行(在有无此情况下尝试) **配置为:Windows Vista,Windows Server 2008 *动作: **启动程序:C:\ test \ my_batch_file.bat **开始于(可选):C:\ test \

该服务器具有Windows Server 2008 R2 Standard,SP1和64位版本

示例错误:

2019-03-18 14:46:59 - ERROR: Unable to open the database connection.
2019-03-18 14:46:59 - ERROR #1: Error Description: (-2147217843) Login failed for user 'MYDOMAIN\MY_TASK_SCH_USER'.

示例批量(my_batch_file.bat):

cscript C:\test\my_vbs_file.vbs 1>C:\test\test.log 2>&1
TASKKILL /F /IM cmd.exe

示例VBS(my_vbs_file.vbs):

Option Explicit
On Error Resume Next

Dim objConn, objRecSet

' Create database connection
Set objConn   = CreateObject("ADODB.Connection")
If Err.Number <> 0 Then
    ' Error handling
    Wscript.Echo FormatDateTime(Now) & " - ERROR: Unable to create database connection &/or record set."
End If

' Open a connection to the database
objConn.Open _
    "Provider=SQLOLEDB;" & _
    "Data Source=MYDBSERVER;" & _
    "Trusted_Connection=Yes;" & _
    "Initial Catalog=MyDatabase;" & _
    "User ID=my_db_user;Password=my_db_pswd;"
If Err.Number <> 0 Then
    ' Error handling
    Wscript.Echo FormatDateTime(Now) & " - ERROR: Unable to open the database connection."
    Wscript.Echo FormatDateTime(Now) & " - ERROR #1: Error Description: (" & Err.Number & ") " & Err.Description
End If

' Close the database connection & record set
objConn.Close
Set objConn = Nothing

' Returns the given date in the format: "yyyy-mm-dd hh:mm:ss"
Function FormatDateTime( dte_Date )
    FormatDateTime = Year(dte_Date)                  & "-" & _
                     Right("0" & Month(dte_Date) ,2) & "-" & _
                     Right("0" & Day(dte_Date)   ,2) & " " & _  
                     Right("0" & Hour(dte_Date)  ,2) & ":" & _
                     Right("0" & Minute(dte_Date),2) & ":" & _
                     Right("0" & Second(dte_Date),2) 
End Function

0 个答案:

没有答案