0

My goal is to export users and role membership from Azure AD and export the list into a spreadsheet. I was given this code from a colleague but I keep getting errors. The goal is to eventually compare two spreadsheets to identify if any roles were changed without authorization. I was given that code too but since I can't get the first spreadsheet, the second part won't work

I don't have much experience with Powershell but I inherited this task. I have a general understanding of the code and what it is trying to do but I can't pinpoint where the error is. I have some experience in Java and I'm looking at this code through that lens, probably not the best approach. I'm hoping someone can tell me what is wrong with the code and perhaps I can learn something here and be able to tackle the second part.

$AzureADDirectoryRoles = Get-AzureADDirectoryRole
ForEach ($role in $AzureAdDirectoryRoles)
{
    $Members = Get-AzureAdDirectoryRoleMember -ObjectID $($role.ObjectID)
    ForEach ($member in $Members)
    {
        $obj = New-Object PSObject
        Add-Member -InputObject $obj -MemberType NoteProperty -Name RoleName 
       -Value $($role.DisplayName)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name ObjectId 
       -Value $($Member.ObjectID)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name 
        ObjectType -Value $($Member.ObjectType)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name 
        DisplayName -Value $($Member.DisplayName)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name 
        UserPrincipalName -Value $($Member.UserPrincipalName)
        $roleMembership += $obj
    }
}

I get multiple errors but it is all the same thing. here is the error I get below.

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'. At line:13 char:9

3
  • please, would you fix your code formatting? there is a link in the "new question" page that tells you how to do it. Commented Aug 28, 2019 at 17:58
  • Yea I am already doing that, I was going to reply to ignore this question and I will re-write it. Sorry about that Commented Aug 28, 2019 at 18:00
  • kool! thanks for fixing it ... [grin] Commented Aug 28, 2019 at 20:53

1 Answer 1

2

Try to add single line to the beginning, where you will define that roleMembership is an empty array.

$roleMembership = @()
$AzureADDirectoryRoles = Get-AzureADDirectoryRole
ForEach ($role in $AzureAdDirectoryRoles) {
    $Members = Get-AzureAdDirectoryRoleMember -ObjectID $($role.ObjectID)
    ForEach ($member in $Members) {
        $obj = New-Object PSObject
        Add-Member -InputObject $obj -MemberType NoteProperty -Name RoleName -Value $($role.DisplayName)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name ObjectId -Value $($Member.ObjectID)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name ObjectType -Value $($Member.ObjectType)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name DisplayName -Value $($Member.DisplayName)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name UserPrincipalName -Value $($Member.UserPrincipalName)
        $roleMembership += $obj
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You can also use [array]$roleMembership += $obj, especially if you don't want to initialize the array of objects for some reason.
That worked , is there a way to export the results into an excel file?
@KevinDennis You can use export-csv.

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.