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