Skip to content

Instantly share code, notes, and snippets.

@matt40k
Last active November 15, 2016 23:13
Show Gist options
  • Save matt40k/ad9aa51201e73f112e790655fb90049a to your computer and use it in GitHub Desktop.
Save matt40k/ad9aa51201e73f112e790655fb90049a to your computer and use it in GitHub Desktop.
PowerQuery (M) code for querying Active Directory for Distribution lists
let
Source = ActiveDirectory.Domains("domain.local"),
#"YourDomain" = Source{[Domain="domain.local"]}[Object Categories],
group = #"YourDomain"{[Category="group"]}[Objects],
#"Expanded group" = Table.ExpandRecordColumn(group, "group", {"mail", "managedBy", "member"}, {"group.mail", "group.managedBy", "group.member"}),
#"Expanded mailRecipient" = Table.ExpandRecordColumn(#"Expanded group", "mailRecipient", {"dLMemSubmitPerms"}, {"mailRecipient.dLMemSubmitPerms"}),
#"Expanded mailRecipient.dLMemSubmitPerms" = Table.ExpandListColumn(#"Expanded mailRecipient", "mailRecipient.dLMemSubmitPerms"),
#"Expanded group.member" = Table.ExpandListColumn(#"Expanded mailRecipient.dLMemSubmitPerms", "group.member"),
#"Filtered Rows to only Groups with Email addresses" = Table.SelectRows(#"Expanded group.member", each [group.mail] <> null),
#"Expanded group.managedBy" = Table.ExpandRecordColumn(#"Filtered Rows to only Groups with Email addresses", "group.managedBy", {"mail"}, {"group.managedBy.mail"}),
#"Expanded user" = Table.ExpandRecordColumn(#"Expanded group.managedBy", "group.member", {"mail"}, {"Email Address"}),
#"Renamed Columns" = Table.RenameColumns(#"Expanded user",{{"Email Address", "Member Email Address"}, {"group.managedBy.mail", "Owner Email Address"}, {"mailRecipient.dLMemSubmitPerms","Restriction"}, {"distinguishedName", "Distribution List Distinguished Name"}, {"group.mail", "Distribution List Email Address"}, {"displayName", "Distribution List"}}),
#"Added Has Owner" = Table.AddColumn(#"Renamed Columns", "Has Owner", each if ([Owner Email Address] = null) then "No" else "Yes"),
#"Removed Columns" = Table.RemoveColumns(#"Added Has Owner",{"top", "msExchMailStorage", "msExchIMRecipient", "msExchBaseClass", "msExchCustomAttributes", "posixGroup", "securityPrincipal"}),
#"Added Has Restriction" = Table.AddColumn(#"Removed Columns", "Has Restriction", each if([Restriction] = null) then "No" else "Yes")
in
#"Added Has Restriction"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment