1
$result = New-Object System.Collections.ArrayList
ForEach ($repoElement in $Repo.value)
{
 $repoId = $repoElement.id
 $BranchCreatorUrl = "https://dev.azure.com/xyz/_apis/git/repositories/$repoId/refs?api-version=6.1-preview.1"
 $CreateorInfo = (Invoke-RestMethod -Uri $BranchCreatorUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
 $url1= "https://dev.azure.com/xyz/_apis/policy/configurations?api-version=4.1"
 $response = (Invoke-RestMethod -Uri $url1 -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
 
 $obj=[pscustomobject]@{
            RepositoryName = $repoElement.name
            RepositoryId = $repoId
            BranchName = $CreateorInfo.value.name
            PolicyName = $response.value.type.displayname
        }
        $result += $obj
        
}
Write-Output $result


The above code gives an output 

enter image description here

I want to show all the branch name as string and not in the form of object ie this way {..,...,...,..}

Sudarshan Sharma
  • 105
  • 1
  • 11
  • 2
    `$result.BranchName` to see each object's value. `$result[0].BranchName` to see the value for the first object. `$result[0].BranchName -join ','` to see the first object's value as a comma-delimited string rather than a collection. This is all assuming you aren't actually using PowerShell v2 as you have tagged. – AdminOfThings Feb 17 '21 at 14:28
  • I want to show all the names in the branch object as comma separated string for all the repositories(rows) @AdminOfThings – Sudarshan Sharma Feb 17 '21 at 14:41
  • 1
    `$result.foreach({$_.BranchName -join ','})` will do that – AdminOfThings Feb 17 '21 at 14:56
  • 1
    You probably need to replace BranchName = $CreateorInfo.value.name with BranchName = $($CreateorInfo.value.name -join ","). – Clint Oliveira Feb 17 '21 at 14:57
  • It works. Thankyou for the help. – Sudarshan Sharma Feb 18 '21 at 15:05

1 Answers1

1

If this is just about for-display formatting, i.e. what is shown in the implicitly table-formatted display output, you can call Format-Table explicitly and use a calculated column to apply custom formatting to the BranchName column:

# Sample result.
$result = [pscustomobject]@{
  RepositoryName = 'name'
  RepositoryId = 'id'
  BranchName = 'branch1', 'branch2', 'branch3'
  PolicyName = 'policy'
}

# Use explicit Format-Table formatting with custom formatting of the 
# 'BranchName' column.
$result | Format-Table RepositoryName, 
                       RepositoryId, 
                       @{ n='BranchName'; e={ $_.BranchName -join ', ' } },
                       PolicyName

The above yields a tabular display whose BranchName column doesn't have the enclosing { ... }, which PowerShell uses to indicate that the column value is an array (perhaps confusingly, because it looks like a script block):

RepositoryName RepositoryId BranchName                PolicyName
-------------- ------------ ----------                ----------
name           id           branch1, branch2, branch3 policy
mklement0
  • 382,024
  • 64
  • 607
  • 775