2015-05-27 69 views
0

我在下面有一個腳本。我想運行並獲取所有Office 365郵箱的報告。但有一個屬性EmailAddresses具有超過價值,我想通過在開始時刪除SMTP進行清理,這部分沒有問題,它是最終分割的結果,我堅持使用,我想將其分解爲基於它們的電子郵件地址數量,根據需要提供許多行。我做計數,但在底部構建自定義數組不起作用。單獨的PowerShell拆分值

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原值:

[email protected] 
[email protected] 

我想要的輸出(特別是對於該屬性)是:

OtherProxyAddress1 [email protected] 
OtherProxyAddress2 [email protected] 
+0

原始值和期望結果的示例可能有所幫助。 –

+0

這裏是'$ OtherProxyAddress'的原始值: [email protected] [email protected] 我想要的輸出(特別是對於該屬性)是: ' OtherProxyAddress1 \t [email protected] OtherProxyAddress2 \t fred.smith @ company.com' –

+0

@MarcKean這很好,但你可以添加到你的問題,並告訴我們你想要的輸出在問題中看起來像什麼。我問,因爲看到你想要的是我們確信的唯一方法。我可以猜測,但如果我錯了,這將浪費我的時間。 – Matt

回答

0

你可能會想要這樣的事情(假設EmailAddresses屬性包含smtp:[email protected]形式的電子郵件地址數組):

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 
} 

首先,建立與「簡單」屬性的自定義對象,那麼您可以通過在嵌套循環利用Add-Member添加的每個電子郵件地址作爲一個單獨的屬性到該對象。

+0

如果對象具有超出第一個創建的更多附加地址,這不會引起問題嗎?我打算這樣做,但要計算最大地址數量,以便每個對象具有相同的金額 – Matt

+0

@Matt我對這個問題的理解是他希望每個地址有一個屬性,而不管有多少個地址。但他可能想跳過第一個地址,因爲這已經是'PrimaryProxyAddress'了。更新。 –

+0

我改變了行(第8行和第12行)''PrimaryProxyAddress'= $ _。EmailAddresses -cmatch「SMTP:」-replace'^ smtp:''and'$ _。EmailAddresses -cmatch「smtp:」| %{'使用cmatch更加精確,因爲大寫SMTP地址是主要代理地址。但是,在此過程中,我現在在結果中的電子郵件地址的任意一側都有** {} **。我如何從結果中刪除{}? –