'powershell export to csv ISSUE

I need to fetch data with the request API and export it to a CSV file, each column will need to be the name of a group and below all the members (the group name and the members are information that I receive from the request API).

This is the code:

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json")

    $response = Invoke-RestMethod 'https://X.X.X.X/policy/api/v1/infra/domains/default/groups/<group-name>/members/virtual-machines' -Method 'GET' -Headers $headers

    $members_of_group = $response.results

    $out_csv =@()

    for ($i=0; $i -lt $members_of_group.Length; $i++)

    {
     $members = $members_of_group[$i].display_name
     Write-Output $members
     $out_csv += $members
     $count = $i
    }
    $out_csv | Export-Csv $path -NoTypeInformation

This is the output: enter image description here

How can I export each group name with all the members?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source