Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-24
6
Medium Priority
?
1,627 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
[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
  • 4
  • 2
6 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1200 total points
ID: 33508903
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
ID: 33515157
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
ID: 33516378
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
Protect Your Retail Business and Reputatio

Wi-Fi access doesn't just impact your business & customer experience, it can also affect your security.  Join us for a webinar on Sept. 28th to learn more about the top threats and trends impacting retail today, and the key solutions to protecting retail networks and reputations.

 
LVL 71

Expert Comment

by:Chris Dent
ID: 33518789
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
ID: 33518817
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
ID: 33518818
Excellent.
0

Featured Post

Industry Leaders: 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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

660 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