Search code examples
vbscriptwmisccm

Need help assigning variables to VBScript WMI array results


I am trying to write a VBScript that pulls 3 different properties from the same WMI class (Win32_DiskDrive). That class gives an array if you have more than one drive. I want to assign all of the results from the array to SCCM variables, but having trouble doing that because I've never done something quite like this before.

Here is my script:

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly       = &h20

Dim objWMIService, objItem, colItems, strComputer, Index, Model, Size
Dim Index(4)
Index(0) = Index0
Index(1) = Index1
Index(2) = Index2
Index(3) = Index3

Dim Model(4)
Model(0) = Model0
Model(1) = Model1
Model(2) = Model2
Model(3) = Model3

Dim Size(4)
Size(0) = Size0
Size(1) = Size1
Size(2) = Size2
Size(3) = Size3

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set env = CreateObject("Microsoft.SMS.TSEnvironment")

For Each objItem in colItems
    Index(4) = objItem.Index
    Model(4) = objItem.Model
    Size(4) = objItem.Size
    env("DriveIndex") = Index(4)
    env("DriveModel") = Model(4)
    env("DriveBytes") = Size(4)
Next

Solution

  • Here's one possible solution that doesn't need arrays in your vbscript. It will output a TS variable that has a suffix of the drive number (starting at 0, or whatever you set iCount to start).

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly       = &h20
    
    Dim objWMIService, objItem, colItems, strComputer
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
    Set env = CreateObject("Microsoft.SMS.TSEnvironment")
    
    iCount = 0
    For Each objItem in colItems
        env("DriveIndex" & iCount) = objItem.Index
        env("DriveModel" & iCount) = objItem.Model
        env("DriveBytes" & iCount) = objItem.Size
        iCount = iCount + 1
    Next
    

    So if you have 3 drives you'll end up with something like this in SCCM:
    DriveIndex0 = TheIndexOf0
    DriveModel0 = TheModelOf0
    DriveSize0 = TheSizeOf0
    DriveIndex1 = TheIndexOf1
    DriveModel1 = TheModelOf1
    DriveSize1 = TheSizeOf1
    DriveIndex2 = TheIndexOf2
    DriveModel2 = TheModelOf2
    DriveSize2 = TheSizeOf2

    You could then reference each variable later in the TS the same way you normally would. %DriveIndex0%,%DriveIndex1%, etc.