Trying to search exchange 2007 smtp addresses

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&quot;, ($_&#46;EmailAddresses))}})<br><br>$test</code><br><br>Here is the result of the 1st part $test<br>&gt;&gt; Running (SmtpAccountCheck.ps1) Script...<br>&gt;&gt; Platform: V2 64Bit (STA)<br><br>Name DisplayName PrimarySmtpAddress EmailAddresses<br>---- ----------- ------------------ --------------<br>Ethel Edwards Ethel Edwards <!-- e --><a href="mailto:eedwards@tcctech.net">eedwards@tcctech.net</a><!-- e --> SMTP:eedwards@tcctech.net<br>Etta D. Layug Etta D. Layug <!-- e --><a href="mailto:elayug@tcctech.net">elayug@tcctech.net</a><!-- e --> SMTP:elayug@tcctech.net<br>Elizabeth T. Harris Elizabeth T. Harris <!-- e --><a href="mailto:etharris@tcctech.net">etharris@tcctech.net</a><!-- e --> SMTP:etharris@tcctech.net<br>Ed Tolleson Ed Tolleson <!-- e --><a href="mailto:etolleson@tcctech.net">etolleson@tcctech.net</a><!-- e --> smtp:tctolle@tcctech.net...<br>Erica Tripp-Wiggins Erica Tripp-Wiggins <!-- e --><a href="mailto:etrippwiggins@tcctech.net">etrippwiggins@tcctech.net</a><!-- e --> SMTP:etrippwiggins@tcctech.net<br>Emma Truitt Emma Truitt <!-- e --><a href="mailto:etruitt@tcctech.net">etruitt@tcctech.net</a><!-- e --> SMTP:etruitt@tcctech.net<br>Elizabeth K. Turcic Elizabeth K. Turcic <!-- e --><a href="mailto:eturcic@tcctech.net">eturcic@tcctech.net</a><!-- e --> SMTP:eturcic@tcctech.net<br><br>&gt;&gt; Execution time: 00:00:08<br>&gt;&gt; Script Ended<br><br>2nd part<br><code>$test | foreach {$_&#46;EmailAddresses -like 'smtp&#58;tctolle*'}</code><br><br>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.<br>&gt;&gt; Running (SmtpAccountCheck.ps1) Script...<br>&gt;&gt; Platform: V2 64Bit (STA)<br>False<br>False<br>False<br>True<br>False<br>False<br>False<br>&gt;&gt; Execution time: 00:00:09<br>&gt;&gt; Script Ended</blockquote>by DonJ at 2013-02-22 04:31:32<blockquote>In the second part, you're simply performing a comparison, which is always going to produce True/False. You need to add logic:<br><br>if ($_.EmailAddresses -like 'smtp:tctolle*') { write $_.EmailAddresses }<br><br>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.</blockquote>by mjolinor at 2013-02-22 05:25:31<blockquote>You're concatenating all the smtp addresses into a single string, which means that this:<br><br><code>$test | foreach {$_.EmailAddresses -like &#39;smtp]<br><br>Will only return $true if the first address in the list matches. <br>You need to wildcard both ends of that search argument so that it will match anywhere in that string:<br><br><code>$test | foreach {$_.EmailAddresses -like &#39;*smtp]</code></code></blockquote>by etolleson at 2013-02-22 08:14:00<blockquote>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<br><code>Add-PSSnapin Microsoft&#46;Exchange&#46;Management&#46;PowerShell&#46;Admin<br> Add-PSSnapin Microsoft&#46;Exchange&#46;Management&#46;Powershell&#46;Support<br><br> $user = Read-Host &quot;Enter account to search for&quot;<br> $test = (Get-Mailbox &quot;$user&quot; | select Name, DisplayName, PrimarySmtpAddress,<br> @{Name='EmailAddresses';Expression={&#91;string&#93;&#58;&#58;join(&quot;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*"}