Combined the functionality of Radimus' WMIQuery with Jens' WMIAuthenticate for a recent project - had to query non-domain systems with unique credentials. Here's the result.
It adds 2 optional args to the WMIQuery for the userID and Password. WMIAuthenticate code has basically replaced the original object creation code in WMIQuery, and it's varnames have been updated to match WMIQuery's use.
Thanks to the original code authors - I just joined the "peanut butter" with the "chocolate".
BLACK = Radimus' original code
BLUE = Jens' transplanted code (with vars renamed)
RED = Glenn's additions
Code:
;FUNCTION WMIQuery
;
;ACTION Queries WMI information from supported systems
;
;AUTHOR Radimus - Base code
; Jens Meyer (sealeopard@usa.net) - Authentication
; Glenn Barnas - merge base & authentication code
;
;CONTRIBUTORS kdyer, Shawn, And Howard
;
; gbarnas: expanded Loop & If statements (improve clarity while evaluating code)
; altered EXECUTE function for NoVarsInStrings support
; added SNVerify() for FRIT environment
; merged authentication support from Jens' fnWMIAuthenticate UDF
;
;VERSION 2.4.2
;
;DATE CREATED 12/22/2001
;
;DATE MODIFIED 09/23/2003
;DATE CONVERTED 02/26/2004
;
;KIXTART 4.x
;
;SYNTAX WMIQuery($what,$from,optional $computer,optional $where, optional $x, Optional $UID, Optional $Pwd)
;
;PARAMETERS $what
;
;
; $from
; - Win32 Collection
;
; optional $computer
; - defaults to local PC
;
; optional $where
; - addl parameter for a 'WHERE' clause. Used with $x
;
; optional $x
; - addl parameter for a 'WHERE' clause. Used with $Where
;
; optional $fUserID
; - User ID for when authentication is needed
;
; optional $fUserPW
; - User Password for when authentication is needed
;
;
;RETURNS Array
; @error 1 = Cannot create COM object on target PC
;
;REMARKS This is chage alters the return from the function into an ARRAY, where the previous version
; was a pipe '|' delimited string. If you are updating to this version, check your code closely
;
;DEPENDENCIES kix 4.x+, WMI
;
;EXAMPLE $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
;
;KIXTART BBS http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000117
; http://download.microsoft.com/download/platformsdk/wmicore/1.5/W9XNT4/EN-US/wmicore.EXE
Function WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, $sUserID, $sUserPW)
Dim $sQuery, $objEnum, $sValue, $sItem, $Tmp, $SystemSet
Dim $, $objInstance, $Dollar, $oCmd, $objLocator, $objWBEM
$Dollar = Chr(36)
If Not $sComputer
$sComputer = "."
EndIf
;if instr($sComputer,'\')
; $sComputer=Right($sComputer,instrrev($sComputer,'\'))
;Endif
$sComputer = SNVerify($sComputer, 1)
$root = trim($root) ; GAB added
if not $root
$root="\root\cimv2"
Endif
$sQuery = "Select " + $sWhat + " From "+ $sFrom
If $sWhere AND $x
$sQuery = $sQuery+" Where "+$sWhere+" = '"+$x+"'"
EndIf
; this is the original Object definition
; $SystemSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $sComputer + $root)
; Heavy mods here to integrate WMIAuthenticate() functionality =======================
; Jens' code has been transplanted, and integrated with appropriate varname changes
; check to see whether we're connecting to a local or remote computer
$sComputer = trim($sComputer)
select
case $sComputer = @WKSTA
$sComputer = '.'
case $sComputer
case 1
$sComputer='.'
endselect
select
case $sUserID and $sComputer <> '.'
; create locator object for connection to a remote computer
$objLocator = CreateObject('WbemScripting.SWbemLocator')
if @ERROR
exit @ERROR
endif
; create an credentialed (username/password provided) connection to a remote computer
$SystemSet = $objLocator.ConnectServer($sComputer, $root, $sUserID, $sUserPW)
if @ERROR
exit @ERROR
endif
; set the impersonation level
$SystemSet.Security_.ImpersonationLevel = 3
if @ERROR
exit @ERROR
endif
case 1
;set the impersonation level
$SystemSet = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $sComputer + $root)
if @ERROR
exit @ERROR
endif
endselect
; original code resumes ==============================================================
If @ERROR
Exit Val("&" + Right(DecToHex(@ERROR), 4))
EndIf
$objEnum = $SystemSet.ExecQuery($sQuery)
If @ERROR
Exit Val("&" + Right(DecToHex(@ERROR), 4))
EndIf
For Each $objInstance in $objEnum
If $objInstance
$oCmd = $Dollar + 'sValue = ' + $Dollar + 'objInstance.' + $sWhat
$ = Execute($oCmd)
if VarType($sValue) & 8192
For Each $sItem in $sValue
$Tmp = $Tmp + '|' + Trim($sItem)
Next
else
$Tmp = $Tmp + '|' + Trim($svalue)
Endif
EndIf
Next
$WMIQuery = Split(SubStr($Tmp,2), '|')
Exit Val("&" + Right(DecToHex(@ERROR), 4))
EndFunction