PS Script to look up email address
Hi - I'm trying to modify a script so that it will take an argument instead of having to modify the script each time I need it. It's just a one liner that find the name of the mailbox that has an email address.This works: (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:EMAIL REMOVED"}).name (I found this on a blog; wish I remembered where so I could credit the blog writer.)What I'd like to do is modify it so I could give it a name (say Lookup-Address.ps1,) and invoke it with the email address I'm looking up - ie:PS> .\LookUp-Address.ps1 EMAIL GONE So I need to pass the $Args (that would contain EMAIL GONE ) in the above example into the -match "smtp:EMAIL REMOVED " part of the script. The difficult part is prepending the smtp: part. I've tried all of these and none have worked: (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match $Args}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "$Args"}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match smtp:"$Args"}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:$Args"}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp: + "$Args""}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match ("smtp:"+$Args)}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:" + $Args}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match ("smtp:" + $Args)}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp: + $Args"}).nameI've tried a few -contains where -match is above & have had no luck there either.Anybody have any suggestions?? Thanks very much!
October 8th, 2009 11:22pm

I see where the powers that be have edited my post so that syntactically correct smtp addresses are replaced with EMAIL REMOVED and EMAIL GONE. I hope you can figure out that where those two text pieces are, in actuality there is a real, legitimate email address like "username (AT) emaildomain dot EDU.Thanks.
Free Windows Admin Tool Kit Click here and download it now
October 8th, 2009 11:25pm

Put the line below in the LookUp-Address.ps1 ======== LookUp-Address.ps1======== (Get-Mailbox -ResultSize Unlimited | Where {$_.EmailAddresses -match "smtp:$a"}).name ======== LookUp-Address.ps1======== Launch EMS [PS] C:\>$a = “EMAIL REMOVED” [PS] C:\> ./LookUp-Address.ps1 James Luo
October 9th, 2009 5:35am

On Thu, 8-Oct-09 20:22:03 GMT, RussPa53 wrote:>Hi - I'm trying to modify a script so that it will take an argument instead of having to modify the script each time I need it. It's just a one liner that find the name of the mailbox that has an email address.>>This works: (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:EMAIL REMOVED"}).name (I found this on a blog; wish I remembered where so I could credit the blog writer.)>s I'm looking up - ie:>PS> .\LookUp-Address.ps1 EMAIL GONE >>So I need to pass the $Args (that would contain EMAIL GONE ) in the above example into the -match "smtp:EMAIL REMOVED " part of the script. >>The difficult part is prepending the smtp: part. I've tried all of these and none have worked:>> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match $Args}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "$Args"}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match smtp:"$Args"}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:$Args"}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp: + "$Args""}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match ("smtp:"+$Args)}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp:" + $Args}).name> (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match ("smtp:" + $Args)}).name (Get-Mailbox -ResultSize Unlimited | ? {$_.EmailAddresses -match "smtp: + $Args"}).name>>I've tried a few -contains where -match is above & have had no luck there either.>>Anybody have any suggestions?? Thanks very much!Well, I think I'd try using something like "param([string]$addr)" soyou get a named parameter to work with. Then, before you start usingit, modify it: "$addr = 'smtp:' + $addr" (or $addr = "smtp:$addr"). The -match operator is using your attempt to concatenate the 'smtp:'and $args as a literal RegEx and not interpolating the string.---Rich MatheisenMCSE+I, Exchange MVP--- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
October 9th, 2009 6:15am

Rus, Honestly why are you gathering all the mailboxes in your org to find an email address? Get-Mailbox will find the mailbox that has that email address whether it's primary or not, so if you want to still make it a script file do this. PS C:\>.\LookUp-Address.ps1 EMAIL REMOVED ##LookUP-Address (Get-Mailbox $Args[0]).name BTW I tested this in my environment and it works, but you do need to specify the location in the argument collection for this to work. However, that is not the case where passing it to a WHERE. Sr. Exchange Engineer - Constellation Energy
October 9th, 2009 4:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics