0

I have a user named Lamda that I want to extract the OU and Domain sequence from the DistinguishedName.

For example, for the DistinguishedName of CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD

I want to extract the sequence OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD and assign to a variable.

I tried the following script but it did not work:

$var = dsquery user -name 'Lamda' | dsget user -dn [regex]::match($var,'(?=OU)(.*\n?)(?<=.)').Value
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
MKH_Cerbebrus
  • 51
  • 2
  • 10
  • What have you tried already? – boxdog Mar 01 '19 at 09:07
  • $var = dsquery user -name 'Lamda' | dsget user -dn – MKH_Cerbebrus Mar 01 '19 at 09:11
  • That just gets the user details, which I'd already assumed you had. What have you done to try to extract the info you want? Incidentally, why not use the [AD Cmdlets](https://learn.microsoft.com/en-us/powershell/module/addsadministration/?view=win10-ps) instead of a command-line tool? It's almost always easier to use a 'native' PowerShell command rather than external apps. – boxdog Mar 01 '19 at 09:15
  • Okey I tried the following script but it did not work $var = dsquery user -name 'Lamda' | dsget user -dn [regex]::match($var,'(?=OU)(.*\n?)(?<=.)').Value – MKH_Cerbebrus Mar 01 '19 at 09:18

1 Answers1

0

Split by non-escaped comma once, discard the first substring:

$null,$ou = 'CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD' -split '(?<!\\),',2
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206