Home > c# > Capturing AD GUIDs to String

Capturing AD GUIDs to String

August 24, 2006

The results of a couple days of coding and Googling and such, I finally figured out how to output a GUID as it’s HEX format to a string.

1. Create a Property reader that outputs Objects rather than Strings.

public static object GetPropertyByte(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0];
}
return string.Empty;
}

2. Now, I have setup a switch statement to filter out the GUIDs, memberOf arrays, and regular strings.  If you’re just capturing GUIDs, this is unnecessary.  I’ve highlighted the key lines of GUID code in red.

switch (sADUserColumn)
{
case “objectGUID”:
  System.Guid g = new System.Guid((System.Byte[])GetPropertyByte(result1, sADUserColumn));
  builder2.Append(g.ToString());

  builder2.Append(“,”);
  break;
case “memberOf”:
  // Skip!
  break;
default:
  builder2.Append(GetProperty(result1, sADUserColumn));
  builder2.Append(“,”);
  break;

}

I had come across dozens of articles using ASCIIEncoding and other functions to manipulate the byte[], but, finally managed to just try to force it into an actual GUID object and DING it worked!

[edit]: You could probably streamline this and have your GetProperty function JUST capture the Guid and return a Guid rather than an object.  Heh, a later iteration though ’cause this works for now! LOL

Categories: c#
%d bloggers like this: