How to Split DistinguishedName?

后端 未结 4 762
孤独总比滥情好
孤独总比滥情好 2020-12-21 11:14

I have a list of folks and their DN from AD (I do not have direct access to that AD). Their DNs are in format:

$DNList = \'CN=Bob Dylan,OU=Users,OU=Dept,OU=         


        
4条回答
  •  执念已碎
    2020-12-21 11:26

    Similar to Grahams answer but removed the hardcoded array values so it will just remove the CN portion without worrying how long the DN is.

    $DNList | ForEach-Object{($_ -split "," | Select-Object -Skip 1) -join ","}
    

    Ansgar most likely has a good reason but you can just use regex to remove every before the first comma

    $DNList -replace "^.*?,"
    

    Update based on briantist

    To maintain a different answer but one that works this regex can still have issues but I doubt these characters will appear in a username

    $DNList -replace "^.*?,(?=OU=)"
    

    Regex uses a look ahead to be sure the , is followed by OU=

    Similarly you could do this

    ($DNList | ForEach-Object{($_ -split "(,OU=)" | Select-Object -Skip 1) -join ""}) -replace "^,"
    

提交回复
热议问题