Solved

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

Posted on 2016-10-24
10
44 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
 
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

Join & Write a Comment

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
A procedure for exporting installed hotfix details of remote computers using powershell
In this video we show how to create a Distribution Group 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 Recipients >>…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

747 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

15 Experts available now in Live!

Get 1:1 Help Now