【问题标题】:How to export to csv in powershell如何在powershell中导出为csv
【发布时间】:2014-03-12 13:42:52
【问题描述】:

我有下面的代码,我想导出到 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

        }

    }

}

这是当前的输出

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    您需要将所有新对象作为一个数组。这是通过流水线命令的结果来完成的。所以你需要把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
    

    【讨论】:

    • 当我这样做时它没有导出数据?只是说#TYPE System.String 长度
    • 如果你在没有export-csv的情况下运行脚本,你的数据是如何呈现的?你能在你的问题中加入一个真实的(但经过消毒的)样本吗?
    • 好的,我在上面加了一张图片
    • 我仍然坚持我的回答。如果这不起作用,我不确定出了什么问题。
    • @user1342164:显示Export-Csv 创建的输出。
    猜你喜欢
    • 2014-01-29
    • 2017-12-31
    • 2022-01-14
    • 2018-01-05
    • 2019-06-09
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多