我有以下脚本使用硬编码值来检查SQL perfmon计数器
Dim objWMIService Dim colItems Dim objItem Dim objProperty strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_MSSQLSERVER_SQLServerBufferManager", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each objItem in colItems If objItem.FreeliststallsPersec > 0 Then Wscript.Echo "SERVICE STATUS: Critical" Wscript.Quit(2) Else Wscript.Echo "SERVICE STATUS: OK" Wscript.Quit(0) End If Next
但如果我尝试将一些值作为变量传递,以使脚本更灵活/更有用,如下所示
Dim objWMIService Dim colItems Dim objItem Dim objProperty Dim strPerfCounterQuery Dim strPerfCounter Dim intWarnValue Dim intCritValue Dim strWMIQuery strComputer = "." strPerfCounterQuery = Wscript.Arguments.Item(0) strPerfCounter = Wscript.Arguments.Item(1) intCritValue = Wscript.Arguments.Item(2) Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") strWMIQuery = """SELECT * FROM " & strPerfCounterQuery & """" & ", " & """WQL""" & ", " & "wbemFlagReturnImmediately + wbemFlagForwardOnly" Set colItems = objWMIService.ExecQuery(strWMIQuery) For Each objItem in colItems If objItem.& strPerfCounter > & intCritValue Then Wscript.Echo "SERVICE STATUS: Critical" Wscript.Quit(2) Else Wscript.Echo "SERVICE STATUS: OK" Wscript.Quit(0) End If Next
objWMIService.ExecQuery(strWMIQuery)返回一个空值(尽管如果我回显查询它似乎与硬编码版本字符串匹配)
和objItem。似乎也不喜欢被赋予变量使用
我使用以下命令从命令行调用脚本:
cscript perfmon-greater Win32_PerfFormattedData_MSSQLSERVER_SQLServerBufferManager FreeliststallsPersec 0
是我尝试的可能吗?有没有人有我可以参考的例子?
任何信息非常感谢
感谢
答案 0 :(得分:2)
您的问题是objWMIService.ExecQuery
将字符串视为一个参数。
你可以这样做:
strWMIQuery = "SELECT * FROM " & strPerfCounterQuery
strWMILang = "WQL"
strWMIFlags = wbemFlagReturnImmediately + wbemFlagForwardOnly
Set colItems = objWMIService.ExecQuery(strWMIQuery, strWMILang, strWMIFlags)
答案 1 :(得分:0)
您将变量放在三引号中
例如
Execquery ("select * from cim_datafile where drive="""&driveletter&"""")
driveletter
是可变的