Powershell自定义将对象附加到csv文件

时间:2017-11-28 10:18:49

标签: powershell csv object append

我尝试将自定义对象输出到csv格式的文本文件,因为我循环访问每个文件。每行一个对象。

但没有任何内容写入文件。

是否有要转换类型的东西?

$rechten = Get-ADGroupMember -Identity $v -Recursive -ERRORACTION silentlycontinue | Get-ADUser -Property DisplayName -ERRORACTION silentlycontinue | Select-Object Name

Write-Host -ForegroundColor Yellow "ADgroup $v wordt uitgevlooid."

foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject
    $objResults | Add-Member -MemberType NoteProperty -Name DirectoryPath -Value $objPath
    $objResults | Add-Member -MemberType NoteProperty -Name Identity -Value    $rechtenhouder.name
    $objResults | Add-Member -MemberType NoteProperty -Name Systemrights -Value $accessRight.FileSystemRights
    $objResults | Add-Member -MemberType NoteProperty -Name systemrightstype -Value $accessRight.accesscontroltype
    $objResults | Add-Member -MemberType NoteProperty -Name isinherited -Value $accessRight.isinherited
    $objResults | Add-Member -MemberType NoteProperty -Name inheritanceflags -Value $accessRight.inheritanceflags
    $objResults | Add-Member -MemberType NoteProperty -Name rulesprotected -Value $objACL.areaccessrulesprotected
    $objResults | Add-Member -MemberType NoteProperty -Name Adtype -Value "User"

    $arrResults += $objResults
    Add-Content $exportpathtxtappend $objresults
}

2 个答案:

答案 0 :(得分:0)

首先,我建议你以一种更聪明的方式创建你的对象:

foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject -Property @{
        DirectoryPath    = $objPath;
        Identity         = $rechtenhouder.name;
        Systemrights     = $accessRight.FileSystemRights;
        systemrightstype = $accessRight.accesscontroltype;
        isinherited      = $accessRight.isinherited;
        inheritanceflags = $accessRight.inheritanceflags;
        rulesprotected   = $objACL.areaccessrulesprotected;
        Adtype           = "User";
    }
    $arrResults += $objResults
}

完成此操作后,您的$arrResults现在包含您的对象。这可以使用内置Export-CSV内的PowerShell轻松导出为CSV文件:

$arrResults | Export-Csv -Path "C:/temp/text.csv"

在每次循环迭代中使用Add-Content是IMHO对性能无效的。如果您的脚本运行了很长时间,并且您想要间隔保存当前状态,则可以例如启动异步作业 - 假设每10次迭代 - 导出当前数组:

$i = 0
foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject -Property @{
        DirectoryPath    = $objPath;
        Identity         = $rechtenhouder.name;
        Systemrights     = $accessRight.FileSystemRights;
        systemrightstype = $accessRight.accesscontroltype;
        isinherited      = $accessRight.isinherited;
        inheritanceflags = $accessRight.inheritanceflags;
        rulesprotected   = $objACL.areaccessrulesprotected;
        Adtype           = "User";
    }
    $arrResults += $objResults
    if ($i % 10 -eq 0) {
        Start-Job -ScriptBlock {
            param($T, $Path)
            $T | Export-Csv -Path $Path
        } -ArgumentList @($arrTest, "Path/to/script")
    }
    $i++
}

答案 1 :(得分:0)

对于您的特定用途,一次或批量导出所有对象将是最有效的,但是有时一次将一个记录导出到CSV文件是有意义的,这就是导致我提出此问题的原因,所以我想发布我的解决方案。

使用Export-CSV -Append连续添加到csv文件的末尾。

foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject -Property @{
        DirectoryPath    = $objPath;
        Identity         = $rechtenhouder.name;
        Systemrights     = $accessRight.FileSystemRights;
        systemrightstype = $accessRight.accesscontroltype;
        isinherited      = $accessRight.isinherited;
        inheritanceflags = $accessRight.inheritanceflags;
        rulesprotected   = $objACL.areaccessrulesprotected;
        Adtype           = "User";
    }

    $objResults | Export-CSV $csvPath -Append -NoTypeInformation
}

如果您按设置的时间间隔连续进行轮询,这将很有用,但是如果您要遍历一组对象,则一次将它们全部导出,则该功能就不那么有用了。例如,我将对以下脚本使用这种导出方法:

while($true){
    $procs = Get-Process | Select-Object Name,CPU
    $procs | Add-Member -type NoteProperty -Name "Timestamp" -Value $(Get-Date)

    $procs | Export-CSV $csvPath -Append -NoTypeInformation

    sleep -Seconds 60
}