none
Manipulating LDIF files using PowerShell RRS feed

  • Question

  • To begin with, I appreciate there are many ways to get users into AD rather than using LDIF files, but that is what I have. I am not going to focus on how I import the users, more manipulating the files - basically changing text in text files.

    In summary, I have multiple files, which will have multiple operations to add objects to AD groups. Within each operation it can be adding one or more object to a given group - I want to convert all the operations so that they only add one object per operation.

    As an example operation below with two members being added. There could be just one member, or many members, but this one just has two.

    dn: <groupname distinguished name [DN] >

    changetype: modify

    add: member

    member: <object1DN>

    member: <object2DN >

    -

    So I would want the script to read through the above, and produce:

    dn: <groupname distinguished name [DN] >

    changetype: modify

    add: member

    member: <object1DN>

    -

    dn: <groupname distinguished name [DN] >

    changetype: modify

    add: member

    member: <object2DN>

    -

    The operation will always :

    Start with dn:

    have carriage returns at the end of each line

    have the same lines as above, but with different DNs

    Can have one or more members

    End with minus sign (-)

    I have tried various ways of doing this (all my testing has been on one file, so not scaled up to doing multiples yet):

    using get-content on the file, and then trying to use split, using (-) as a delimiter, but this just creates a string which has the - removed. The string is already delimited, so don't think I can use this.

    Then I tried to use select-string, using "-" as the pattern, and thought I could use -context to get the previous lines, which obviously vary. To try and get the number of lines to grab for context I then tried:

    $delimiterline = select-string $file -Simplematch - pattern "-"

    $delimiterlinenumber = $delimiterline.Linenumber

    Then I tried creating an array of these numbers, so I could calculate the difference between the line numbers, and this number could be used for the context switch on select-string...but it just got more and more convoluted!

    So any suggestions would be much appreciated - it seems on the surface to be relatively straightforward, files are cleanly delimited, etc...I feel like I am missing something very obvious!

    thanks,


    Craig.

     

    Friday, September 30, 2016 7:49 AM

All replies