woef
woef

Reputation: 57

Powershell custom append object to csv file

I'm trying to output a custom object to a csv formatted text file as I loop through a for each. One object per line.

But nothing is written to the file.

Is it something with types to be converted ?

$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
}

Upvotes: 2

Views: 7425

Answers (2)

Brandon McClure
Brandon McClure

Reputation: 1410

For your specific use exporting all objects at once or in batches would be the most efficient, but there are times were it would make sense to export a record one at a time to a CSV file which is what led me to this question, so I want to post my solution.

Use Export-CSV -Append to continually add to the end of a csv file.

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
}

This is useful if you are continually polling at set time intervals, but less so if you are iterating over a collection of objects, just export them all at once. For example, I would use this method of exporting for a script like below:

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
}

Upvotes: 1

Clijsters
Clijsters

Reputation: 4256

First, I suggest you to create your object in a decent smarter way:

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
}

With this done, your $arrResults now contains your objects. This can easily exported to CSV files with PowerShells builtin Export-CSV:

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

Using Add-Content on every loop iteration is IMHO ineffective regarding performance. If your script runs for a long time and you want to save your current state in intervals, you could e.g. start an asynchronous job - let's say every 10th iteration - exporting your current array:

$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++
}

Upvotes: 0

Related Questions