Windows

PowerShell:獲取 Exchange 通訊組的註釋

  • January 27, 2022

我想建構一個 One-Liner 來獲取特定通訊組的註釋並將其與通訊組的名稱和其他資訊一起輸出。

我用Google搜尋了它,發現所有相同解決方案的不同來源。這是我找到的解決方案之一:

https://richgski.blogspot.com/2012/03/powershell-get-exchange-distribution.html

Get-DistributionGroup Head-of-Operations | Select-Object Name, GroupType, ManagedBy, @{Name="Notes";Expression={(Get-Group $_).Notes}}

但是,帶有註釋的行將始終保持空白,我不知道為什麼:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

當我單獨執行以下命令時:

Get-Group Head-of-Ops | Select-Object Notes

…它給了我正確的註釋作為輸出:

Notes
-----
Owner- Paul J.

不久之後,我檢查了是否有正確的參數,所以我嘗試這樣做:

$Result = Get-DistributionGroup Head-of-Operations
Get-Group $Result.Name | Select-Object Notes

有效。輸出:

Notes
-----
Owner- Paul J.

我對命令進行了一些更改,但是當我以這種方式嘗試時,它仍然無法正常工作:

Get-DistributionGroup Head-of-Operations | Select-Object Name,GroupType,ManagedBy,@{Name="Notes";Expression={(Get-Group $_.Name | Select-Object Notes)}}

輸出:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

之後,我在這裡找到了這個主題的另一個文章: https ://www.oxfordsbsguy.com/2014/04/21/exchange-powershell-how-to-enumerate-distribution-lists-managers-and-members/#comment -4452

所以我再次對命令進行了一些更改……

Get-DistributionGroup Head-of-Ops | Select-Object Name,GroupType,ManagedBy,@{Expression={(Get-Group $_.Name).Notes};Label="Notes"}

……仍然,什麼都沒有改變。輸出:

Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

我只是不明白:/

你們中有人看到這個問題並可以指出我嗎?

親切的問候,

凱文·範泰爾

pS:我現在已經使用 -verbose 參數執行了這個命令,我認為我現在離解決方案更近了一步。我認為在某些時候它只是缺少一個參數,但我不知道是哪個參數。這是程式碼:(我用 * 星號標記了敏感資訊)

Get-DistributionGroup Head-of-Ops@h***.com -Verbose | Select-Object Name,GroupType,ManagedBy,@{Name="Notes";Expression={(Get-Group $_.Name).Notes}} -Verbose
VERBOSE: [16:04:28.885 GMT] Get-DistributionGroup : Active Directory session settings for 'Get-DistributionGroup' are: View Entire Forest: 'False', Default Scope: 'h***.de', Configuration Domain Controller: 'H***.h***.de',
Preferred Global Catalog: '***.h***.h***.de', Preferred Domain Controllers: '{ ****.h***.h***.de, H***.h***.de }'
VERBOSE: [16:04:28.916 GMT] Get-DistributionGroup : Runspace context: Executing user: h***.de/Companies/H***/D***/User/IT Service/****, Executing user organization: , Current organization: , RBAC-enabled: Enabled.
VERBOSE: [16:04:28.916 GMT] Get-DistributionGroup : Beginning processing &
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Current ScopeSet is: { Recipient Read Scope: {{, }}, Recipient Write Scopes: {{, }}, Configuration Read Scope: {{, }}, Configuration Write Scope(s): {{, }, }, Exclusive Recipient
Scope(s): {}, Exclusive Configuration Scope(s): {} }
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Resolved current organization: .
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Searching objects "Head-of-Ops@h***.com" of type "ADGroup" under the root "$null".
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Previous operation run on domain controller 'H***.h***.de'.
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Previous operation run on domain controller 'H***.h***.de'.
VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Preparing to output objects. The maximum size of the result set is "1000".

VERBOSE: [16:04:28.947 GMT] Get-DistributionGroup : Ending processing &
Name        GroupType ManagedBy Notes
----        --------- --------- -----
Head-of-Ops Universal {}

以下行是我認為問題被埋沒的行:

VERBOSE: [16:04:28.932 GMT] Get-DistributionGroup : Searching objects "Head-of-Ops@h***.com" of type "ADGroup" under the root "$null".

任何人都知道它缺少什麼以及為什麼?

使用Select-Object您需要 創建計算屬性時

  1. 一個表達式,可以縮寫為 E/e
  2. 名稱標籤,它們是同義詞,也可以簡化為第一個字母。

從您的問題來看,不清楚是Get-DistributionGroup Head-of-Operations返回單個對像還是多個對象?

Head-of-Operations一樣Head-of-Ops嗎?

通過 Select-Object 添加屬性的另一種方法是創建一個[PSCustomObject]

foreach($HeadOP in Get-DistributionGroup Head-of-Operations){
   [PSCustomObject]@{
       Name      = $HeadOp.Name
       GroupType = $HeadOp.GroupType
       ManagedBy = $HeadOp.ManagedBy
       Notes     = (Get-Group $HeadOp.Name | Select-Object Notes)
   }
}

最終你需要-ExpandProperty Notes

       Notes     = (Get-Group $HeadOp.Name | Select-Object -ExpandProperty Notes)

或更短

       Notes     = (Get-Group $HeadOp.Name).Notes

引用自:https://serverfault.com/questions/958967