无法在 VBScript 中打开 SQL 服务器的连接字符串

Can't Open Connection String to SQL Server in VBSscript

我有一个运行以下 sql 查询的 .vbs 脚本: Select COUNT (*) from sys.objects 从 sql 查询输出中计算行数: https://i.stack.imgur.com/wduXW.png[1]

如果找到任何行 (> 0)。使用 SCOM 中的 PropertyBag 脚本运行时在 SCOM 中生成警报。

问题是, 调试脚本时(using cscript),我收到以下错误消息:

(11,1) Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied.

尽管连接字符串似乎是正确的:

strConnection = "Driver={SQL Server};Server=SCOMSRVDB01;Database=DBABee;Trusted_Connection=TRUE"

这是完整的 VBScript:

Dim objCN, strConnection
Dim oAPI, oBag

Set objCN = CreateObject("ADODB.Connection")
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()



strConnection = "Driver={SQL Server};Server=SCOMSRVDB01;Database=DBABee;Trusted_Connection=TRUE"
objCN.Open strConnection

Dim strSQLQuery
strSQLQuery = "Select COUNT (*) from sys.objects"

Dim objRS
Set objRS=CreateObject("ADODB.Recordset")

Set objRS = objCN.Execute(strSQLQuery)

Do Until objRS.EOF
                'WScript.Echo objRS.Fields("No column name")
                if objRS.Fields("No column name") > 0 then
                                                                'WScript.Echo "evaluated as bad"
                                Call oBag.AddValue("State","BAD")
                                Call objAPI.Return(oBag)
                else
                                Call oBag.AddValue("State","GOOD")
                                Call objAPI.Return(oBag)
                end if
                objRS.MoveNext
Loop

objRS.Close

值得一提的是, 在我们公司,如果不提及端口号就无法连接到 sql 服务器。

但是当我尝试在连接字符串中添加它(端口:2880)时:

strConnection = "Driver={SQL Server};Server=SCOMSRVDB01,2880;Database=DBABee;Trusted_Connection=TRUE"

脚本返回以下错误:

(23,17) ADODB.Recordset: Item cannot be found in the collection corresponding to the requested name or ordinal.

The ADODB error indicating that item connect be found 表示您已成功连接到数据库,但找不到您请求的列。这是找不到的:objRS.Fields("No column name")

更改查询并命名列:

strSQLQuery = "Select COUNT (*) as countStuff from sys.objects"

然后更改您要查找的内容:

if objRS.Fields("countStuff") > 0 then