Solved

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

Posted on 2016-10-24
10
72 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
Independent Software Vendors: 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!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
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…
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…

734 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