0

I have following ConvertFrom-Json output from JSON file:

Id                 : 1
ItemName           : TestFile
SharingInformation : {@{[email protected]; ResharePermission=Read}, @{[email protected]; ResharePermission=Read}}

I would like to save this data to .csv file in following manner as columns:

Id                 : 1
ItemName           : TestFile
Users              : (read) [email protected] ; (write) [email protected]

as columns.. Here you can find part of my actual PS code (which do not work properly when there is more than one embedded values):

$JSONFile = $ExctratedFile | ConvertFrom-Json
$psObjectForCsv = $JSONFile | ForEach-Object {
    [PSCustomObject]@{
        "id"=$_.Id
        "ItemName"=$_.ItemName
        "RecipientEmail"=$_.SharingInformation.RecipientEmail
        "ResharePermission"=$_.SharingInformation.ResharePermission

    }
}
$psObjectForCsv | Export-Csv -path $fileName -Force -NoTypeInformation
} 

do you have any ideas how to achieve this?

Thank you for your help! Regards

1 Answer 1

1

You can format the SharingInformation in any form you like.

Try

$JSONFile = $ExctratedFile | ConvertFrom-Json
$result   = $JSONFile | ForEach-Object {
    # output a formatted string "(permission) emailaddress" 
    $users = foreach ($shareInfo in $_.SharingInformation) {
        '({0}) {1}' -f $shareInfo.ResharePermission, $shareInfo.RecipientEmail
    }
    [PSCustomObject]@{
        id       = $_.Id
        ItemName = $_.ItemName
        Users    = $users -join "; "
    }
}

$result | Export-Csv -Path $fileName -Force -NoTypeInformation
Sign up to request clarification or add additional context in comments.

2 Comments

that's working:) is there any way to sort values by $shareInfo.ResharePermission ?
@anders1990 You could do $users = foreach ($shareInfo in ($_.SharingInformation | Sort-Object ResharePermission)) { '({0}) {1}' -f $shareInfo.ResharePermission, $shareInfo.RecipientEmail }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.