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,616 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 300 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
Does Powershell have you tied up in knots?

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

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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.

622 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