0

I'm attempting to merge several cmdlets from Azure PowerShell into one object. For example:

$rbacroles = Get-AzRoleAssignment -ObjectId $user.id
$obj = New-Object -TypeName psobject
$obj | Add-Member -MemberType NoteProperty -Name AzureRoles -Value $rbacroles.roleDefinitionName
$obj | Add-Member -MemberType NoteProperty -Name Scope -Value $rbacroles.scope

However, sometimes I have to convert a list Ids into names, as such

ForEach ($aadroleid in $aadroles)
{
$id = $aadrole.roleDefinitionId
$uri = "https://graph.microsoft.com/beta/roleManagement/directory/roleDefinitions/$id"
$roledef = Invoke-RestMethod -Headers $Headers -Uri $uri
$obj | Add-Member -MemberType NoteProperty -Name AzureADRoles -Value $roledef.displayName
}

The problem I'm running into, is that when I go to add another member of the same name in the ForEach loop, it will only overwrite if I add -Force, but I really just want it appended.

Ryvik
  • 363
  • 1
  • 3
  • 14
  • Very similar to https://stackoverflow.com/questions/62971092/add-member-to-in-foreach-object-loop – Nico Nekoru Jul 18 '20 at 22:24
  • Maybe I'm reading it wrong but that did not help. – Ryvik Jul 18 '20 at 22:30
  • so you want `Prop1 = Value1` to become `Prop1 = Value1, Value2`? so that the value becomes an array of values? or a string of values? or a collection of some sort? – Lee_Dailey Jul 18 '20 at 22:33
  • Correct, an array would be fine – Ryvik Jul 18 '20 at 23:11
  • 6
    @Rooskyyy - then, instead of making a property ... add to it. [*grin*] `$Object.PropertyOne = @($Object.PropertyOne, $NewArrayItem)` might work. or you could define the Prop as a `Generic.List` and then use `.Add()` to add more items to it. – Lee_Dailey Jul 18 '20 at 23:21

0 Answers0