2015-08-27 78 views
0

以下PowerShell腳本生成CSV有三列(組,用戶SAM帳戶名),其與相應的構件的規定分佈組的每個實例相關聯的文件:交換PowerShell腳本變形

$ DIST =的ForEach($組in(Get-DistributionGroup -Filter {name -like「*」})){Get-DistributionGroupMember $ group |選擇@ {Label =「Group」; Expression = {$ Group.Name}},@ {Label =「User」; Expression = {$ _。Name}},SamAccountName} $ dist |排序組,用戶| Export-CSV c:\ scripts \ file.csv -NoTypeInformation

它爲用戶提供了一種方便的方式來過濾組和顯示組成員。我的問題:是否有一種快速修改此腳本的方法,以便添加顯示組屬性的第四列(特別是「HiddenFromAddressListsEnabled」屬性)?

回答

0

只需將其添加到Select-對象部分

$dist = ForEach ($group in (Get-DistributionGroup -Filter {name -like "*"})) { Get-DistributionGroupMember $group | Select @{Label="Group";Expression={$Group.Name}},@{Label="User";Expression={$_.Name}},SamAccountName,HiddenFromAddressListsEnabled} $dist | Sort Group,User | Export-CSV c:\scripts\file.csv -NoTypeInformation 

看到它的SAM帳戶

後獲得HiddenFromAddressListsEnabled的羣組:

$dist = ForEach ($group in (Get-DistributionGroup -Filter {name -like "*"})) { Get-DistributionGroupMember $group | Select @{Label="Group";Expression={$Group.Name}},@{Label="User";Expression={$_.Name}},SamAccountName,{$Group.HiddenFromAddressListsEnabled}} $dist | Sort Group,User #| Export-CSV c:\scripts\file.csv -NoTypeInformation 

然而,還有另一種方式來做到這一點,並更容易閱讀和操作:

$Array = @() 
$Groups = Get-DistributionGroup 
Foreach ($Group in $Groups) 
{ 
$DGMembers = Get-DistributionGroupMember $Group 
Foreach ($Member in $DGMembers) 
    { 
    $Result = "" | Select GroupName,Member,SamAccountName,HiddenFromAddressListsEnabled 
    $Result.GroupName = $Group.Name 
    $Result.Member = $Member.Name 
    $Result.SamAccountName = $Member.SamAccountName 
    $Result.HiddenFromAddressListsEnabled = $Group.HiddenFromAddressListsEnabled 
    $Array += $Result 
    } 

} 

$Array | Export-CSV c:\scripts\file.csv -NoTypeInformation 
+0

謝謝你的迴應。我在這裏擔心的是,「HiddenFromAddressListsEnabled」屬性需要適用於組,而不是其中的用戶。有沒有辦法在原始腳本中配置它,還是必須完全重新設計? – JFope

+0

我看到了修訂版並測試了腳本。完美工作。非常感謝Avshalom! – JFope