PowerShell在单独的行上拆分值

时间:2015-05-27 20:01:55

标签: powershell powershell-v2.0 powershell-v3.0

我的脚本如下。我想运行并获取所有Office 365邮箱的报告。但是一个属性 EmailAddresses 具有超过值,我想通过删除开头的 SMTP 来清理,这部分没问题,它是在最终我被困住了,我想根据需要根据电子邮件地址的数量将这一部分拆分成多行。我计算,但在底部构建自定义数组并不起作用。

Get-Mailbox `
           | ForEach{
           $DisplayName = $_.DisplayName
           $PrimarySmtpAddress = $_.PrimarySmtpAddress
           $alias = $_.alias
           $UserPrincipalName = $_.UserPrincipalName
           $EmailAddresses = $_.EmailAddresses
           $OtherProxyAddress = $_.EmailAddresses -cmatch "smtp:" -replace "smtp:",""
           $StepProxyAddress = ($OtherProxyAddress).count
           $number = 0
           $UserPrincipalName | Select-Object `
           @{n="DisplayName";e={$DisplayName}},`
           @{n="alias";e={$alias}},`
           @{n="UserPrincipalName";e={$UserPrincipalName}},`
           @{n="PrimarySmtpAddress";e={$PrimarySmtpAddress}},`
           @{Name="PrimaryProxyAddresses"; Expression={$EmailAddresses -cmatch "SMTP:" -replace "SMTP:",""}},`
           do {@{Name="OtherProxyAddress" + $number++; Expression={($OtherProxyAddress)[$number]}}} until($number -eq $StepProxyAddress)
           }

以下是 $ OtherProxyAddress 的原始值:

fred.smith@tenant.onmicrosoft.com
fred.smith@company.com

我想要输出(特别是这个属性):

OtherProxyAddress1  fred.smith@tenant.onmicrosoft.com
OtherProxyAddress2  fred.smith@company.com

1 个答案:

答案 0 :(得分:0)

您可能想要这样的事情(假设EmailAddresses属性包含smtp:localpart@example.com形式的电子邮件地址数组:

Get-Mailbox | % {
  $o = New-Object -Type PSCustomObject -Property @{
         'DisplayName'         = $_.DisplayName
         'alias'               = $_.alias
         'UserPrincipalName'   = $_.UserPrincipalName
         'PrimarySmtpAddress'  = $_.PrimarySmtpAddress
         'PrimaryProxyAddress' = ($_.EmailAddresses | select -First 1) -replace '^smtp:'
       }

  $i = 1
  $_.EmailAddresses | select -Skip 1 | % {
    $o | Add-Member -Type NoteProperty -Name "OtherProxyAddress$i" `
      -Value ($_ -replace '^smtp:')
    $i++
  }

  $o
}

首先使用" simple"构建自定义对象。属性,然后在嵌套循环中使用Add-Member将每个电子邮件地址作为单独的属性添加到该对象。