Issues running Export-M365DSCConfiguration using Application ID + Application Secret
I'm trying to run Export-M365DSCConfiguration using the ApplicationId and ApplicationSecret parameters for authentication to export basic a Azure AD tenant configuration. I have created an app registration with basic Microsoft Graph Directory.Read.All application permissions.
When I run the code in the script below, I immediately receive the error Add-M365DSCTelemetryEvent : Cannot index into a null array., then the Verbose output shows many modules and functions being imported. and finally the output ends with:
Cannot index into a null array.
Partial Export file was saved at: "C:\Users\<username>\AppData\Local\Temp\<GUID>.partial.ps1.ps1.partial
I tried intentionally providing the wrong ApplicationId and ApplicationSecret values, and I receive the same results, so it seems Microsoft365DSC isn't even authenticating to Azure AD.
One additional note is that I am working in a secure environment and cannot install PowerShell modules using Install-Module. Because of this, I have installed Microsoft365DSC and the required modules from PowerShellGallery.com by downloading the module as a .nupkg file, renaming it to .zip, and extracting it to a folder on the C: drive. Before running the script below, I import the modules into the session manually using Import-Module. If I am just trying to export Azure AD tenant configs, do I still need all required modules, including the unrelated modules like Microsoft.Graph.Teams, MicrosoftTeams, etc.? Additionally, do I need the exact version specified in Microsoft365DSC.psd1, or can I use new versions of the required modules?
Script:
$TenantId = 'contoso.onmicrosoft.com'
$ApplicationId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ApplicationSecret = Read-Host -Prompt
$Params = @{
TenantId = $TenantId
ApplicationId = $ApplicationId
ApplicationSecret = $ApplicationSecret
ComponentsToExtract = @("AADTenantDetails")
FileName = "C:\Users\<username>\Desktop\Configuration.ps1"
Quiet = $true
Verbose = $true
}
Export-M365DSCConfiguration
Following up on this, I found the Microsoft365DSC module was not getting imported to my current session correctly. I commented out the non-essential modules in Microsoft365DSC.psd1 (Microsoft.Graph.Teams, MicrosoftTeams, etc.), ensured I had the correct versions downloaded, and only installed the following modules:
- AzureADPreview
- DSCParser
- Microsoft.Graph.Authentication
- Microsoft365DSC
- MSCloudLoginAssistant
- ReverseDSC
Once I successfully imported the required modules, I ran the script from my post above and Microsoft365DSC reached Connecting to {Azure AD}... and then popped open a web form for me to provide login credentials. I'm assuming this means exporting this configuration is only supported using a user account (GlobalAdminAccount parameter)?
This leads me to ask is there a matrix or documentation detailing what config settings can be pulled by an application vs a user account?
Hi @dbird03, to best of my knowledge, the authentication request is not for the DSC module, but it's for granting consent to the permissions required by the module. Once consent has been given, it will work without any interaction. Could you verify that for me?
Hey @RuudGijsbers, my understanding is that M365DSC is running Connect-AzureAD in the background during this step and using user credentials to export some resources rather than an application service principal.
Since my last reply, I learned the Wiki has a page for each supported resource with an Azure AD Permissions section that details the Graph API permissions and/or Azure AD role required to automate or export the resource (Example: AADConditionalAccessPolicy).
Using this information, I was able to manually create an App registration, grant the necessary Graph API application permissions, upload a self-signed certificate, and use the ApplicationId and CertificateThumbprint parameters with Export-M365DSCConfiguration to export resources that supported this authentication method.
I'll close this issue since I was able to get this working. Hopefully my previous comment about the required modules and this comment help others who run into the same issue.
@dbird03 - Can you share what permissions the application needs?
@hmank Check out my previous comment on this issue. To export the Azure AD Conditional Access policies, the app registration would need the following application permissions for Graph API per the documentation.
- Application.Read.All
- Group.Read.All
- Directory.Read.All
- Policy.Read.All
- Policy.Read.ConditionalAccess
- RoleManagement.Read.All
- RoleManagement.Read.Directory
- User.Read.All
Documentation: https://github.com/microsoft/Microsoft365DSC/wiki/AADConditionalAccessPolicy#azure-ad-permissions
Additionally, I found that some resources required a specific authentication method when using an app registration for authentication. For example, even if you have the right permissions set, some resource configurations would only export if you were using certificate authentication, while some would export if you were using either certificate or secret authentication.
I hope this helps!