question

RASHMAAR-2635 avatar image
1 Vote"
RASHMAAR-2635 asked RichMatheisen-8856 edited

Get mail from AD and replace with name

Hi team,
My goal is to create a CSV file with two data, the name of all the servers from Active Directory and the email of its owner.
In the first step I am asked about all the servers and who owns them:

$Servers = Get-ADComputer -Filter -SearchBase 'OU=Servers,OU=CONTOSO Computers,DC=contoso,DC=lan' -Properties ManagedBy | Select-Object -Property Name, @{label='ManagedBy';expression={$_.ManagedBy -replace '^CN=|,.$'}}

And now I have the names of the servers and the names of their owners in such mode:
Name ManagedBy
Server1 Stevo Polyi
Server2 Rich Turner
Server3 Kirk Baltz

Now I need to ask AD about the email of each of the names in ManagedBy and replace the name with the email, and here's the part I need help with. I tried all sorts of ways to query AD with the names in $Server.ManagedBy without success.
I would appreciate your help Thanks

windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered RASHMAAR-2635 commented

The ManagedBy attribute returns a CN?

If so, maybe this will help (not tested):

 Get-ADUser -LDAPFilter '(cn=<CN of Managed By User>' -Properties EmailAddress | Select EmailAddress 


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IIRC, the ManagedeBy property contains the distinguishedName of an object.

1 Vote 1 ·

Even better :-)



Kind regards
Andreas Baumgarten

1 Vote 1 ·

Does not work.
Email does not exist in ManagedBy details.
In this way it does work for me, But only if I ask about one server:

         $11Server = Get-ADComputer -Filter {Name -eq 'EXchangeServer'} -Properties ManagedBy | Select-Object -Property Name, @{label='ManagedBy';expression={$_.ManagedBy -replace '^CN=|,.*$'}}
         $22Owner = $11Server.ManagedBy
         $33mail =  Get-ADUser -Filter {Name -eq $22Owner} -Properties mail | Select-Object mail
         $11Server.ManagedBy = $33mail.mail
         $11Server
        
     Name                      ManagedBy
     EXchangeServer      Stevo.Polyi@Contoso.com



How do I make this happen for a list of servers as in the first example?
Thanks




1 Vote 1 ·
RichMatheisen-8856 avatar image
2 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

See if this works for you:

 Get-ADComputer -Filter -SearchBase 'OU=Servers,OU=CONTOSO Computers,DC=contoso,DC=lan' -Properties Name, ManagedBy | 
     ForEach-Object{
         [PSCustomObject]@{
             Name = $_.Name
             ManagedBy = (Get-ADUser $_.ManagedBy | Select -Expand mail)
         }
     } Export-CSV c:\junk\X.csv -NoTypeInformation

You may need to use Get-ADObject instead of Get-ADUser if the ManagedBy property contains the DN of an object other than a user.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Gets the following error, The same goes for Get-ADObject:

Get-ADUser : A parameter cannot be found that matches parameter name 'Expand'.
At line:5 char:50
+ ManagedBy = Get-ADUser $_.ManagedBy -Expand EMailaddress
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

1 Vote 1 ·

Place the in parentheses . . . also, The "-Expand" was supposed to belong to a "Select" cmdlet, which I completely missed typing (although it was there in my head!) E.G.:

 ManagedBy = (Get-ADUser $_.ManagedBy | Select -Expand EMailaddress)

I'll correct the code example I posted.

1 Vote 1 ·

Thanks.
The result is the names of the servers and an empty field of the owner.
I can do this for one server this way:

             $11Server = Get-ADComputer -Filter {Name -eq 'EXchangeServer'} -Properties ManagedBy | Select-Object -Property Name, @{label='ManagedBy';expression={$_.ManagedBy -replace '^CN=|,.*$'}}
             $22Owner = $11Server.ManagedBy
             $33mail =  Get-ADUser -Filter {Name -eq $22Owner} -Properties mail | Select-Object mail
             $11Server.ManagedBy = $33mail.mail
             $11Server
            
         Name                      ManagedBy
         EXchangeServer      Stevo.Polyi@Contoso.com




How do I make this happen for a list of servers as in the first example?
Thanks

1 Vote 1 ·
Show more comments