arrays - 输出到CSV包含逗号

标签 arrays csv powershell

以下代码旨在列出特定上级支持小组的所有成员。我想将“Members”数组中的所有值分开,并在输出到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"

最佳答案

只需将成员加入逗号分隔的字符串即可:

... | Select-Object Name,Description,GroupCategory,@{n='Members';e={
  (Get-ADGroupMember $_.SamAccountName | Select -Expand SamAccountName) -join ','
}} | Export-Csv ...
Export-Csv将所有导出的字段都放在双引号中,因此您将获得如下输出:

“名称”,“描述”,“GroupCategory”,“成员”
...,“foo,bar,baz”
...,“一些”
...,“或其他”

字符串值中的嵌套逗号为有效CSV。

关于arrays - 输出到CSV包含逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38688904/

相关文章:

python - 对嵌套列表进行排序 : Exclude first item from sorting

powershell - 有没有比在管道中为单个对象使用foreach更干净的方法?

python - 使用numpy中的一维数组从二维数组中选择多个元素

c - C 中多维数组的问题

r - Rdata 文件的大小与 csv 的比较

csv - 何时使用 tensorflow datasets api 与 pandas 或 numpy

powershell - 无法将 "-persist"添加到 New-PSDrive - 出现错误 "network resource type is not correct"

windows - GetScheduled-Job 不返回任何工作

arrays - 使用数组 Swift SpriteKit 生成多个硬币节点

javascript - angularjs 将 json 对象转换为数组