Kevin Dennis
Kevin Dennis

Reputation: 1

Powershell script error: Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'

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

Upvotes: 0

Views: 1244

Answers (1)

Stanislav Castek
Stanislav Castek

Reputation: 158

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
    }
}

Upvotes: 2

Related Questions