连接到SQL Server要求使用显式凭据启动CMD

时间:2018-06-22 12:24:10

标签: sql-server vbscript

[使用更多测试方案更新了我的查询]

在四种情况下,我正在VBS代码下运行。

连接字符串1:已添加“用户ID = YYDOMAIN \ YYAccount; Password = YYPassword;”并删除了“ Integrated Security = SSPI”

connect.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;" _
"Initial Catalog=reportservertempdb;Data Source=10.10.10.10;Use Procedure for Prepare=1;" _
"Auto Translate=True;Packet Size=4096;Workstation ID=ServerHostName;Use Encryption for Data=False;" _
"Tag with column collation when possible=False;User ID=YYDOMAIN\YYAccount;Password=YYPassword;"

方案1:'用户YYDOMAIN \ YYAccount登录失败''80040E4D''SQL Server的Microsoft OLE DB提供程序'

  1. 我用登录名“ YYdomian \ yyram”登录了加入域“ yydomain.com”的本地计算机
  2. 打开CMD
  3. CScript.exe,然后按Enter
  4. 获取错误'用户YYDOMAIN \ YYAccount登录失败''80040E4D''SQL Server的Microsoft OLE DB提供程序'

方案1的命令行输出如下:

C:\Users\yyram>whoami
YYDOMAIN\yyram

C:\Users\yyram>CScript.exe "C:\Temp\Sql_Connect.vbs"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Temp\Sql_Connect.vbs(8, 1) Microsoft OLE DB Provider for SQL Server: Login failed for user 'YYDOMAIN\YYAccount'.


C:\Users\yyram

方案2:'用户YYDOMAIN \ YYAccount登录失败''80040E4D''SQL Server的Microsoft OLE DB提供程序'

  1. 我用登录名“ YYdomian \ yyram”登录了加入域“ yydomain.com”的本地计算机
  2. 打开CMD“以其他用户身份运行”并提供“用户:YYDOMAIN \ YY帐户和密码:YYPassword”作为详细信息(仅供参考:在SQL查询中使用相同的用户详细信息来连接SQLDB)
  3. CScript.exe,然后按Enter
  4. 获取错误'用户YYDOMAIN \ YYAccount登录失败''80040E4D''SQL Server的Microsoft OLE DB提供程序'

方案2的命令行输出如下:

C:\Users\yyram>whoami
YYDOMAIN\YYAccount

C:\Users\yyram>CScript.exe "C:\Temp\Sql_Connect.vbs"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Temp\Sql_Connect.vbs(8, 1) Microsoft OLE DB Provider for SQL Server: Login failed for user 'YYDOMAIN\YYAccount'.


C:\Users\yyram>

连接字符串2:已删除“用户ID = YYDOMAIN \ YYAccount; Password = YYPassword;”并添加了“ Integrated Security = SSPI”

connect.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;" _
"Initial Catalog=reportservertempdb;Data Source=10.10.10.10;Use Procedure for Prepare=1;" _
"Auto Translate=True;Packet Size=4096;Workstation ID=ServerHostName;Use Encryption for Data=False;" _
"Tag with column collation when possible=False;Integrated Security=SSPI;"

方案3:'用户YYDOMAIN \ yyram的登录失败''80040E4D''用于SQL Server的Microsoft OLE DB提供程序'

  1. 我用登录名“ YYdomian \ yyram”登录了加入域“ yydomain.com”的本地计算机
  2. 打开CMD
  3. CScript.exe,然后按Enter
  4. 获取错误'用户YYDOMAIN \ yyram登录失败''80040E4D''SQL Server的Microsoft OLE DB提供程序'

方案3的命令行输出如下:

C:\Users\yyram>whoami
YYDOMAIN\yyram

C:\Users\yyram>CScript.exe "C:\Temp\Sql_Connect.vbs"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Temp\Sql_Connect.vbs(8, 1) Microsoft OLE DB Provider for SQL Server: Login failed for user 'YYDOMAIN\yyram'.


C:\Users\yyram>

方案4:脚本执行成功

  1. 我用登录名“ YYdomian \ yyram”登录了加入域“ yydomain.com”的本地计算机
  2. 打开CMD“以其他用户身份运行”并提供“用户:YYDOMAIN \ YY帐户和密码:YYPassword”作为详细信息(仅供参考:在SQL查询中使用相同的用户详细信息来连接SQLDB)
  3. CScript.exe,然后按Enter
  4. 报告已成功生成“ C:\ temp \ test.csv”。

方案4的命令行输出如下:

C:\Users\yyram>whoami
YYDOMAIN\YYAccount

C:\Users\yyram>CScript.exe "C:\Temp\Sql_Connect.vbs"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Users\yyram>

那么,有什么方法可以提供另一个Windows身份验证详细信息,而不是要在VBScript之上运行的登录用户详细信息吗?

主要的VBScript代码如下:

Dim connect, sql, resultSet, pth, txt

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set connect = CreateObject("ADODB.Connection")

connect.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;" _
"User ID=YYDOMAIN\YYAccount;Password=YYPassword;Initial Catalog=reportservertempdb;Auto Translate=True;" _
"Data Source=10.10.10.10;Use Procedure for Prepare=1;Packet Size=4096;Workstation ID=ServerHostName;" _
"Use Encryption for Data=False;Tag with column collation when possible=False;Trusted_Connection=True;"

connect.Open

sql="select sql query"

Set resultSet = connect.Execute(sql)

pth = "C:\temp\test.csv"

Set txt = ObjFSO.CreateTextFile(pth, True)

On Error Resume Next
resultSet.MoveFirst
Do While Not resultSet.eof
  txt.WriteLine(resultSet(0) & "," & resultSet(1))
  resultSet.MoveNext
Loop

resultSet.Close
connect.Close
Set connect = Nothing

1 个答案:

答案 0 :(得分:0)

我使用两个批处理文件。

RUNAS_callVBSScript.cmd

RUNAS /savecred /user:Domain\User callVBSScript.cmd

callVBSScript.cmd

.\VBScript.vbs

如果必须更改密码,我必须在第一次运行时输入密码。