Scenario: We have a CSV file with 50,000 SMTP address. From this list we want to determine if there is a valid user object that owns that address. If there is no valid user object we want to log that fact. If there IS a user object that matches we want the SamAccountName,Displayname, and to know what the primary vs. secondary SMTP addresses are (defined by the presence of "SMTP" in CAPS for the primary, and "smtp" lower case for the secondary) for the "ABC.123.com" domain
This is related to one of my previous postings
http://powershell.com/cs/forums/t/24266.aspx
I have approached this from a different angle, and I am stuck on a different part of my code, so new thread. My code works perfectly except for one minor annoyance in the formatting on the CSV file but I am not sure how to go about fixing it.
functionget-proxyaddresses
{
[CmdletBinding()]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
#ValueFromPipeline=$true,
Position=0)]
[string[]]
$Identity
)
Begin
{
# Blank arrays
$working= @()
Foreach ($Iin$Identity)
{
$Check=Get-ADUser-LDAPFilter"(SamAccountName=$I)"-Propertiesdisplayname,proxyaddresses
if ($Check-ne$null)
{Write-Verbose"Adding $I to working list"
$working+=$Check
}
Else
{Write-Verbose"Adding $I to failed list"
$Failed=New-Objectpsobject
$Failed|Add-MemberNotePropertySamAccountName$I
$failed|Export-Csv-PathC:\ProxyAddress_NotFound.csv-Append-NoTypeInformation
}
}
}
Process
{
ForEach ($win$working)
{
$Logon=$w.samaccountname
$Display=$w.displayname
$Primary=$w.proxyaddresses |? {$_-clike"SMTP:*"-and$_-like"*@ABC.123.com"}
$Secondary=$w.proxyaddresses |? {$_-clike"smtp:*"-and$_-like"*@ABC.123.com"}
$Secondary2=[string]::Join(";",$Secondary)
$Final=New-Objectpsobject
$Final|Add-MemberNotePropertyDisplayName$Display
$Final|Add-MemberNotePropertyLogonName$Logon
$Final|Add-MemberNotePropertyPrimarySMTP$Primary
$Final|Add-MemberNotePropertySecondarySMTP$Secondary2
$Final|Export-Csv-PathC:\Valid_ProxyAddresses.csv-NoTypeInformation-Append-Force
}
}
End
{
}
}
The part I am not happy with is where I use the "Join" method
$Secondary2=[string]::Join(";",$Secondary)
Currently if there are multiple values determined as the "secondary" smtp address I am joining the strings together and seperating them with a semi-colon. I would much prefer the additional values output to their own colomn. There is no consistency to it however, 1 user might only have a primary SMTP address, another user might have 5 secondary smtp addresses. I dont know until I get to that object and retrieve the values from the attribute.
I would appreciate the following:
1. Suggestions on how I could go about writing additional values to their own colomn
2. Suggestions on how I can streamline the code, or more effectivly achieve the same results
I am sure I am not going about it the most efficient way and I welcome constructive criticism so I can learn better techniques in the future. Thanks as always for any help!