简体   繁体   English

Powershell导出带有列的CSV

[英]Powershell export csv with columns

i want to export csv file with columns "ParentGroupName", "MemberName", "DisplayName" 我想导出带有“ ParentGroupName”,“ MemberName”,“ DisplayName”列的csv文件

At the moment it is exporting the three datas into one column. 目前,它将三个数据导出到一列中。

function getGroups{

    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname

    return $Groups
}

Measure-Command{ 


$Groups = getGroups
write-host "Groups:" $Groups.Count 

}

Measure-Command{



$date = $(get-date).ToString("dd MMM yyyy")
$global:FileName = "Active Directory Group Membership - DCM -" + $date

$stringBuilder = New-Object System.Text.StringBuilder

foreach ($GroupName in $Groups){

        Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object {
            $ParentGroupName = (Get-ADGroup -Identity $GroupName).SamAccountName 
            $MemberName = $_.SamAccountName # Member of the Group.


            if ($_.ObjectClass -eq 'group') {



                $DisplayName = ' - '

            } elseif ($_.ObjectClass -eq 'user') {


                $a = (Get-ADUser -Identity $MemberName -Properties Displayname)
                $DisplayName = $a.DisplayName 

            }



            $null = $stringBuilder.Append("$ParentGroupName, $MemberName, $DisplayName")


        }
    } 


outputArray = $stringBuilder.ToString()


out-file C:\Users\augut\Desktop\$FileName.csv


outputArray | out-file C:\Users\augut\Desktop\$FileName.csv
}

You're making a headache for yourself by manually constructing the CSV file. 通过手动构建CSV文件,您为自己感到头疼。 This can be simplified considerably by constructing a custom object for each item found with the properties you need recorded, then stuffing those into an array and exporting that array to a CSV file. 通过为找到的具有所需记录属性的每个项目构造一个自定义对象,然后将其填充到数组中并将该数组导出到CSV文件,可以大大简化此过程。

function getGroups{
    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname
    return $Groups
}
$Groups = getGroups
write-host "Groups:" $Groups.Count 

$date = $(get-date).ToString("dd MMM yyyy")
$global:FileName = "Active Directory Group Membership - DCM -" + $date

$results = @();

foreach ($GroupName in $Groups){
        Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object {
            $ItemProperties = @{
                "ParentGroupName" = (Get-ADGroup -Identity $GroupName).SamAccountName;
                "MemberName" = $_.SamAccountName
            }

            if ($_.ObjectClass -eq 'group') {
                $ItemProperties.Add("DisplayName","-");
            } elseif ($_.ObjectClass -eq 'user') {
                $ItemProperties.Add("DisplayName",(Get-ADUser -Identity $MemberName -Properties DisplayName).DisplayName);
            }
            $MyItem = New-Object -TypeName psobject -property $ItemProperties;
            $Results += $MyItem;
            $ItemProperties = $null;
        }
    } 
$results | export-csv -path "C:\Users\augut\Desktop\$FileName.csv" -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM