Azure Automation Powershell job for MFA Enabled Account

Comp_tech 20 Reputation points
2023-04-04T11:33:40.6066667+00:00

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

Connect-AzureAD
$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,366 questions
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2023-04-05T03:10:35.76+00:00

    Hello,

    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

    https://learn.microsoft.com/en-us/azure/automation/learn/powershell-runbook-managed-identity


    If this is helpful please accept answer.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.