string - PowerShell: Split user defined distinguished name into individual parts -
i have gui accepts few dns user. know how text, can't life of me figure out how split dn individual parts. number of parts might vary. example:
$string1 = "ou=this,ou=is,dc=a,dc=string" $string2 = "ou=this,ou=is,ou=another,dc=dn,dc=string
what i'm trying separate ou's dc's , create ou's if don't exist. can't figure out how separate ou's dc's , make them separate strings i'll left with:
$newstring1 = "ou=this,ou=is" $newstring2 = "dc=a,dc=string" $newstring3 = "this,ou=is,ou=another" $newstring4 = "dc=dn,dc=string
i know can split strings using $string1.split(",")
, i'm @ loss @ point how store individual values in new variable. i'm new powershell, advice appreciated.
i know can create ou's information have, need them separated other work that's going done in code. nice see them split such can grab ou's individually.
hopefully helpful in terms of what's being asked. assumes want store relative distinguished names in 2 groups: before hit dc=
, after (and including) hit dc=
.
$string1 = "ou=this,ou=is,dc=a,dc=string" $string2 = "ou=this,ou=is,ou=another,dc=dn,dc=string" foreach ($dn in $string1, $string2) { # break distinguished name list of relative distinguished names $rdnlist = $dn -split ','; $cnlist = @(); $nextindex = 0; while ($nextindex -lt $rdnlist.length) { $rdn = $rdnlist[$nextindex]; $name, $value = $rdn -split '='; # stop looping if we've hit dc= rdn if ($name -eq 'dc') { break; } $cnlist += $rdn; $nextindex++; } # remainder of array our rdn list $dclist = $rdnlist[$nextindex..($rdnlist.length - 1)]; # reassemble both lists strings $cntext = $cnlist -join ','; $dctext = $dclist -join ','; # data has been processed; print out write-host "`$dn: $dn"; write-host "`$cntext: $cntext"; write-host "`$dctext: $dctext"; write-host; }
output is...
$dn: ou=this,ou=is,dc=a,dc=string $cntext: ou=this,ou=is $dctext: dc=a,dc=string $dn: ou=this,ou=is,ou=another,dc=dn,dc=string $cntext: ou=this,ou=is,ou=another $dctext: dc=dn,dc=string
Comments
Post a Comment