Overview

This PowerShell script is designed to automate the process of ensuring that specific groups (such as AlsoAdminAgents, AlsoHelpdeskAgents, EdenAkersAdminAgents, and EdenAkersHelpdeskAgents) have the “Contributor” role assigned across all Azure subscriptions accessible by the logged-in user so that the Azure Consumption for that Subscription is seen by Microsoft as Managed. If a particular group does not have the role assigned within a subscription, the script attempts to assign it. In case the standard role assignment fails, the script retries the assignment using the -ObjectType ForeignGroup parameter.


Detailed Explanation

1. Comment-Based Help Header

  • Purpose:
    The header section uses PowerShell’s comment-based help format and provides essential metadata such as the script’s name, description, usage examples, author information, date, and version. This helps other users (or even your future self) understand the intent and requirements of the script.

2. Defining Variables

  • Group Definitions:
    The script defines an array $groups that contains custom objects. Each object has two properties:

    • Description: A human-friendly name (e.g., "AlsoAdminAgents")

    • ObjectID: The corresponding GUID for the group.

  • This design allows the script to output descriptive messages that make it clear which group is being processed.

  • Role Definition:
    The $role variable is set to "Contributor". This variable represents the role that the script will check for and, if necessary, assign to each group.

3. Module Check and Authentication

  • Module Check:
    The script checks if the Az module (which is required for managing Azure resources via PowerShell) is installed. If it is not installed, the script outputs a message indicating that it’s missing and, optionally, can install it (the actual installation command is commented out).

    • Successful detection of the Az module is printed in green to indicate a positive outcome.

  • Authentication:
    The script calls Connect-AzAccount to authenticate the user to Azure. It uses parameters like -WarningAction SilentlyContinue to suppress any unwanted warning messages during authentication.

4. Retrieving Subscriptions

  • Subscription Retrieval:
    The script uses Get-AzSubscription with warning suppression (using both -WarningAction SilentlyContinue and redirecting warnings with 3>$null) to fetch all Azure subscriptions to which the logged-in user has access.

    • This ensures that warnings do not clutter the output, making the script’s log easier to read.

5. Looping Through Subscriptions and Groups

  • Subscription Loop:
    For each subscription retrieved, the script sets the context using Set-AzContext so that subsequent Azure commands affect the correct subscription. The subscription's ID and Name are printed for clarity.

  • Group Loop:
    The script then iterates through each group defined in the $groups array:

    • Check Role Assignment:
      The script uses Get-AzRoleAssignment to determine if the group already has the Contributor role assigned in that subscription. This helps avoid redundant role assignments.

    • Conditional Output:
      If the role is already assigned, it prints a message (in green) confirming the assignment.

    • Role Assignment Attempt:
      If the role is not assigned:

      • The script attempts to assign the role using New-AzRoleAssignment.

      • Error Handling:
        If this initial attempt fails, the script catches the error and logs it. Then, it makes a second attempt using the -ObjectType ForeignGroup parameter—which is useful in certain scenarios where the object type might need to be explicitly specified.

      • Success or Further Failure:
        Any successful role assignment results in a green success message. If both attempts fail, the error message is output for troubleshooting.

6. Output and Logging

  • Visual Cues:
    Throughout the script, the use of Write-Host with descriptive messages—especially those marked with -ForegroundColor Green for successes—helps users visually track which steps have succeeded and where issues might be occurring.

  • Descriptive Logging:
    Both the subscription information (ID and Name) and the group descriptions are logged, providing a clear audit trail of what the script is doing at each stage.

Summary

This script automates the management of Azure role assignments for multiple groups across all subscriptions accessible to the authenticated user. It includes the following processes:

  1. Define Variables and Group Data: Prepares the list of groups and the role to be assigned.

  2. Module Check and Azure Authentication: Ensures that the necessary Az module is present and logs into Azure.

  3. Retrieve Subscriptions and Set Context: Fetches all subscriptions and sets the execution context for each one.

  4. Iterate Through Groups: For each subscription and each group, the script checks for the existing Contributor role and assigns it if necessary, with a fallback mechanism for handling errors.

  5. Provide Detailed Output: Uses descriptive messages and color-coded outputs to improve clarity during execution.

This detailed explanation should help you and others understand both what the script does and how it accomplishes its tasks.