Solved

Powershell script to extract name, samaccount, and email server name from text file with users' display name

Posted on 2010-08-24
6
1,578 Views
Last Modified: 2012-05-10
Goal:
Powershell script uses a TXT file with user's display name (usually first and last name) to extract users' first and last names, samaccountname, and email server to a CSV file.

** NOTE: I'm not using Exchange Shell. I'm using ARM and PowerGUI.

Work so far:
I've been able to accomplish all of the tasks that I mentioned above individually, but I'm not sure how to put them all together in one script. The logic that I've followed is as follows:

1. Use the user's display name provided in the TXT file to obtain the user's DN.
2. Use the user's DN to obtain the email server name (msExchHomeServerName).
3. Use the split switch to get *only* the server's name from #2.
4. Output first and last names, samaccountname, and email server name to CSV file.


I'm attaching the code that I have so far.
####################################################################

# Assuming that a different name is passed to this value in some type of loop, the function will return the user's DN.



function Get-userDN($user){

	return ((Get-QADUser $user -IncludedProperties DN).DN)

}



####################################################################

# Again, assuming that a user's DN is passed to this function in #some type of look, the function will obtain the user's email #server name.



function Get-userServerName($userDN){

	return ((Get-QADUser -Identity $userDN -IncludedProperties msExchHomeServerName).msExchHomeServerName)

}



#################################################################

# This function will extract the server's name from the email  #server name DN

function Get-homeServer([string]$DN){            

    return ($DN.replace('/','') -split ",*..=")[4]            

}     



##################################################################

#This is how I usually export info to a CSV file. However, I #usually use the Get-QAD...cmdlet. Never tried more complex scripts #with functions and such.



$users = Get-Content -Path 'C:\users.txt'



@(ForEach($user in $users){

		

	Get-QADUser -Identity $user -IncludedProperties name,msExchHomeServerName | select name,msExchHomeServerName

	}) |  Export-Csv C:\output.csv -NoType

Open in new window

0
Comment
Question by:bndit
  • 4
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 300 total points
Comment Utility
So much code :)

You only need this. Please let me know if this raises any questions.

Chris
# Read the file
Get-Content "YourFile.txt" | ForEach-Object {
  # Get the user and return some properties
  # EmailServerName is a custom property, and is the result of ripping apart msExchHomeServerName
  Get-QADUser $_ -IncludedProperties msExchHomeServerName | 
    Select-Object FirstName, LastName, SamAccountName, @{n='EmailServerName';e={ $_.msExchHomeServerName -Replace '.*=' }}
# Export the results to a CSV file
} | Export-Csv "out.csv"

Open in new window

0
 
LVL 2

Author Comment

by:bndit
Comment Utility
Thanks Chris. Your script worked perfectly. One favor, could you explain this part of the script?

"# EmailServerName is a custom property, and is the result of ripping apart msExchHomeServerName"

  Get-QADUser $_ -IncludedProperties msExchHomeServerName |
    Select-Object FirstName, LastName, SamAccountName, @{n='EmailServerName';e={ $_.msExchHomeServerName -Replace '.*=' }}

I follow the above line up until the end of the Select-Object cmdlet, but kinda get lost with the ...@{n='EmailServerName';e={ $_.msExchHomeServerName -Replace '.*=' }}
I kinda get the current pipe obj ($_) and the -Replace switch but the 'n=' and 'e=' and the '@' are throwing me off.
0
 
LVL 2

Author Comment

by:bndit
Comment Utility
I wanted to get an idea of how long the script would take to create the report so I used the Measure-Command cmdlet. The script created the output file with data in it, but Powershell generated this error message. Is it because I'm using the Measure-Command cmdlet incorrectly?
measure-object-error.png
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility
Good morning :)

> @{n='EmailServerName';e={ $_.msExchHomeServerName -Replace '.*=' }}

When you want to add a property to the output using Select-Object you have to construct a description of that property. The description is stored in a Hash Table, a table of key and value pairs, denoted by @{ ... }). The Hash Table must contain two specific keys, Name (n, name), and Expression (e, expression).

The name is pretty much self-explanetory, it's what you want to call the property. The Expression is a Script Block, denoted by { }, it tells us how we would calculate the value, and in this case we're stripping off everything before the last equal symbol from msExchHomeServerName.

Measure-Object... what are you trying to measure? You are not exporting any numeric properties. Did you mean Group-Object? That could show you the number of mailboxes per server?

e.g.

.\report-emailServer.ps1 | Group-Object EmailServerName

It's worth knowing how to use Measure-Object, you might do this to find the total sizes of all files in a directory:

Get-ChildItem | Measure-Object Length -Sum

But it's difficult to apply the command to your current situation unless you've added more fields?

Chris
0
 
LVL 2

Author Comment

by:bndit
Comment Utility
Thanks Chris. Very helpful as always.  As for the Measure-Object...it's actually Measure-Command...I wanted to know how long it took the script to complete. I'll deal with that later, for now I'll award you the points.

On a different note, I've been cracking my head on a different Powershell script...would you mind taking a look at it and give me some pointers? Here's the link

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26426961.html

Basically I'm trying to use a WMI query to find mailbox statistics from an Exchange 2003. However, I want to use a source text file with only a handful of mailbox as opposed to have the WMI query search the entire mail server. Thanks for your help.
0
 
LVL 2

Author Closing Comment

by:bndit
Comment Utility
Excellent.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

10 Experts available now in Live!

Get 1:1 Help Now