Manage Microsoft 365 users and groups with Azure Functions (original) (raw)

Microsoft 365 provides efficient management capabilities for most administrative needs, but specific complex scenarios require more advanced automation and customization approaches. Azure Functions is one option that extends the capabilities of Microsoft 365 administration beyond conventional boundaries.

Managing user attributes, such as job titles, departments or contact information, in large organizations can be difficult. Azure Functions is a Microsoft serverless computing service that Microsoft 365 administrators can use for a variety of management jobs related to users and groups on the cloud collaboration platform. With its versatile trigger functionality, Azure Functions provides flexibility that native Microsoft 365 tools can't match. You can use them to dynamically provision user accounts, manage group access with nuanced control or execute bulk attribute updates. This article explains how to deploy advanced automation routines for enhanced efficiency and customization in managing users and groups.

Automating Microsoft 365 user and group tasks with Azure Functions

Automating the process of adding and removing users in Microsoft 365 can significantly reduce the workload of administrators. By configuring Azure Functions to listen for events, such as updates from the HR system or specific email requests, you can automate the creation or deletion of user accounts. It ensures users have access when needed and maintains security on these accounts throughout the lifecycle.

Azure Functions is capable of dynamically managing group memberships and access controls. It can adjust group memberships based on factors such as user roles, project or location to align access levels with organizational policies and changes.

Integration with third-party products and services

Another benefit of Azure Functions is its ability to integrate Microsoft 365 management with third-party products and services. Azure Functions interacts with external APIs and services to create comprehensive management workflows that include external identity providers, security tools or productivity applications.

Deploying Azure Functions for Microsoft 365 management

The Azure Functions versatile serverless computing framework provides Microsoft 365 administrators an advanced toolkit for automating and customizing user and group management tasks. This section covers practical applications of Azure Functions to streamline the management of Microsoft 365 environments, focusing on dynamic provisioning, customized group management, bulk user attribute updates and advanced auditing.

Dynamic provisioning and deprovisioning

Dynamic provisioning and deprovisioning entail automatically creating or removing user accounts based on organizational needs, reducing manual work and enabling the operations team to be more efficient. This automation aligns user access rights with their status, such as employment changes, ensuring security and compliance.

To configure the trigger, set up either an HTTP trigger or a scheduled trigger in Azure Functions. The trigger listens for events that indicate a change in user status, such as updates in the HR system. Next, develop a PowerShell script that connects to the Microsoft Graph API to create or delete user accounts in Microsoft 365, depending on the trigger. The script should be versatile enough to handle several scenarios, such as new hires or terminations. After developing the script, test it thoroughly in a nonproduction environment to ensure it works as intended. Once the script is verified, deploy it to manage user accounts dynamically. Microsoft constantly updates its cloud-based products, so it's essential to run regular checks on code to stay current with Microsoft 365 APIs or organizational policies.

Customized group management and access control

Customizing group management and access control gives precise control over resource access, ensuring users have the necessary permissions based on their roles and responsibilities. This level of customization prevents unauthorized access to bolster security.

You can utilize Azure Functions with triggers based on user activity or admin requests to automate user access by modifying group memberships and access permissions. Moreover, you can use scripts to automatically update group memberships and access levels in Microsoft 365 based on project assignments, department changes or any other criteria.

Bulk user attribute management

Manually managing attributes for many users can be a daunting and time-consuming task. Without automation, it's easy to introduce errors. However, with bulk user attribute management through Azure Functions, automation can ensure consistency and accuracy across the user base. The general procedure for this task includes the following steps:

  1. Bulk update mechanism. Create an Azure Function triggered by an external event, such as a database update or a predetermined schedule.
  2. Script execution. The function executes a script to modify user attributes in bulk via the Microsoft Graph API. It could involve updating job titles, department names or contact information.
  3. Validation and rollout. Implement validation checks within the script to prevent incorrect data updates. After thorough testing, deploy the function for ongoing, automated user attribute management.

Advanced auditing and reporting

It's imperative to have in-depth auditing and customized reporting for security, compliance and operational oversight. With Azure Functions, you can automate report generation and alerts, providing real-time insights into user and group activities. These are the components for this type of automated task:

Examples of dynamic provisioning and deprovisioning

In this example, to start, you need to have created the function app and added an HTTP trigger. To learn how to do this, refer to the following documentation.

The next requirement is app registration for Microsoft Graph with the following permissions:

The following code gives permission to send a JSON request to the Azure function app:

{

"displayName": "John Doe",

"mailNickname": "johndoe",

"userPrincipalName": "[email protected]",

"passwordProfile": {

"password": "Pass@word!"

},

"groupName": " Mark 8 Project Team"

}

The code for the HTTP trigger is:

using namespace System.Net

param($Request, $TriggerMetadata)
 

# For Testing Only

$clientId = "3dc8e6d6-e7f5-4e75"

$tenantId = "f5dba133-4a2b-40d4"

$clientSecret = "6328Q~em6eORoAMkQs1jFaDq~"
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>u</mi><mi>r</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>d</mi><mo>=</mo><mi>C</mi><mi>o</mi><mi>n</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>T</mi><mi>o</mi><mo>−</mo><mi>S</mi><mi>e</mi><mi>c</mi><mi>u</mi><mi>r</mi><mi>e</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">securePassword = ConvertTo-SecureString </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">sec</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ss</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.13889em;">tT</span><span class="mord mathnormal">o</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mord mathnormal">St</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>clientSecret `

-AsPlainText `

-Force
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>N</mi><mi>e</mi><mi>w</mi><mo>−</mo><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>S</mi><mi>y</mi><mi>s</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi mathvariant="normal">.</mi><mi>M</mi><mi>a</mi><mi>n</mi><mi>a</mi><mi>g</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>A</mi><mi>u</mi><mi>t</mi><mi>o</mi><mi>m</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi mathvariant="normal">.</mi><mi>P</mi><mi>S</mi><mi>C</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>l</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">credential = New-Object System.Management.Automation.PSCredential (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">cre</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mord mathnormal">ec</span><span class="mord mathnormal" style="margin-right:0.05764em;">tS</span><span class="mord mathnormal">ys</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord mathnormal">ana</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">A</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mord mathnormal">o</span><span class="mord mathnormal">ma</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.07153em;">PSC</span><span class="mord mathnormal">re</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mopen">(</span></span></span></span>clientId, $securePassword)
 

Connect-MgGraph `

-TenantId $tenantId `

-ClientSecretCredential $credential `

-NoWelcome
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">body = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>Request.Body | ConvertFrom-Json | ConvertFrom-Json
 

# Retrieve the Project Group

$groupName = "Mark 8 Project Team"
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi><mi>r</mi><mi>o</mi><mi>u</mi><mi>p</mi><mo>=</mo><mi>G</mi><mi>e</mi><mi>t</mi><mo>−</mo><mi>M</mi><mi>g</mi><mi>G</mi><mi>r</mi><mi>o</mi><mi>u</mi><mi>p</mi><mo>−</mo><mi>F</mi><mi>i</mi><mi>l</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi mathvariant="normal">&quot;</mi><mi>d</mi><mi>i</mi><mi>s</mi><mi>p</mi><mi>l</mi><mi>a</mi><mi>y</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>e</mi><msup><mi>q</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">group = Get-MgGroup -Filter &quot;displayName eq &#x27;</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">G</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">G</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.9463em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">i</span><span class="mord mathnormal">lt</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord">&quot;</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mord mathnormal">am</span><span class="mord mathnormal">ee</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span></span></span></span>groupName'"
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi><mi>r</mi><mi>o</mi><mi>u</mi><mi>p</mi><mi>I</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">groupId = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>group.Id
 

try {

$newUser = New-MgUser -AccountEnabled `

-DisplayName $body.displayName `

-MailNickname $body.mailNickname `

-UserPrincipalName $body.userPrincipalName `

-PasswordProfile @{

ForceChangePasswordNextSignIn = $true;

Password = $body.passwordProfile.password

}
 

New-MgGroupMember -GroupId <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi><mi>r</mi><mi>o</mi><mi>u</mi><mi>p</mi><mi>I</mi><mi>d</mi><mo>−</mo><mi>D</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>y</mi><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>I</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">groupId -DirectoryObjectId </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.03588em;">ory</span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">d</span></span></span></span>newUser.Id

$result = @{

Id = $newUser.Id

Displayname = $newUser.Displayname

UserName = $newUser.UserPrincipalName

GroupName = $group.DisplayName

}

} catch {

$result = @{

Error = $_.Exception.Message

}

}
 

Disconnect-MgGraph
 

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{

StatusCode = [HttpStatusCode]::OK

Body = $result

Headers = @{

"Content-Type" = "application/json"

}

})

When executed for testing within Azure Portal using Test/Run, it returns the ID, DisplayName and UserPrincipalName of the newly created Entra ID user account. It also adds the created user to the specified group. The function can also be called from PowerShell with the following approach:

$uri = 'https://func.azurewebsites.net/api/UserProvision?code=KQd1bEB6BlIu2zK-dDLYPNpzh72IGolua8lAzFuEl2A=='
 

$userProperties = @{

displayName = "John Doe"

mailNickname = "johndoe"

userPrincipalName = "[email protected]"

passwordProfile = @{

password = "Pass@word!!"

}

groupName = "Mark 8 Project Team"

}
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mi>s</mi><mi>o</mi><mi>n</mi><mi>B</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">jsonBody = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">so</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>userProperties | ConvertTo-Json | ConvertTo-Json
 

$headers = @{

"Content-Type" = "application/json"

}
 

$response = Invoke-RestMethod `

-Uri $uri `

-Method Post `

-Body $jsonBody `

-Headers $headers
 

Write-Output $response

How to execute bulk user attribute management

To perform bulk updates, use the function app already created in the earlier example to generate a few users. The first is the user accounts:

$userAccounts = @(

@{

displayName = "John Doe"

mailNickname = "johnd"

userPrincipalName = "[email protected]"

passwordProfile = @{

password = "Pass@word1!!"

}

groupName = "Mark 8 Project Team"

},

@{

        displayName = "Jane Doe"

        mailNickname = "janed"

        userPrincipalName = "[email protected]"

        passwordProfile = @{

            password = "Pass@word2!!"

        }

        groupName = "Mark 8 Project Team"

    },

    @{

        displayName = "Alex Smith"

        mailNickname = "alexs"

        userPrincipalName = "[email protected]"

        passwordProfile = @{

            password = "Pass@word3!!"

        }

        groupName = "Mark 8 Project Team"

    },

    @{

        displayName = "Maria Garcia"

        mailNickname = "mariag"

        userPrincipalName = "[email protected]"

        passwordProfile = @{

            password = "Pass@word4!!"

        }

        groupName = "Mark 8 Project Team"

    },

    @{

        displayName = "James Johnson"

        mailNickname = "jamesj"

        userPrincipalName = "[email protected]"

        passwordProfile = @{

            password = "Pass@word5!!"

        }

        groupName = "Mark 8 Project Team"

    }

)

The following PowerShell code calls the HTTP trigger:

$uri = 'https://func.azurewebsites.net/api/UserProvision?code=KQd1bEB6BlIu2zK-dDLYPNpzh72IGolua8lAzFuEl2A=='
 

$headers = @{

"Content-Type" = "application/json"

}
 

foreach ($user in $userAccounts) {

$userForFunction = @{

displayName = $user.displayName

mailNickname = $user.mailNickname

userPrincipalName = $user.userPrincipalName

passwordProfile = $user.passwordProfile

}
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mi>s</mi><mi>o</mi><mi>n</mi><mi>B</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">jsonBody = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">so</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>userForFunction | ConvertTo-Json
 

try {

$response = Invoke-RestMethod `

-Uri $uri `

-Method Post `

-Headers $headers `

-Body $jsonBody

                Write-Host "Successfully created the user: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>user.displayName)"

     } catch {

           Write-Error "Failed to create the user: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>user.displayName). Error: $_"

     }

}

With the accounts added, create a new HTTP trigger, and use the following code to perform a bulk update of user properties:

using namespace System.Net

param($Request, $TriggerMetadata)
 

# For Testing Only

$clientId = "3dc8e6d6-e7f5-4e75"

$tenantId = "f5dba133-4a2b-40d4"

$clientSecret = "6328Q~em6eORoAMkQs1jFaDq~"
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>u</mi><mi>r</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>d</mi><mo>=</mo><mi>C</mi><mi>o</mi><mi>n</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>T</mi><mi>o</mi><mo>−</mo><mi>S</mi><mi>e</mi><mi>c</mi><mi>u</mi><mi>r</mi><mi>e</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">securePassword = ConvertTo-SecureString </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">sec</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ss</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.13889em;">tT</span><span class="mord mathnormal">o</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mord mathnormal">St</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>clientSecret -AsPlainText -Force
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>N</mi><mi>e</mi><mi>w</mi><mo>−</mo><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>S</mi><mi>y</mi><mi>s</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi mathvariant="normal">.</mi><mi>M</mi><mi>a</mi><mi>n</mi><mi>a</mi><mi>g</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>A</mi><mi>u</mi><mi>t</mi><mi>o</mi><mi>m</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi mathvariant="normal">.</mi><mi>P</mi><mi>S</mi><mi>C</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>l</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">credential = New-Object System.Management.Automation.PSCredential (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">cre</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mord mathnormal">ec</span><span class="mord mathnormal" style="margin-right:0.05764em;">tS</span><span class="mord mathnormal">ys</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord mathnormal">ana</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">A</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mord mathnormal">o</span><span class="mord mathnormal">ma</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.07153em;">PSC</span><span class="mord mathnormal">re</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mopen">(</span></span></span></span>clientId, $securePassword)
 

Connect-MgGraph `

-TenantId $tenantId `

-ClientSecretCredential $credential `

-NoWelcome
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>T</mi><mi>o</mi><mi>U</mi><mi>p</mi><mi>d</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">usersToUpdate = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal">sers</span><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.10903em;">U</span><span class="mord mathnormal">p</span><span class="mord mathnormal">d</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>Request.Body | ConvertFrom-Json | ConvertFrom-Json
 

foreach ($user in $usersToUpdate) {

    $userId = Get-MgUser `

-Filter "userprincipalname eq '$($user.userPrincipalName)'" | `

Select-Object Id

           Write-Output $userId
 

try {

                Update-MgUser -UserId $userId.Id `

                           -GivenName $user.firstName `

                           -Surname $user.lastName `

                           -JobTitle $user.jobTitle `

                           -Department $user.department `

                           -OfficeLocation $user.office
 

           Write-Output "Successfully updated user: $userId"

    } catch {

           Write-Error "Failed to update user: $userId"

    }

}
 

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{

    StatusCode = [System.Net.HttpStatusCode]::OK

    Body = $userId

})

This code supports working with either a single account or a nested structure. For example, the following code updates the user attributes of all the accounts created in the earlier example:

$usersToUpdate = @"

[

    {

        "firstName": "John",

        "lastName": "Doe",

        "jobTitle": "Project Manager",

        "department": "Consulting",

        "office": "Chicago",

        "userPrincipalName": "[email protected]"

    },

    {

        "firstName": "Jane",

        "lastName": "Doe",

        "jobTitle": "Managing Director",

        "department": "Consulting",

        "office": "New York",

        "userPrincipalName": "[email protected]"

    }

]

"@
 

$uri = "https://func.azurewebsites.net/api/BulkAttributes?code=KQd1bEB6BlIu2zK-dDLYPNpzh72IGolua8lAzFuEl2A=="
 

$headers = @{

"Content-Type" = "application/json"

}
 
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mi>s</mi><mi>o</mi><mi>n</mi><mi>B</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">jsonBody = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord mathnormal">so</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>usersToUpdate | ConvertTo-Json
 

try {

$response = Invoke-RestMethod `

-Uri $uri `

-Method Post `

-Headers $headers `

-Body $jsonBody

Write-Host "Successfully updated user accounts."

     } catch {

Write-Error "Failed to update user accounts. Error: $_"

     }

}

The code updates the attributes of the users specified in the JSON object with the corresponding values within Entra ID.

Liam Cleary is founder and owner of SharePlicity, a technology consulting company that helps organizations with internal and external collaboration, document and records management, business process automation, automation tool deployment, and security controls and protection. Cleary's areas of expertise include security on the Microsoft 365 and Azure platforms, PowerShell automation and IT administration. Cleary is a Microsoft MVP and a Microsoft Certified Trainer.