;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       WMIQuery() 
;; 
;;ACTION         Queries WMI information from supported systems 
;; 
;;AUTHOR         Radimus 
;; 
;;CONTRIBUTORS   kdyer, Shawn, And Howard 
;;               gbarnas: altered EXECUTE function for NoVarsInStrings support 
;;                        Added support for pre-auth object 
;; 
;;VERSION        2.4.2 - 2007/04/02 
;;                GAB: added pre-authenticated object pointer support 
;;               1.0.0 - 2001/12/22 
;; 
;;SYNTAX         WMIQuery(what, from, [computer], [where], [where_arg], [objAuth]) 
;; 
;;PARAMETERS     what - Required - String 
;;               -  
;; 
;;               from - Required - String 
;;               - Win32 Collection 
;; 
;;               computer - Optional - String 
;;               - defaults to local PC 
;; 
;;               where - Optional - String 
;;               - addl parameter for a 'WHERE' clause. Used with $x 
;; 
;;               where_arg - Optional - String 
;;               - addl parameter for a 'WHERE' clause. Used with $Where 
;; 
;;               objAuth - Optional - Object 
;;               - pre-authenticated token obtained from WMIAuthenticate 
;; 
;;REMARKS        9/2003 - This release alters the return from the function into 
;;               an ARRAY, where previous versions used a pipe '|' delimited strings. 
;;               If you are updating to this version, check your code closely! 
;;               2/2004 - Added support for authentication 
;; 
;;RETURNS        Array 
;; 
;;DEPENDENCIES   WMI 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
;;EXAMPLES       $make  = WMIQuery("Manufacturer","Win32_ComputerSystem")[0] 
;;               $modem = WMIQuery("Description","Win32_POTSModem",$remotePC,"Status","OK")[0] 
;;               for each $stick in WMIQuery("Capacity","Win32_PhysicalMemory") 
;;                 ? val($stick) / 1048576 
;;               next 
; 
Function WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, Optional $pAuth)
 
  Dim $, $sQuery, $objEnum, $sValue, $Tmp, $SystemSet, $objInstance, $objLocator 
 
  $sComputer = Trim(Join(Split($sComputer,'\'),''))
 
  If Not $sComputer Or $sComputer = @WKSTA
    $sComputer = '.'
  EndIf
 
  If Not $root
    $root = '\root\cimv2'
  Endif
 
  $sQuery = 'Select ' + $sWhat + ' From '+ $sFrom
 
  If $sWhere And $x
    $sQuery = $sQuery + " Where " + $sWhere + " = '" + $x + "'"
  EndIf
 
  If $pAuth
    $SystemSet = $pAuth
  Else
    $SystemSet = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $sComputer + $root)
    If @ERROR Or Not $SystemSet
      Exit Val('&' + Right(DecToHex(@ERROR), 4))
    EndIf
  EndIf
 
  $objEnum = $SystemSet.ExecQuery($sQuery)
  If @ERROR Or Not $objEnum
    Exit Val("&" + Right(DecToHex(@ERROR), 4))
  EndIf
 
  For Each $objInstance in $objEnum
    $ = Execute(Chr(36) + 'sValue = ' + Chr(36) + 'objInstance.' + $sWhat)
    If VarType($sValue) & 8192
      $Tmp = $Tmp +'|' + Join($sValue,'|')
    Else
      $Tmp = $Tmp +'|' + $sValue 
    EndIf
  Next
 
  $WMIQuery = split(substr($Tmp,2),'|')
  Exit Val("&" + Right(DecToHex(@ERROR), 4))
 
EndFunction