Extracting Location from Active Directory Path

(imported topic written by Matt.Johnson)

Hello all,

I am trying to create a custom property from the Active Directory Path default property, and am a little confused on how to proceed.

When I run the code:

if exists value of settings “_BESClient_ActiveDirectoryPathOverride” of client then value of setting “_BESClient_ActiveDirectoryPathOverride” of client else if exists true whose (if true then exists distinguished name of local computer of active directory else false) then distinguished name of local computer of active directory else “”

I get:

CN=PCNAME,OU=Workstations,OU=HICKORY ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com

(I’ve changed a few values for privacy reasons)

I would like to create a property in which I can specify a string to look for, and give a result, such as:

IF exists HICKORY then “HICKORY” ELSE IF exists Duluth, then “DULUTH” ELSE …

I hope this makes sense. I appreciate any suggestions on how to author this code.

Thank you,

Matt Johnson

(imported comment written by liuhoting)

Here are some relevance queries you can run. In these examples, I’m using the string explicitly to do string comparisons since I’m not in the same active directory environment as you. You should use:

if exists value of settings “_BESClient_ActiveDirectoryPathOverride” of client then value of setting “_BESClient_ActiveDirectoryPathOverride” of client else if exists true whose (if true then exists distinguished name of local computer of active directory else false) then distinguished name of local computer of active directory else “”

instead, but these should give you the basic concept.

Basically the idea is we can do the string comparison over and over… I’m comparing only in the case of HICKORY and DULUTH here:

Q: if (“CN=PCNAME,OU=Workstations,OU=HICKORY ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string contains “HICKORY”) then “HICKORY” else if (“CN=PCNAME,OU=Workstations,OU=HICKORY ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string) contains “DULUTH” then “GHOST” else “NOTHING”

A: HICKORY

T: 0.270 ms

Q: if (“CN=PCNAME,OU=Workstations,OU=DULUTH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string contains “HICKORY”) then “HICKORY” else if (“CN=PCNAME,OU=Workstations,OU=DULUTH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string) contains “DULUTH” then “DULUTH” else “NOTHING”

A: DULUTH

T: 0.058 ms

Q: if (“CN=PCNAME,OU=Workstations,OU=BLAH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string contains “HICKORY”) then “HICKORY” else if (“CN=PCNAME,OU=Workstations,OU=BLAH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com” as string) contains “DULUTH” then “DULUTH” else “NOTHING”

A: NOTHING

T: 0.059 ms

But if you have lots of strings that you need to check for you have to basically run that big active directory check again and again which is really annoying… Luckily you can be clever and do something like this instead:

Q: (if (it contains “HICKORY”) then “HICKORY” else if (it contains “DULUTH”) then “DULUTH” else “NOTHING”) of “CN=PCNAME,OU=Workstations,OU=HICKORY ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com”

A: HICKORY

T: 0.052 ms

Q: (if (it contains “HICKORY”) then “HICKORY” else if (it contains “DULUTH”) then “DULUTH” else “NOTHING”) of “CN=PCNAME,OU=Workstations,OU=DULUTH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com”

A: DULUTH

T: 0.053 ms

Q: (if (it contains “HICKORY”) then “HICKORY” else if (it contains “DULUTH”) then “DULUTH” else “NOTHING”) of “CN=PCNAME,OU=Workstations,OU=BLAH ,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com”

A: NOTHING

T: 0.061 ms

Hopefully this helps…

(imported comment written by Matt.Johnson)

Thank you for your reply!

That code is great, but I need it to look at the current AD Path of the device, where yours uses a static OU string from what I can see, or did I miss something?

Thanks,

Matt

(imported comment written by martinc)

Hi Matt,

I was trying to think of a more manageable way to do this so that you would not have to add a new “if” statement every time there was a new site. I did not know how many sites you may have so I was thinking something like this

q: following texts of first “=” of parenthesized part 3 of matches (regex “^(.),(.),(.),(.),(.),(.),(.),(.),(.*)$”) of “CN=PCNAME,OU=Workstations,OU=HICKORY,OU=STATE,OU=MIT,DC=DOM,DC=core,DC=DOMAIN,DC=com”

A: HICKORY

The idea here is that you would parse the path and then get the value of the site from the path. Now the only real issue with this is that it expects a certain length in the AD path.

If you think this could work, but the path length could change, I could see what else to come up with :slight_smile:

(imported comment written by Matt.Johnson)

Than you for looking in to this. I am still new to properties when it comes to the details.

(imported comment written by martinc)

oops did the same as the other poster, forgot to add the original stuff back in. Here you go:

q: following texts of first “=” of parenthesized part 3 of matches (regex “^(.),(.),(.),(.),(.),(.),(.),(.),(.*)$”) of distinguished name of local computer of active directory

Now I know that the original had all the stuff for “_BESClient_ActiveDirectoryPathOverride” but most likely you are not using that, so this is just more “fluff”. If you want to make this better for error checking, then maybe something like:

if exists true whose (if true then exists distinguished name of local computer of active directory else false) then (following texts of first “=” of parenthesized part 3 of matches (regex “^(.),(.),(.),(.),(.),(.),(.),(.),(.*)$”) of distinguished name of local computer of active directory) else “”

Still early in the morning and only one cup of coffee :wink:

(imported comment written by Matt.Johnson)

Works perfectly! Thank you.

(imported comment written by MaherBA)

Dear all,

This property works perfectly.

I want to create a new property to extract the 4th item and the 3th one and to put them in a same property.

I tried this relevance but it doesn’t work:

if exists true whose (if true then exists distinguished name of local computer of active directory else false)

then

(following texts of first “=” of parenthesized part 4 of matches (regex “^(.),(.),(.),(.),(.),(.),(.),(.),(.)$") of distinguished name of local computer of active directory) & (following texts of first “=” of parenthesized part 3 of matches (regex "^(.),(.),(.),(.),(.),(.),(.),(.),(.)$”) of distinguished name of local computer of active directory)

else “”

Can you help me please?