Im trying to export a recursive list of Distribution List members (including groups) in a particular format.<\/p>\n
Ive found a few sample scripts to export recursively but im struggling with getting these into a format I am after… They usually just give me all members in a single flat list.
\nWhat im hoping to do is present the list in “levels”. For example, say the top level DL column1, then everything it contains is column2, then everything the Column2 DL Contains in column3.
\nUltimately I want to use Excel to create a pivot table of the info to show the DL structure in a collapsible tree format.
\nIve attached a sample xls of what im hoping to achieve via powershell.
\nIm thinking i need to somehow fill out an array, with a variable for each level… but I just cant seem to get my head around the logic.
\nAny thoughts around how I might achieve this?<\/p>\n
Sample_Export.xlsx<\/a> (15.1 KB)<\/p>","upvoteCount":3,"answerCount":5,"datePublished":"2020-02-03T10:21:02.000Z","author":{"@type":"Person","name":"spiceuser-lvtva","url":"https://community.spiceworks.com/u/spiceuser-lvtva"},"acceptedAnswer":{"@type":"Answer","text":" Hi and Welcome to Spiceworks - thanks for your first post here. Take a look at this: PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> which describes how we work here! I hope this is helpful.<\/p>\n Importing and exporting from Excel can be done using a CSV file. The CSV file is essentially a table of objects, preceded by a header to name each column. In the CSV, each row after the first represents an object occurrences properties. If you have a simple object, then exporting to CSV just gives you a row with the values as in the original object. WIth Complex objects, is ones where a property of the object is actually an object that has properties (which can be objects that have properties). An obvious example is (Get-Process | Select -skip 1 -First 1). Modules - Each process object has a Modules property. That property is an array of objects each with properties. This makes it more complex to build your excel spreadsheet. The problem is that when PowerShell takes an object array (ie what you get back from Get-ADGroup) it can not export the objects in the format you want.<\/p>\n SO, you can do this, but it is more wok. What you have to do is to run through the nested groups and build the relevant objects with the relevant properties via a set of next for each. It could certainly be done. You would need to Get the top level lists, and for each member spit out the users at that level, then get the level 1 groups. For each of those, spit out the users at that level and the Level 2 groups. FOr each of those, spit out the users at that level plus the level three groups. And for each of them spit out the users at level 4.<\/p>\n You can start by getting the objects at the top level:<\/p>\n I hope this gets you started.<\/p>\n I haven’t looked closely enough, but you might be able to get some help from the ImportExcel module which you can download from the PS Gallery. See PowerShell Gallery | ImportExcel 5.4.2<\/a><\/p>\n Have a go, post what you come up with for more help.<\/p>","upvoteCount":1,"datePublished":"2020-02-03T11:54:16.000Z","url":"https://community.spiceworks.com/t/script-to-export-distribution-list-tree-to-csv-or-excel/749461/2","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},"suggestedAnswer":[{"@type":"Answer","text":" Im trying to export a recursive list of Distribution List members (including groups) in a particular format.<\/p>\n Ive found a few sample scripts to export recursively but im struggling with getting these into a format I am after… They usually just give me all members in a single flat list.$ArrayOfUsers = @() # you ad to this\nFunction Add-User { <adds the user to the array} # fill in the blanks\n$TopLevel = Get-AdObject -Filter * -SearchScope OneLevel -SearchBase 'ou=cookhamhq,dc=cookham,dc=net'\nForeach ($Obj in $TopLevel) \n If (! (object is a user or a group) continue # skip over this\n If <object is user> {add-uuser $obj -Level 1} # add this user to the array of user\n Else { #here to process a level 1 group\n $Level2objes = Get-Adobject -Filter * =searchscope ONeLevel -Searchbase <work it out from $obj> \n Foreach (L2obj in $Level2objs)\n -skip over non group/users\n =add a L2 user> \n <process L2 groups>\n etc\n\n<\/code><\/pre>\n
\nWhat im hoping to do is present the list in “levels”. For example, say the top level DL column1, then everything it contains is column2, then everything the Column2 DL Contains in column3.
\nUltimately I want to use Excel to create a pivot table of the info to show the DL structure in a collapsible tree format.
\nIve attached a sample xls of what im hoping to achieve via powershell.
\nIm thinking i need to somehow fill out an array, with a variable for each level… but I just cant seem to get my head around the logic.
\nAny thoughts around how I might achieve this?<\/p>\n