在VBScript中将对象作为参数传递

时间:2009-12-25 04:52:24

标签: performance vbscript wmi disk

我正在开发一个项目,使用VBScript捕获各种磁盘性能指标,并希望使用带有对象作为参数的子过程。在下面的代码示例中,我所指的对象是objitem.AvgDiskQueueLength,它将提供磁盘队列长度的值。我没有找到一种方法使它工作,因为它被识别为一个字符串,然后没有捕获值。我的目标是让任何人只需在一个位置(过程调用参数)进行更改即可轻松更改要捕获的计数器。我这样做的方式可能不是最好的,但我愿意接受建议。子程序调用如下。

PerfCounter "Average Disk Queue Length", "disk_queueLength", "objItem.AvgDiskQueueLength"

以下代码是子程序。

Sub PerfCounter(CounterDescription, CounterLabel, CounterObject)
  Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48) 
  args_index = args_index + 1
  arrCriteria = split(command_line_args(args_index),",")
  strDriveLetter = UCase(arrCriteria(0))
  intCriticalThreshold = arrCriteria(1)
  intWarningThreshold = arrCriteria(2)
  For Each objItem in colItems
    With objItem
    WScript.Echo "objitem.name = " & objitem.name
    If InStr(objItem.Name, strDriveLetter & ":") > 0 Then
      intChrLocation = InStr(objItem.Name, strDriveletter)
      strInstanceName = Mid(objItem.Name, intChrLocation, 1)
    End If
    If strDriveLetter = strInstanceName AND InStr(objItem.Name, strDriveLetter & ":") > 0 Then
      If intActiveNode = 1  OR Len(intActiveNode) < 1 Then
        WScript.Echo "CounterDescription = " & CounterDescription
        WScript.Echo "CounterLabel = " & CounterLabel
        WScript.Echo "CounterObject = " & CounterObject
        If CInt(CounterOjbect) => CInt(intCriticalThreshold) Then 
          arrStatus(i) = "CRITICAL: " & strDriveLetter & ": " &  CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 2
          arrExitCode(i) = intExitCode
        ElseIf CInt(CounterOjbect) => CInt(intWarningThreshold) AND CInt(CounterObject) < CInt(intCriticalThreshold) Then
          arrStatus(i) = "WARNING: " & strDriveLetter & ": " & CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 1
          arrExitCode(i) = intExitCode
        Else
          arrStatus(i) = "OK: " & strDriveLetter & ": " & CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 0
          arrExitCode(i) = intExitCode
        End If
        Else
          PassiveNode CounterDescription, CounterLabel
        End If
      End If
    End With
  Next
  i = i + 1
  ReDim Preserve arrStatus(i)
  ReDim Preserve arrTrendData(i)
  ReDim Preserve arrExitCode(i)
End Sub

2 个答案:

答案 0 :(得分:2)

为什么你不能这样做......

PerfCounter "Average Disk Queue Length", "disk_queueLength", objItem.AvgDiskQueueLength

答案 1 :(得分:0)

要传递对象,您必须传递一个对象,而不是一个字符串。
要使此方法按预期工作,您必须在过程调用之前拥有该对象,但在您的代码示例中,您似乎尝试传递一个您没有的对象。一个工作的例子:

Set objFSO = CreateObject("Scripting.FileSystemObject")
UseFileSystemObject objFSO

Sub UseFileSystemObject( objfso)
 'Now I can use the FileSystemObject in this procedure.
End Sub

但是像这样调用UseFileSystemObject过程是行不通的,

UseFileSystemObject "objFSO"

因为你传入的是字符串而不是对象 我能想到实现你想要的唯一方法是使用select语句来编写对象的适当属性,就像这样。

Call PerfCounter "Average Disk Queue Length", "disk_queueLength", "AvgDiskQueueLength"

Sub PerfCounter(CounterDescription, CounterLabel, CounterObjectAttribute)
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48) 
    For Each objItem in colItems
        Select Case CounterObjectAttribute
            Case "ObjectAttribute1"

            Case "ObjectAttribute2"

            Case "AvgDiskQueueLength"
                Wscript.Echo objItem.AvgDiskQueueLength
        End Select
    Next
End Sub

因此在select中你必须为每个可以使用的属性添加一个case,但它允许你将一个字符串传递给过程。我可能会对此不以为然,但如果您没有首先使用该对象,我不知道如何传递对象。