Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit
bndit

ASKER

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.
Avatar of bndit

ASKER

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
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
Avatar of bndit

ASKER

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

https://www.experts-exchange.com/questions/26426961/Getting-Exchange-2003-mailbox-information-using-Powershell.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.
Avatar of bndit

ASKER

Excellent.