2017-08-07 35 views
0

我已經編寫了一個腳本來從Active Directory和Exchange中返回一些詳細信息,然後將這些詳細信息放入數組中。腳本不向新列添加新數據

問題是Exchange部分覆蓋了數組中的現有值,而我希望它基本上爲Exchange數據創建一個新列並將其放在那裏。

這裏的腳本:

$adusers = Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail | 
      Where-Object { $_.Name -like $_."Name" } | 
      Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail 

$adusers | ForEach-Object { 
    $mailboxstats = Get-Mailbox $_.mail | 
        Get-MailboxStatistics | 
        Select-Object TotalItemSize 
    $adusers += $mailboxstats 
} 

return $adusers 
+1

'Where-Object {$ _。Name-like $ _。「Name」}'你知道這總是對的,對嗎? –

回答

1

使用郵箱大小calculated property。此外,條件$_.Name -like $_."Name"始終爲真,因此您可以簡單地刪除Where-Object篩選器,並且不需要關鍵字return,因爲PowerShell函數總是返回Success output stream上的所有未捕獲輸出。

更改您的代碼是這樣的:

Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail | 
    Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail, 
     @{n='MailboxSize';e={ 
      Get-Mailbox $_.mail | 
       Get-MailboxStatistics | 
       Select-Object -Expand TotalItemSize 
     }} 
0

使用Select-Object修改集合中的項目:

return $adusers | Select *,@{Name='TotalItemSize';Expression={ 
    $mailboxstats = Get-Mailbox $_.mail | 
        Get-MailboxStatistics | 
        Select-Object -Expand TotalItemSize}} 
0

謝謝你這麼多了點。這完美地完成了這個技巧。

謝謝!!!

Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail | 
    Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail, 
     @{n='MailboxSize';e={ 
      Get-Mailbox $_.mail | 
       Get-MailboxStatistics | 
       Select-Object -Expand TotalItemSize 
     }}