2016-07-31 118 views
2

下面的代碼旨在列出特定父支持組的所有成員。我想分開「成員」數組中的所有值,並將輸出中的逗號包括到CSV中。我該怎麼做?輸出爲CSV包括逗號

Set-ExecutionPolicy Bypass -Force 

#Report start of script actions. 
Write-Output "Discovering Security Group members..." 

# Install Remote Server Administration Tools if not already installed. 
if (!(Get-Module -ListAvailable -Name ActiveDirectory)) { 
    Install-WindowsFeature RSAT-AD-PowerShell 
} 

# Import the ActiveDirectory module. 
Import-Module ActiveDirectory 

#Prevent truncated output of objects in the Members array. 
$FormatEnumerationLimit = -1 

#Define the Export-CSV output location. 
$OutputFile = "$home\Desktop\Web_Filtering_Group_memberships_" + (Get-Date -Format "M.d.yyyy-HHmm") + ".csv" 

#Prevent truncated output of arrays. 
$FormatEnumerationLimit = -1 

#Discovering Security Group members. 
Get-ADGroup -SearchBase 'OU=Parent Group,OU=Security Groups,DC=domain,DC=com' -Filter * -Properties * | 
    Select-Object -Property Name, Description, GroupCategory, 
     @{Name='Members';exp={ 
      Get-ADGroupMember $_.SamAccountName | Select -ExpandProperty SamAccountName 
     }} | 
    Export-CSV -NoTypeInformation $OutputFile 

#Report end of script actions. 
Write-Output "Discovery of all Web Filtering Group members is complete. Output saved to: $OutputFile" 

回答

0

成員只需加入一個逗號分隔的字符串:

... | Select-Object Name,Description,GroupCategory,@{n='Members';e={ 
    (Get-ADGroupMember $_.SamAccountName | Select -Expand SamAccountName) -join ',' 
}} | Export-Csv ... 

Export-Csv將所有出口領域在雙引號,所以你會得到這樣的輸出:

"Name","Description","GroupCategory","Members" 
...,"foo,bar,baz" 
...,"some" 
...,"or,other"

字符串值中的嵌套逗號是有效的CSV。

+0

太棒了!正是我需要的。謝謝。 – newyorkstateofmind