Azure Automation Powershell job for MFA Enabled Account

Comp_tech 20 Reputation points

Hi, Can someone help me to create a powershell task with Azure Automate to exports users with the license details etc. each month? My main concern is how can we automate it for MFA enabled account? The script I am using is as below but at the moment I am running it manually and entering the credentials and MFA code manually as well. Can this be automated with Azure Automation and email it as well? This is something I have never done before so prefer an idiot guide please :) Thanks in advance. Powershell Script

$Result = @() #Result array
#Get all subscribed license Skus to Microsoft services
$AllSkus= Get-AzureADSubscribedSku
#Get all Azure AD Users with the required properties
$AllUsers = Get-AzureADUser -All $true | Select DisplayName, UserPrincipalName, AssignedLicenses, AssignedPlans, ObjectId
#Iterate users one by one and resolve the license details
ForEach ($User in $AllUsers)
$AssignedLicenses = @();
$LicensedServices = @();
if($User.AssignedLicenses.Count -ne 0)
#Resolve license SKU details
ForEach($License in $User.AssignedLicenses)
$SkuInfo = $AllSkus | Where { $_.SkuId -eq $License.SkuId}
$AssignedLicenses += $SkuInfo.SkuPartNumber;
#Resolve assigned service plans
ForEach($ServicePlan in $User.AssignedPlans)
$LicensedServices += $ServicePlan.Service;
#Add user detail to $Result array one by one
$Result += New-Object PSObject -property $([ordered]@{
UserName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
UserId= $User.ObjectId
IsLicensed  = if ($User.AssignedLicenses.Count -ne 0) { $true } else { $false }
Licenses = $AssignedLicenses -join ","
LicensedServices = ($LicensedServices | Sort-Object | Get-Unique)  -join ","
#Export All Microsoft 365 Users report to CSV
$Result | Export-CSV "C:\Temp\Microsoft365Users.CSV" -NoTypeInformation -Encoding UTF8
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,138 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,772 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dillon Silzer 54,746 Reputation points


    Here is a guide for using Azure Automation with Managed Identities (this is how you can automate using PowerShell without having to manually enter credentials):

    Tutorial: Create Automation PowerShell runbook using managed identity

    If this is helpful please accept answer.

    0 comments No comments