1
votes

I have the following script that uses hardcoded values to check a SQL perfmon counter

    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

but if I attempt to pass some of the values as variables to make the script more flexible/useful as shown below

    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

the objWMIService.ExecQuery(strWMIQuery) returns a null value (although if I echo the query it it seems to match the hardcoded version string)

and the objItem. also seems not to like being given a variable to use

I call the script from the command line using:

cscript perfmon-greater Win32_PerfFormattedData_MSSQLSERVER_SQLServerBufferManager FreeliststallsPersec 0

is what I am attempting possible ? does anyone have an example I could refer to ?

any info greatly appreciated

thanks

2

2 Answers

2
votes

Your problem is objWMIService.ExecQuery sees the string as one parameter.

You could do this:

strWMIQuery = "SELECT * FROM " & strPerfCounterQuery
strWMILang = "WQL"
strWMIFlags = wbemFlagReturnImmediately + wbemFlagForwardOnly

Set colItems = objWMIService.ExecQuery(strWMIQuery, strWMILang, strWMIFlags)
0
votes

You put the variable in triple quotes E.g Execquery ("select * from cim_datafile where drive="""&driveletter&"""")

driveletter is the varaible