Scripting. Stuff. (By Froosh)

September 22, 2005

VBScript vs. Byte()

Filed under: VBScript — Froosh @ 9:43 am

In a recent fight with VBScript I was attempting to read SID’s from the logged in users Access Token and translate them to group names. In the process, objUser.Get(“tokenGroups”) returns a Variant() array of Byte() arrays.

Easy you say, just do something like for each bytPart in Byte() … except that VBScript doesn’t actually know how to work with a Byte(). It knows Byte and Array(), just not how they work together.

After much head-scratching and googling, the secret was revelead in a snippet of code attributed to Richard Mueller, a MS MVP in Scripting and ADSI (found on Michael Harris’ blog). And the secret is: Byte() arrays are really just byte strings (as opposed to unicode/char strings).

Knowing that, the next minor secret is how to work with byte strings. Unsurprisingly, the answer is in the documentation. Read any of the string functions (Len, InStr, Mid, etc) and pay attention to any Note: sections that mention a *B variant of the function. e.g. LenB, InStrB, MidB

ConvertByteArrayToHexString = ""
For intCounter = 1 to LenB(arrBytes)
  ConvertByteArrayToHexString = ConvertByteArrayToHexString & Right("0" & Hex(AscB(MidB(arrBytes, intCounter, 1))), 2)
Next

So now you know. Happy Byte()-ing!

Advertisements

4 Comments »

  1. I have been looking all over the place for a script that does exactly this, read the user SIDs from their access token. Is there any chance I could get a complete copy of your script that did this?

    Comment by Steve — May 3, 2007 @ 2:08 am

  2. […] move that is a little like Marge re-using the 1 stylish dress she found, I’ve wrapped my old Byte() and SID manipulation code into a VBScript Class and a Windows Script […]

    Pingback by Byte Array and SID manipulation class and component « Scripting. Stuff. (By Froosh) — January 6, 2009 @ 11:41 am

  3. This is very helpful. Finally found a way to loop through an Array of Byte in VBScript

    Comment by Geer — August 11, 2012 @ 3:10 am

  4. 10Q Dear …you get a one BIG LIKE FROM ME 🙂

    Comment by Etay G :-) — August 15, 2012 @ 11:51 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: