Get mail from AD and replace it with ManagedBy

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.

 

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

 

I would appreciate your help Thanks

 

Tre, welcome to Powershell.org. Please take a moment and read the first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

[quote quote=286990]In the first step I am asked about all the servers and who owns them:

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. [/quote]

Why do you want to do this in two steps? Instead of messing with the Distinguished Name you get from the property ManagedBy you should use this inside your calculated property to query the AD for the user and use the property email from this query.

You are making this more difficult by destroying the distinguished name with -replace ‘^CN=|,.*$’. If you instead use it in your lookup it will make it much simpler.

$Servers = Get-ADComputer -Filter -SearchBase ‘OU=Servers,OU=CONTOSO Computers,DC=contoso,DC=lan’ -Properties ManagedBy |
    Select-Object -Property Name, @{label=’ManagedBy’;expression={(Get-ADUser $_.managedby -Properties mail).mail}}

[quote quote=287011]You are making this more difficult by destroying the distinguished name with -replace ‘^CN=|,.*$’. If you instead use it in your lookup it will make it much simpler.<textarea class=“urvanov-syntax-highlighter-plain print-no” style=“tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;” readonly=“readonly” data-settings=“dblclick”></textarea>

1
2
$Servers = Get-ADComputer -Filter -SearchBaseOU=Servers,OU=CONTOSO Computers,DC=contoso,DC=lan-Properties ManagedBy |
Select-Object -Property Name, @{label=ManagedBy;expression={(Get-ADUser $_.managedby -Properties mail).mail}}
[/quote]

Amazing! Thank you man.

Another little question. In some cases the value in ManagedBy is the name of a group, and Get-ADGroup should be used for them.

How to properly integrate it in the code?

You could use Get-ADObject and use the property ObjectClass to distinguish between users and groups.

 

Edit:

Or even better if your AD groups have email addresses you can simply use the output you get from Get-ADObject and use the property mail. So id doesn’t matter if it’s a user or a group.

[quote quote=287032]You could use Get-ADObject and use the property ObjectClass to distinguish between users and groups.

Edit:

Or even better if your AD groups have email addresses you can simply use the output you get from Get-ADObject and use the property mail. So id doesn’t matter if it’s a user or a group.

[/quote]

great. works like a charm.