none
Dynamic wmi function RRS feed

  • Question

  • Hi Guys

    I need help to get the information on one function dynamically "objWMI" contains the information for my wmi query and I need to access to the properti for example "name" that I stored on the variable "sInfo". What will be the way on vbs to access to value on this way.

    sInfo = "Name"

    obj = objWMI.sInfo


    sQuery = "SELECT * FROM Win32_ComputerSystemProduct" 
    
    sToSearch = "Name"
    
    strComputer = "."
    Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    
    GetInfo sQuery, sToSearch
    
    Function GetInfo(sWMI, sInfo) 
     
     MsgBox " querying "  & sWMI  & " " & sInfo
    	 
        Set colWMI = objWMI.ExecQuery(sWMI)
        If Err then
            MsgBox "Error querying " & sWMI & Err.Description & " (" & Err.Number & ")"
        Else
            For Each objWMI in colWMI
                 
    			MsgBox sInfo
    						
    			obj = objWMI.sInfo
    			
    			If not IsNull(obj) then
    			
                    sWMIreturn = Trim(obj)
    				
                    MsgBox "return: " & sWMIreturn 
                End If
            Next
        End if
    	
        GetInfo = sWMIreturn   
    	
        MsgBox "UserExit: SetComputerSystemProduct has been set to " & sWMIreturn
    	
    End Function
    


    Wednesday, February 17, 2016 1:29 PM

Answers

  • We can also do this:

    sQuery = "SELECT * FROM Win32_ComputerSystemProduct" 
    strComputer = "."
    strName = "Name"
    
    Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colWMI = objWMI.ExecQuery(sQuery)
    For Each objWMI in colWMI
        WScript.Echo objWMI.Properties_(strName).Value
    Next



    \_(ツ)_/


    • Edited by jrv Wednesday, February 17, 2016 2:10 PM
    • Marked as answer by robeglez Wednesday, February 17, 2016 2:44 PM
    Wednesday, February 17, 2016 2:10 PM

All replies

  • obj = objWmi.Name

    \_(ツ)_/

    Wednesday, February 17, 2016 2:03 PM
  • You don't have to search for properties they are just part of  the objects.

    sQuery = "SELECT * FROM Win32_ComputerSystemProduct" 
    
    strComputer = "."
    Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colWMI = objWMI.ExecQuery(sQuery)
    For Each objWMI in colWMI
        WScript.Echo objWMI.Name
    Next


    \_(ツ)_/

    Wednesday, February 17, 2016 2:07 PM
  • We can also do this:

    sQuery = "SELECT * FROM Win32_ComputerSystemProduct" 
    strComputer = "."
    strName = "Name"
    
    Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colWMI = objWMI.ExecQuery(sQuery)
    For Each objWMI in colWMI
        WScript.Echo objWMI.Properties_(strName).Value
    Next



    \_(ツ)_/


    • Edited by jrv Wednesday, February 17, 2016 2:10 PM
    • Marked as answer by robeglez Wednesday, February 17, 2016 2:44 PM
    Wednesday, February 17, 2016 2:10 PM