Solved

Need to compile a CSV containing Email Addresses, LinkedMasterAccount, and EmployeeID, among other things

Posted on 2016-10-24
10
51 Views
Last Modified: 2016-10-25
We have multiple domains with a trust in between.  I need to compile a CSV combining both of these commands, with accounts from one OU and its sub-OUs:

Get-ADuser -Filter * -SearchBase "OU=Company1,DC=domain,DC=local" -properties * | Select-Object Name,Mail,EmployeeId
Get-Mailbox | Select-Object -ExpandProperty Name,SamaccountName,LinkedMasterAccount,ObjectClass

Basically I need to work up a CSV that shows the Name and EmployeeID from Get-ADUser, and the SAMAccountName and LinkedMasterAccount (from the other domain), that is obtained from the Get-Mailbox command.  Can anyone help?  I've been at it for 5 hours, and still cannot get it to work.  From my reading I need to create an array and such, but it's really beyond me, and I'm not getting anywhere.  Thanks so much!
0
Comment
Question by:redmanjb
  • 6
  • 4
10 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41858041
Try it like this:
Get-ADUser -Filter * -SearchBase "OU=Company1,DC=domain,DC=local" -Properties * |
	ForEach-Object {
		$Mailbox = Get-Mailbox -Identity $_.DistinguishedName
		$_ | Select-Object -Property `
			Name,
			Mail,
			EmployeeId,
			@{Name=SamAccountName; Expression={$Mailbox.SamAccountName}},
			@{Name=LinkedMasterAccount; Expression={$Mailbox.LinkedMasterAccount}}
	}

Open in new window

0
 

Author Comment

by:redmanjb
ID: 41858403
Thanks for the quick reply!  Neither of the lines at the end with the @ are working though, saying:
The term 'LinkedMasterAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:8 char:39

I may not have given you the correct cmdlets before.

So, this command works to get a few of the things I need:
Get-Mailbox -OrganizationalUnit "OU=Org1,DC=domain,DC=local" | Select LinkedMasterAccount,Name,SamaccountName,ObjectClass

And this gives the EmployeeID portion:
Get-Mailbox -OrganizationalUnit "OU=Org1,DC=domain,DC=local" | Select LinkedMasterAccount,Name,SamaccountName,employeeid,ObjectClass
Get-Aduser –filter * -SearchBase "OU=Org1,DC=domain,DC=local" -properties * | Select-Object Name,EmployeeID

I would just need to put them together.  Sorry to be confusing!  I really appreciate your help!
0
 

Author Comment

by:redmanjb
ID: 41858407
And how would I take the result and export to CSV?   Normally I would just do:  | Export-CSV C:\tech\doc.csv

Thanks again!
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41858419
Sorry, that was just some missing quotes:
$SearchBase = "OU=Company1,DC=domain,DC=local"
Get-ADUser -Filter * -SearchBase $SearchBase -Properties * |
	ForEach-Object {
		$Mailbox = Get-Mailbox -Identity $_.DistinguishedName
		$_ | Select-Object -Property `
			Name,
			Mail,
			EmployeeId,
			@{Name='SamAccountName'; Expression={$Mailbox.SamAccountName}},
			@{Name='LinkedMasterAccount'; Expression={$Mailbox.LinkedMasterAccount}}
	}

Open in new window

0
 

Author Comment

by:redmanjb
ID: 41858714
Awesome!  That works!  :) How would I export it to a table in CSV format, to where each of the values are column headings, and one employee per row?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 83

Expert Comment

by:oBdA
ID: 41858732
Just pipe it to Export-Csv; add the following at the end of line 11 (after the "}"):
 | Export-Csv -NoTypeInformation -Path C:\Temp\Whatever.csv

Open in new window

0
 

Author Comment

by:redmanjb
ID: 41858783
Thanks again.  It looks like it didn't like the Export-Csv:

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "Export-Csv" value of type "System.String" to type
 "System.Management.Automation.ScriptBlock".
At line:2 char:19
+     ForEach-Object <<<<  {
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand



Here's the script in its entirety:


      import-module activedirectory
      $SearchBase = "OU=Org1,DC=domain,DC=local"
Get-ADUser -Filter * -SearchBase $SearchBase -Properties * |
      ForEach-Object {
            $Mailbox = Get-Mailbox -Identity $_.DistinguishedName
            $_ | Select-Object -Property `
                  Name,
                  Mail,
                  EmployeeId,
                  @{Name='SamAccountName'; Expression={$Mailbox.SamAccountName}},
                  @{Name='LinkedMasterAccount'; Expression={$Mailbox.LinkedMasterAccount}}
      } Export-Csv -NoTypeInformation -Path C:\Tech\test.csv
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41858840
You left out the pipe symbol right before "Export-Csv".
0
 

Author Comment

by:redmanjb
ID: 41858995
PERFECT!  Thank you so much for all your patience and your efforts!  It works great!
0
 

Author Closing Comment

by:redmanjb
ID: 41858999
Super fast and very knowledgeable response!  A+!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now