Solved

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

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

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
Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

 
LVL 85

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 85

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 85

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

631 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