是否可以仅从 public 文件夹中提取 SMTP 地址

Is it possible just to extract the SMTP addresses from a public folder

我正在尝试使用 get-mailpublicfolder 命令

提取与我们的一些 Exchange 2010 public 文件夹关联的 SMTP 地址列表

虽然我可以提取 WindowsEmailAddress 和 EmailAddresses 值,但后者包含我想排除的 X400 地址。

我能找到的与此最接近的代码是下面列出的代码 link

https://unlockpowershell.wordpress.com/2010/01/27/powershell-get-mailbox-display-smtp-addresses/

但这似乎只适用于 Get-Mailbox,因为以下代码 returns 以下错误

$data=Get-MailPublicFolder -Identity $id |select-object WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}| ForEach-Object {$_.SmtpAddress}

 

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.Str

ing, System.Management.Automation.ScriptBlock}.

At C:\Temp\FindAllEmailAdresses_v1.ps1:33 char:56

+ $data=Get-MailPublicFolder -Identity $id |select-object <<<<  WindowsEmailAddress,@{Name="EmailAddresses";Expression=

{$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}| ForEach-Object {$_.SmtpAddress}

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException

    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.Str

ing, System.Management.Automation.ScriptBlock}.

$data=Get-MailPublicFolder -Identity $id |select WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses}|Where-Object {$_.PrefixString -ceq "smtp:"}}

 

+ $data=Get-MailPublicFolder -Identity $id |select <<<<  WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.

ilAddresses}|Where-Object {$_.PrefixString -eq "smtp:"}}

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException

    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Key "Expression" has illegal type System.Management.Automation.PSObject; expected types are {System.

ing, System.Management.Automation.ScriptBlock}.

At C:\Temp\FindAllEmailAdresses_v1.ps1:31 char:49

是否可以只提取 SMTP 地址,或者我是否也坚持提取 x400 地址? 提前致谢

编辑

根据答案,我已将代码更改为以下内容,并且它可以进行一项更改,即从 -ceq“smtp:”部分中删除冒号,因为将其保留在原位会导致电子邮件地址未返回。请注意,这是我的错误,而不是 Dougs,因为他们只是基于我之前提供的代码。

$expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses"; Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops

这就是 formatting/indentation 可以为您节省大量时间和挫败感的地方。您得到的错误是说您的表达式(e={} 计算的 属性 的一部分)只能是字符串或脚本块。简而言之,这两个命令在错误的位置都有花括号。避免这种情况的一种真正简单的方法是将代码块存储在变量中。

这是移动了大括号的原始行。

$data = Get-MailPublicFolder -Identity $id |select-object WindowsEmailAddress,@{Name="EmailAddresses"; Expression={$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"}| ForEach-Object {$_.SmtpAddress}}}

$data = Get-MailPublicFolder -Identity $id |select WindowsEmailAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses|Where-Object {$_.PrefixString -ceq "smtp:"}}}

这是我真正推荐的

$expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"} | ForEach-Object {$_.SmtpAddress}}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses"; Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops

$expression = {$_.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp:"}}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses";Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops

注意

我保留了 powershell 2.0 代码格式,以防您被困在旧版本中。如果是 5.1 那么这可能会更整洁一些。

$expression = {$_.EmailAddresses | Where-Object PrefixString -ceq "smtp:" | ForEach-Object SmtpAddress}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses"; Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops

$expression = {$_.EmailAddresses | Where-Object PrefixString -ceq "smtp:"}

$selectprops = 'WindowsEmailAddress',
               @{Name="EmailAddresses";Expression=$expression}

$data = Get-MailPublicFolder -Identity $id | Select-Object $selectprops