Author Posts

January 1, 2012 at 12:00 am

by etolleson at 2013-02-21 10:20:37

New to PowerShell I am trying to search for users that changed there name and kept there old email addresses I started with the following and it seems that I am close but missing something or alot.

$user = Read-Host "Enter account to search for"

$test = (Get-Mailbox "$user" | select Name, DisplayName, PrimarySmtpAddress,
@{Name='EmailAddresses';Expression={[string]::join("`n", ($_.EmailAddresses))}})

$test

Here is the result of the 1st part $test
>> Running (SmtpAccountCheck.ps1) Script...
>> Platform: V2 64Bit (STA)

Name DisplayName PrimarySmtpAddress EmailAddresses
—- ———– —————— ————–
Ethel Edwards Ethel Edwards eedwards@tcctech.net SMTP:eedwards@tcctech.net
Etta D. Layug Etta D. Layug elayug@tcctech.net SMTP:elayug@tcctech.net
Elizabeth T. Harris Elizabeth T. Harris etharris@tcctech.net SMTP:etharris@tcctech.net
Ed Tolleson Ed Tolleson etolleson@tcctech.net smtp:tctolle@tcctech.net...
Erica Tripp-Wiggins Erica Tripp-Wiggins etrippwiggins@tcctech.net SMTP:etrippwiggins@tcctech.net
Emma Truitt Emma Truitt etruitt@tcctech.net SMTP:etruitt@tcctech.net
Elizabeth K. Turcic Elizabeth K. Turcic eturcic@tcctech.net SMTP:eturcic@tcctech.net

>> Execution time: 00:00:08
>> Script Ended

2nd part
$test | foreach {$_.EmailAddresses -like 'smtp:tctolle*'}

Here is the result from the 2nd part of search I would like to get the name and smtp address instead of true if it possible.
>> Running (SmtpAccountCheck.ps1) Script...
>> Platform: V2 64Bit (STA)
False
False
False
True
False
False
False
>> Execution time: 00:00:09
>> Script Ended

by DonJ at 2013-02-22 04:31:32

In the second part, you're simply performing a comparison, which is always going to produce True/False. You need to add logic:

if ($_.EmailAddresses -like 'smtp:tctolle*') { write $_.EmailAddresses }

Would be your script block instead. Or something like it. Although I'm not sure -like is the right operator here; if EmailAddresses contains a collection, -like will always return False the way you're doing it. If you want to compare each item in EmailAddresses to a wildcard string, you'll need to enumerate those items and compare each individually.

by mjolinor at 2013-02-22 05:25:31

You're concatenating all the smtp addresses into a single string, which means that this:

$test | foreach {$_.EmailAddresses -like 'smtp]

Will only return $true if the first address in the list matches.
You need to wildcard both ends of that search argument so that it will match anywhere in that string:

$test | foreach {$_.EmailAddresses -like '*smtp]

by etolleson at 2013-02-22 08:14:00

I am a little slow. I tried if, where foreach with -like, -eq and still have an empty result maybe I need to expand $_.EmailAddresses some how and then run some kind of compare against it. need to read/research more here is a sample of my testing
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Support

$user = Read-Host "Enter account to search for"
$test = (Get-Mailbox "$user" | select Name, DisplayName, PrimarySmtpAddress,
@{Name='EmailAddresses';Expression={[string]::join("`n", ($_.EmailAddresses))}}) |
foreach {$_.EmailAddresses -eq '*smtp:tctolle*'} {write $_.EmailAddresses}

and also
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Support

$user = Read-Host "Enter account to search for"
$test = (Get-Mailbox "$user" | select Name, DisplayName, PrimarySmtpAddress,
@{Name='EmailAddresses';Expression={[string]::join("`n", ($_.EmailAddresses))}})

$test | where { $acc -eq '*tctolle*'} | write $acc

am I going in the wrong direction?

by etolleson at 2013-02-22 10:29:38

I found a cmdlet Get-Recipient that does some what the search I am trying to do except you can't use wildcards. Now just need to figure out how to use it or search a noteproperty array.

by etolleson at 2013-02-24 07:45:55

After stepping away from this script then trying simple one liners and absorbing the different post about this script.Thanks to all that replied. This is resolved, after testing " foreach, where, and if alone did not work. It worked with the full where-object not where but where-object. I still have a lot to learn. final result was
$test | Where-Object {$_.EmailAddresses -like "smtp:tcto*"}