如何在powershell中导出到csv

时间:2014-03-12 13:42:53

标签: powershell csv

我有以下代码,我想导出到csv文件。不确定我将把export-csv行放在哪里?

Get-ChildItem \\server\share| Where {
    $_.PSIsContainer
} | Get-ACL | ForEach {
    $Identity = $_.Access | Where {$_.IdentityReference -like '*-W'}
    $Group = ($Identity.IdentityReference -split '\\')[1]
    If ($Identity) {
        "Found $($Identity.IdentityReference)"
        #Assumes you do not have ActiveDirectory module
        $Members = ([adsisearcher]"name=$($Group)").FindOne().GetDirectoryEntry().member -replace 'CN=(.*?)\,.*','$1'
        New-Object PSObject -Property @{
            FullName = Convert-Path $_.Path
            Identity = $Identity.IdentityReference
            Members = $Members

        }

    }

}

这是当前的输出

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要将所有新对象作为数组。这是通过管道化命令的结果来完成的。所以你需要将Export-CSV放在最后......

Get-ChildItem \\server\share| Where {
    $_.PSIsContainer
} | Get-ACL | ForEach {
    $Identity = $_.Access | Where {$_.IdentityReference -like '*-W'}
    $Group = ($Identity.IdentityReference -split '\\')[1]
    If ($Identity) {
        "Found $($Identity.IdentityReference)"
        #Assumes you do not have ActiveDirectory module
        $Members = ([adsisearcher]"name=$($Group)").FindOne().GetDirectoryEntry().member -replace 'CN=(.*?)\,.*','$1'
        New-Object PSObject -Property @{
            FullName = Convert-Path $_.Path
            Identity = $Identity.IdentityReference
            Members = $Members

        }

    }

} | Export-CSV c:\result.csv -NoTypeInformation -Encoding UTF8