1

Actually, I trying to run below PowerShell script which will tell me about the Role Assignments in my Azure Subscription:-

$tenantId = "xxxxxx"
$subscriptionId = "xxxxxxx"

# Log in to Azure withtenant and subscription IDs
Connect-AzAccount -Tenant $tenantId -Subscription $subscriptionId -UseDeviceAuthentication

#Role Assignment Function
Function Function-RoleAssignments(){

    Write-Host "'n> Checking IAM Role Assignments…'n" -ForegroundColor Green
    $Global:RoleAssignments = [System.Collections.ArrayList]::new()
    $RoleAssignment = Get-AzRoleAssignment -Scope ("/subscriptions/"+Sglobal:SourceSubscription )

foreach($role in $RoleAssignment){
$csvObject = New-Object PSObject
Add-Member -inputObject $csv0bject -memberType NoteProperty -name "Display Name" -value$role.DisplayName
Add-Member -inputObject $csvObject -memberType NoteProperty -name "SignIn Name" -value $role.SignInliame
Add-Member -inputObject $csvObject -memberType NoteProperty -name "Object Type" -value $role.ObjectType
Add-Member -inputObject $csvObject -memberType NoteProperty -name "Object ID" -value $role. ObjectId
Add-Member -inputObject $csv0bject -memberType NoteProperty -name "Role" -value $role. RoleDefinitionName
Add-Member -inputObject $csv0bject -memberType NoteProperty -name "RoleDefinition ID" -value $role. RoleDefinitionId
Add-Member -inputObject $csvObject -memberType NoteProperty -name "Scope" -value $role.Scope
Add-Member -inputObject $csv0bject -memberType NoteProperty -name "Description" -value $role. Description
Add-Member -inputObject $csv0bject -memberType NoteProperty -name "CanDelegate" -value $role. CanDelegate

$Global:RoleAssignments.Add($csvObject) | Out-Null

}

}

 # Call the function to populate SGlobal:RoleAssignment
 Function-RoleAssignments

 # Export the collected quota information to a CSV file SGlobal:RoleAssignments | Export-Csv -Path "RoleAssignment.csv" -NoTypeInformation

 Write-Host "Resource report generated and saved to RoleAssignment.csv"

Powershell Script Image

After running the script I faced the below error:-

Error Image

Kindly help me how to solve this issue. Thank you in advance:)

Imran
  • 3,875
  • 2
  • 3
  • 12

1 Answers1

0

I tried the same in my environment I got the same error like below:

enter image description here

To know about the Role Assignments in Azure Subscription, I ran below modified PowerShell script:

$tenantId = "xxxxxxx"
$subscriptionId = "xxxxxxxx"

# Log in to Azure with tenant and subscription IDs
Connect-AzAccount -Tenant $tenantId -Subscription $subscriptionId -UseDeviceAuthentication

#Role Assignment Function
Function Function-RoleAssignments(){

    Write-Host "`n> Checking IAM Role Assignments..." -ForegroundColor Green
    $Global:RoleAssignments = [System.Collections.ArrayList]::new()
    $RoleAssignment = Get-AzRoleAssignment -Scope "/subscriptions/$subscriptionId"

    foreach($role in $RoleAssignment){
        $csvObject = New-Object PSObject
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Display Name" -value $role.DisplayName
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "SignIn Name" -value $role.SignInName
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Object Type" -value $role.ObjectType
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Object ID" -value $role.ObjectId
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Role" -value $role.RoleDefinitionName
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "RoleDefinition ID" -value $role.RoleDefinitionId
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Scope" -value $role.Scope
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "Description" -value $role.Description
        Add-Member -inputObject $csvObject -memberType NoteProperty -name "CanDelegate" -value $role.CanDelegate

        $Global:RoleAssignments.Add($csvObject) | Out-Null
    }
}

# Call the function to populate $Global:RoleAssignment
Function-RoleAssignments

# Export the collected quota information to a CSV file
$Global:RoleAssignments | Export-Csv -Path "RoleAssignment.csv" -NoTypeInformation

Write-Host "Resource report generated and saved to RoleAssignment.csv"

Output:

enter image description here

In csv file, the IAM role assignments retrieve and display for the specified subscription like below:

enter image description here

Imran
  • 3,875
  • 2
  • 3
  • 12