Question : 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.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
####################################################################
# 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

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

So much code :)

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

Chris
1:
2:
3:
4:
5:
6:
7:
8:
# 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"
Random Solutions  
 
programming4us programming4us