Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

powershell - Get Distribution List muliple Owners in exchange

i need a power shell cmd or script which will give me the list of all the Distributions list along with the OWNERS of that like managed by.

But , if there are multiple users inside managedby attribute then I am getting System.Object[].

My question are :

1- how can we get multiple users for managedby attribute ?

2- how can we add employeeid and samaccountname for managedby users ?

3 - if there is no managed by user then it display "NO MANAGED BY USER"

4- I want to get mail groups not hidden.

script :

$DGroups=Get-DistributionGroup -resultsize unlimited
$mastertable=@()
ForEach($Group in $DGroups){
    $table=[pscustomobject][ordered]@{
                                Name = $group.name
                                "Managed By" = $group.managedby.name
                                "DistinguishedName" = $group.DistinguishedName
                                

                                    }
$Mastertable += $table

}


$Mastertable |  export-csv C:	mpmanagedby.csv -NoTypeInformation

My output :

"Name","Managed By","DistinguishedName"
"IT research","System.Object[]","CN=IT research,OU=TEST,DC=contoso,DC=com"
"Test Mail Group 1","User01","CN=Test Mail Group 1,OU=Test,OU=COMPANY,DC=contoso,DC=com"
"Test Mail Group 2",,"CN=Test Mail Group 2,OU=Test,OU=COMPANY,DC=contoso,DC=com"

My desired output :

"Name","Managed By","DistinguishedName","OWNERSAMACCOUNTNAME","OWNEREMPLOYEEID"
"IT research","User01;User02","CN=IT research,OU=TEST,DC=contoso,DC=com","tst124;tst125","242333;344232"
"Test Mail Group 1","User01","CN=Test Mail Group 1,OU=Test,OU=COMPANY,DC=contoso,DC=com","tst124","242333"
"Test Mail Group 2","NO MANAGED BY USER","CN=Test Mail Group 2,OU=Test,OU=COMPANY,DC=contoso,DC=com"

LAST UPDATE :

[PS] C:Windowssystem32>$group.ManagedBy


OrgHierarchyToIgnore : 
IsDeleted            : False
Rdn                  : CN=John T
Parent               : contoso.com/COMPANY/IT
Depth                : 5
DistinguishedName    : CN=John T,OU=IT,DC=contoso,DC=com
IsRelativeDn         : False
DomainId             : contoso.com
PartitionGuid        : 59ce2f71-eaa2-4ddf-a4fa-f25069d0b324
PartitionFQDN        : contoso.com
ObjectGuid           : 62d578e8-b69c-45bc-967a-e530d72792e1
Name                 : John T

[PS] C:Windowssystem32>$group.ManagedBy | ForEach-Object { $_ | Get-ADUser -Properties EmployeeId }
Get-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties 
do not match any of the parameters that take pipeline input.
At line:1 char:42
+ $group.ManagedBy | ForEach-Object { $_ | Get-ADUser -Properties EmployeeId }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (contoso.com/COMPANY/IT/John T:PSObject) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is my understanding the ManagedBy attribute stores the DistinguishedName(s) of one or more users (or none at all).

I haven't tested this myself, but you could try:

$DGroups     = Get-DistributionGroup -ResultSize Unlimited
$mastertable = foreach($group in $DGroups) {
    # initialize a PsCustomObject
    $data = [PsCustomObject]@{
        Name                = $group.Name
        DistinguishedName   = $group.DistinguishedName
        ManagedBy           = 'NOT MANAGED'
        OwnerSamAccountName = $null
        OwnerEmployeeId     = $null
    }

    if ($group.ManagedBy) {
        $managedBy                = $group.ManagedBy | ForEach-Object { Get-ADUser -Identity $_.DistinguishedName -Properties EmployeeId }
        $data.ManagedBy           = $managedBy.Name -join ';'
        $data.OwnerSamAccountName = $managedBy.SamAccountName -join ';'
        $data.OwnerEmployeeId     = $managedBy.EmployeeId -join ';'
    }
    # output this data to be collected in $mastertable
    $data
}


$mastertable |  Export-Csv 'C:	mpmanagedby.csv' -NoTypeInformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...