如何将这3个powershell命令组合成一行?

时间:2015-02-18 20:05:55

标签: email powershell exchange-server powershell-v3.0 exchange-server-2010

我需要从电子邮件联系人中选择三个数据,然后将它们放入列中(export-csv样式列)。

我不确定如何从以下命令中“分割”输出。

获取主SMTP地址

ForEach ($address in Get-Mailbox -ResultSize Unlimited | Select EmailAddresses) {$address.EmailAddresses | Where {$_.Prefix -match"smtp" -and $_.IsPrimaryAddress -eq $true} | Select AddressString }

获取所有smtp地址(希望省略主要内容)

ForEach ($address in Get-Mailbox -ResultSize Unlimited | Select EmailAddresses) {$address.EmailAddresses | Where {$_.Prefix -match "SMTP" } | Select AddressString }

获取X500地址

ForEach ($address in Get-Mailbox -ResultSize Unlimited | Select EmailAddresses) {$address.EmailAddresses | Where {$_.Prefix -match "x400" } | Select AddressString }

如何将它们加入一个列?

1 个答案:

答案 0 :(得分:2)

我很可能会做这样的事情

$results =  Get-Mailbox -ResultSize Unlimited | ForEach-Object{
    $props = @{
        Name = $_.alias
        Primary = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -cmatch "SMTP" -and $_.IsPrimaryAddress -eq $true} | 
                Select-Object -ExpandProperty AddressString 
        Secondary = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -cmatch "smtp" } | 
                Select-Object -ExpandProperty AddressString 
        x400 = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -match "x400" } |  
                Select-Object -ExpandProperty AddressString   
    }

    New-Object –TypeName PSObject -Property $props
} 

$results | Export-CSV -NoTypeInformation C:\temp\export.csv

我在我的机器上使用[pscustomobject]在ISE中对此进行了编码,但我的Exchange服务器只是PowerShell版本2.0。已更新为将New-Object与电子邮件的属性一起分解,并使用邮箱别名来帮助识别输出。

您还需要使用-cmatch,因为主要和次要与字符串“SMTP”的情况相关联

如果您拥有至少3.0版PowerShell,那么这也可以使用

Get-Mailbox -ResultSize Unlimited | ForEach-Object{
    [pscustomobject]@{
        Name = $_.alias
        Primary = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -cmatch "SMTP" -and $_.IsPrimaryAddress -eq $true} | 
                Select-Object -ExpandProperty AddressString 
        Secondary = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -cmatch "smtp" } | 
                Select-Object -ExpandProperty AddressString 
        x400 = $_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -match "x400" } |  
                Select-Object -ExpandProperty AddressString   
    }
} | Export-CSV -NoTypeInformation C:\temp\export.csv
关于辅助地址的

旁注

如果你有多个输出,如果用另一个字符分隔输出可能会更好。

        Secondary = ($_ | Select -ExpandProperty emailaddresses | 
                Where {$_.Prefix -cmatch "smtp" } | 
                Select-Object -ExpandProperty AddressString) -join ";"

该代码现在可以正常运行,如果只有一个辅助代码,那么它就是无害的。