locked
check if collection is empty/null RRS feed

  • Question

  • Hello Experts,

    I want to collecte the details of HBA from machines where SAN is connected from a central Server. This Server manages all type of machines (virtual, physical and physical which has only Local Disks) and below are the sample codes.

    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM MSFC_FCAdapterHBAAttributes")
    If colItems.count > 0 Then
    Wscript.echo "Connected to SAN"
    Else
    Wscript.echo "Not connected to SAN"
    End If

    These code works well on system which are connected to SAN but on other machine it fails with the following error code.

    c:\Scripts\HBAchk.vbs(5, 1) SWbemObjectSet: Not supported

    and i understand that reason is because colItems is empty or Null.

    I am not getting how can i check it against NULL or empty. i tried following

    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM MSFC_FCAdapterHBAAttributes")
    If colItems Is Nothing Then
    Wscript.echo "Connected to SAN"
    Else
    Wscript.echo "Not connected to SAN"
    End If

    This does not return any error but always goes on Else part write "Not connected to SAN".

    Could someone help me with this?


    -KAKA-



    • Edited by KAKA_2 Tuesday, December 23, 2014 1:46 PM correction
    • Moved by Bill_Stewart Monday, May 18, 2015 4:57 PM This is not "fix script for me" forum
    Tuesday, December 23, 2014 1:43 PM

Answers

  • wscript.echo e, does not print anything.
    -> If e does not return anything then you need to revisit your WMI code. Since I do not currently have access to a SAN I cannot test this part of the code for you.

    Could you explain more on statement "you need to check if the "count" method is valid"?
    -> In your initial post you reported that colItems.count returns an error when no SAN is present. By inserting the line On Error Resume Next we capture this error and process it properly instead of halting the script.

    Putting it in a different way: When colItems.count generates an error we conclude that no SAN is present.


    • Proposed as answer by jrv Tuesday, December 23, 2014 8:17 PM
    • Marked as answer by Just Karl Friday, May 22, 2015 7:48 PM
    Tuesday, December 23, 2014 7:06 PM

All replies

  • Since the collection is neither empty nor null, you need to check if the "count" method is valid, e.g. like so:

    Set objWMIService = GetObject("winmgmts:\\.\root\WMI")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM MSFC_FCAdapterHBAAttributes")
    On Error Resume Next
    e = colItems.count
    If Err.Number = 0 Then
        If e > 0 Then
            WScript.echo "Connected to SAN"
        Else
            WScript.echo "Not connected to SAN"
        End If
    Else WScript.echo "Not connected to SAN"
    End If


    Tuesday, December 23, 2014 2:29 PM
  • Hello Frederik,

    Thank you for your response.

    wscript.echo e, does not print anything.

    i removed the On Error Resume Next from the code you provided and it returns the same error. so i am not convinced if above is the correct way to make it work.

    Could you explain more on statement "you need to check if the "count" method is valid"?


    -KAKA-

    Tuesday, December 23, 2014 4:50 PM
  • wscript.echo e, does not print anything.
    -> If e does not return anything then you need to revisit your WMI code. Since I do not currently have access to a SAN I cannot test this part of the code for you.

    Could you explain more on statement "you need to check if the "count" method is valid"?
    -> In your initial post you reported that colItems.count returns an error when no SAN is present. By inserting the line On Error Resume Next we capture this error and process it properly instead of halting the script.

    Putting it in a different way: When colItems.count generates an error we conclude that no SAN is present.


    • Proposed as answer by jrv Tuesday, December 23, 2014 8:17 PM
    • Marked as answer by Just Karl Friday, May 22, 2015 7:48 PM
    Tuesday, December 23, 2014 7:06 PM