Go Premium for a chance to win a PS4. Enter to Win

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,635 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 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

782 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