Page 1 of 2 12>
Topic Options
#115190 - 2004-02-28 05:45 PM WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
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



_________________________
Actually I am a Rocket Scientist! \:D

Top
#115191 - 2004-02-28 05:54 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You might want to rename the UDF to WMI_Auth_Query or similar as your WMIQuery is not backwards compatible with the original WMIQuery.
_________________________
There are two types of vessels, submarines and targets.

Top
#115192 - 2004-02-28 06:21 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
I debated that, actually, but it is backward compatible. I have three (OK - not exactly an exhaustive test) other tools that used WMIQuery, and after re-genning those to use the updated WMIQuery version, they all ran with no changes.

In fact, the orignial statement was copied and pasted as a comment just below the new statement that executes when no authentication is needed - the statement is identical to the original code.

As far as creating a new UDF - Since the original WMIQuery is Radimus' code - I'll leave it up to him to decide if he wants to integrate this moving forward or have this be a seperate UDF. Fair? Your original UDF is still viable as a standalone for other code, and adds value to this one when integrated.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115193 - 2004-02-28 07:40 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Glenn:

If I read this correctly, your output is an array, while Radimus returns a string. This does not make it backward compatible to me.
_________________________
There are two types of vessels, submarines and targets.

Top
#115194 - 2004-02-28 09:30 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Jens:

I did not write any parts of this.. I simply took the latest WMIQuery from the UDF section, commented out the part that instantiated the object, and then added in the parts of your UDF that instantiated the object based on whether security was needed or not.

Look at the latest WMIQuery on the board - it's version 2.4.

I added the .2 version designation based on including your code segment, opened up the one-line IF statements to make them easier (for me) to follow, and added the reference to SNVerify(), which is our standard UDF for formatting server names - adding SNVerify is probably the most significant change I made.

I did not make any changes to the output of the UDF in any way. I believe that the change from a "|" delimited string to an array were made by Radimus some time ago, as his note indicates.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115195 - 2004-02-28 10:15 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
as far as I know, the wmiquery by rad does return arrays.

glenn, you could change the function-post title though.
_________________________
!

download KiXnet

Top
#115196 - 2004-02-29 02:43 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
hmm, I read the REMARKS section so that you made the change and that Radimus' version returns a string with the pipe character as delimiter.
_________________________
There are two types of vessels, submarines and targets.

Top
#115197 - 2004-02-29 04:46 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Arrgh!

I've color-coded the post to clearly show that the BLACK text is Radimus' original code, the BLUE text is what I copied from Jens' WMIAuthenticate UDF (I did not show every varname change, however), and the RED shows what I added. I did not modifiy either original UDF except to match the var names as noted above.

Lonk - I'd like to hear from Radimus as to wether he'd like to integrate this into his code moving forward, or maintain it as a separate UDF, at which time I'll rename it. Personally, I think it makes more sense to have one, integrated UDF, since his functionality is enhanced but fully backward compatible. Maybe he'll check in during a midnight feeding.

If you want to move this to the SCRIPTS forum until then, that's fine with me.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115198 - 2004-02-29 05:37 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
nah, not wanting to move anywhere.
I just want you to change the title

now it's named as "WMIQuery + WMIAuthenticate = WMI_Auth_Query!" and you know udf names can't have control characters nor whitespaces.
how it looks in the collection listing you see at:
http://www.gwspikval.com/jooel/UDF/member/6673.htm
_________________________
!

download KiXnet

Top
#115199 - 2004-02-29 06:27 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
If you haven't posted a seperate thread for this, I can integrate it. I did see a spot that could be integrated a touch better...

I don't know if this board software will allow me to edit the original post, but I'll give it a shot.
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#115200 - 2004-02-29 09:42 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
wanna try this one...

Code:
 

FUNCTION WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, Optional $sUserID, Optional $sUserPW)
Dim $sQuery, $objEnum, $sValue, $TMP, $SystemSet, $, $objInstance, $objLocator

if instr($sComputer,'\')
$sComputer = right($sComputer,instrrev($sComputer,'\'))
Endif
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 $sUserID and $sUserPW and $sComputer <> '.'
$objLocator = CreateObject('WbemScripting.SWbemLocator')
If @ERROR or not $objLocator Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet = $objLocator.ConnectServer($sComputer, $root, $sUserID, $sUserPW)
If @ERROR or not $SystemSet Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet.Security_.ImpersonationLevel = 3
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)
$tmp = $tmp +'|' + iif(VarType($sValue) & 8192,join($sValue,'|',ubound($sValue)),$sValue)
Next
$WMIQuery = split(substr($tmp,2),'|')
Exit VAL("&"+Right(DecToHex(@ERROR),4))
ENDFUNCTION



Edited by Radimus (2004-02-29 09:44 AM)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#115201 - 2004-02-29 02:16 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Radimus:

This looks good - will test it today.

If you are OK supporting these changes in your WMIQuery UDF, I'd just as soon see you update yours with this enhancement and I'll delete this post entirely. I do have just one question, though...

What's with the weird error return values in hex?
(a comment there might be nice!)

Jens:

You might explain why you Trim() the optional args, and maybe Radimus will add them to the top of the UDF if he thinks it's reasonable.

Thanks!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115202 - 2004-02-29 04:00 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Habit
_________________________
There are two types of vessels, submarines and targets.

Top
#115203 - 2004-02-29 04:26 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
the weird hex @error statements were pointed out to me to convert WMI or COM error numbers into the equivalent Kix error code values.

personally, I;d just pull all the error trapping out, except for just before the the for each statement. I typically don't need to know why it failed, just that it did. since that I know that this UDF is pretty much 100% if there is a problem it is usually with the OS on the target machine.
However, since putting in the authentication model, there is a point of failure, so that only leaves 1 possibly unnecessary statement, so it may as well be left in.

if It all works and tests out, I may Lonk it and strip all the vars down to 2 chars and make it completely unreadable, but more compact. I am quite proud of this line:
$tmp = $tmp +'|' + iif(VarType($sValue) & 8192,join($sValue,'|',ubound($sValue)),$sValue)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#115204 - 2004-02-29 05:25 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Quote:

if It all works and tests out, I may Lonk it and strip all the vars down to 2 chars and make it completely unreadable, but more compact.




Not for nothing... but there have been lots of comments when people - especially new users - complain about UDFs not working the way they want. Often, the response has been that UDFs are there for people to expand into things more appropriate for their environment. This isn't easy to do with "Lonkified" code. Witness the Kix2HTM that I worked on - took over 3 hours just to work through the logic and comment it so I could fix the relatively minor line-break issue. Impressive logic, but I'd grade it poorly for style and supportability. (Even Lonk didn't want to troubleshoot his code!)

Personally, I'd rather publish stuff with lots of comments and understandable var names, only get Lonk's shorts in a (tighter? ) twist, and not have to answer lots of questions about how my code works. Just my $0.03.

I'm testing the code now... if you'd like, I'll send you the finished version with EXTRA comments.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115205 - 2004-02-29 05:46 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
if instr($sComputer,'\')
$sComputer = right($sComputer,instrrev($sComputer,'\'))
Endif


could be replaced by

$sComputer = trim(join(split($sComputer,'\'),''))

and I screwed up this line...

$sQuery = $sQuery+" Where "+$sWhere+" = '"+$x+"'"

(this one works)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#115206 - 2004-02-29 05:51 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Thanks - I'll try it and let you know. I was just gonna tell you it isn't working!

Glenn

PS-

No joy, Rad - I already had that correct line in place - nothing is being returned from the new version.

Going skiing - will test more when I return.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#115207 - 2004-02-29 07:04 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
this one is working for me...

Code:

FUNCTION WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, Optional $sUserID, Optional $sUserPW)
Dim $sQuery, $objEnum, $sValue, $TMP, $SystemSet, $, $objInstance, $objLocator

$sComputer = trim(join(split($sComputer,'\'),''))

; if instr($sComputer,'\')
; $sComputer = right($sComputer,instrrev($sComputer,'\'))
; Endif
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 $sUserID and $sUserPW and $sComputer <> '.'
$objLocator = CreateObject('WbemScripting.SWbemLocator')
If @ERROR or not $objLocator Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet = $objLocator.ConnectServer($sComputer, $root, $sUserID, $sUserPW)
If @ERROR or not $SystemSet Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet.Security_.ImpersonationLevel = 3
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)
$tmp = $tmp +'|' + iif(VarType($sValue) & 8192,join($sValue,'|',ubound($sValue)),$sValue)
Next
$WMIQuery = split(substr($tmp,2),'|')
Exit VAL("&"+Right(DecToHex(@ERROR),4))
ENDFUNCTION

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#115208 - 2004-02-29 08:53 PM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh glenn...
there was nothing wrong with the original code in the first place, so why I would troubleshoot it?
like jens (iirc) pointed out weeks before, the work-around for current twisted board was to make the code un-copy&pasteable.

lonkified anyways has the benefit of the newbies not trying to screw the code so easily but to read the faq about using them.
I think we all remember what happened some days ago when noobish member didn't want to listen but insisted on screwing the UDF code (which was nicely and easily understably written)
_________________________
!

download KiXnet

Top
#115209 - 2004-03-01 02:04 AM Re: WMIQuery + WMIAuthenticate = WMI_Auth_Query!
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Radimus:

I have no joy from your latest - whether I use security or not, it is returning no data. Leaving the office - will test at home tonight if I can, otherwise will follow up on Tuesday. I'll try and at least have it dump some error stats and get you that.

First dig - it's returning data, but not being interpreted as it was before..

OK -
Code:
$tmp = $tmp +'|' + iif(VarType($sValue) & 8192,join($sValue,'|',ubound($sValue)),$sValue) 


is returning Error 87 - Invalid Parameter before the exit of the function. The data is OK, but something in the statement is getting upset. I can definately dig into this tonight.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 194 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.143 seconds in which 0.107 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org