header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Extracting substring from CN
Last Post 19 Mar 2010 10:07 PM by cameronove. 5 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
NateCUser is Offline
New Member
New Member
Posts:35
Avatar

--
19 Mar 2010 06:04 PM  
I am trying to extract the first bit of a CN from a Distribution List ManagedBy attribute.  The output is typically something like:

CN=John,OU=OrgUnit,OU=OrgUnit2, etc. . . .

However, the bit between the "CN=" and the first "," is the only thing I want to extract and that bit can vary in length.

I tried using Substring and while I can get the script to start in the third position I haven't yet found a way to tell it to stop when it encounters the first comma.

Anyone found a way to do this?

TIA

Nate
cameronoveUser is Offline
Basic Member
Basic Member
Posts:224
Avatar

--
19 Mar 2010 06:09 PM  
Check out this article I wrote that will get you what you want.

http://blog-powershell.blogspot.com...split.html

Here is the function that the article talks about:
function Get-NameFromDN([string]$DN){                        
return ($DN.replace('\','') -split ",*..=")[1]
}
Cameron
NateCUser is Offline
New Member
New Member
Posts:35
Avatar

--
19 Mar 2010 09:06 PM  
Ok here is what I have so far and it gets me 90+% of the way there.

Get-QADMemberOf $user -indirect| ft Name, @{expression= {$_.ManagedBy.Split(',',2)[0]}}

This gives me the first chunk:

CN=UserName

and chucks the rest.

I tried using Replace before, inline and after, but no go there. 

Back to the drawing board to bang my head for a while.
cameronoveUser is Offline
Basic Member
Basic Member
Posts:224
Avatar

--
19 Mar 2010 09:29 PM  
Hmm.  No need to bang your head.  Not sure why you didn't try my suggestion.  It is exactly what you need.

Maybe this will help:

function Get-NameFromDN([string]$DN){                        
return ($DN.replace('\','') -split ",*..=")[1]
}


Get-QADMemberOf $user -Indirect | ft Name,@{n="ManagedBy";e={Get-NameFromDN $_.ManagedBy}}

If you don't like that then try this:

Get-QADMemberOf $user -Indirect | ft Name,@{n="ManagedBy";e={($_.ManagedBy -split ",*..=")[1]}}

The link to my post earlier explained how the -Split works.  -split will use [regex] as delimiters.

Cameron
NateCUser is Offline
New Member
New Member
Posts:35
Avatar

--
19 Mar 2010 10:05 PM  
Your first solution did work (once I got took my blinders off).  So did your second for that matter.

I will need to work at understanding why the REGEX stuff works, but that is for another day as my work day is ending.

My humble thanks for your patience and assistance.

Nate
cameronoveUser is Offline
Basic Member
Basic Member
Posts:224
Avatar

--
19 Mar 2010 10:07 PM  
No problem bro. Glad you got it working.
Cameron
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer