Solved

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

Posted on 2016-10-24
10
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 84

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
Are your AD admin tools letting you down?

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 84

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
 
LVL 84

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 84

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

Are your AD admin tools letting you down?

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.

Question has a verified solution.

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

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

756 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