Skip to content

M365DSC Azure Automation Account Runbook - The term 'Connect-M365Tenant' is not recognized #5350

Closed
@dergint

Description

@dergint

Description of the issue

Hi,

I have an Azure Automation Account runbook and it runs the following command. I have not included all the variables but all certs etc all working fine.
The runtime environment is Powershell 5.1 and installed the powershell module M365DSC 1.24.1016.1 on the runbook environment.

It fails on the following line
Export-M365DSCConfiguration -Components @("SCDLPComplianceRule") -ApplicationId $AppId -CertificateThumbprint $Cert.Thumbprint -TenantId $TenantId -path $path -filename "purviewdlpconfig_$Date.ps1"

with below error. Not sure why it is not recognising this command assuming it would be part of the M365DSC module.
Write-Error: Failed to export M365DSC configuration: The term 'Connect-M365Tenant' is not recognized as a name of a cmdlet

Any help would be appreciated.

Microsoft 365 DSC Version

1.24.1016.1

Which workloads are affected

Security & Compliance Center

The DSC configuration

#AutomationAccountConnection
try {
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $.Exception
throw $
.Exception
}

Define storage account and container details

$storageAccountName = "azuresssssss"
$containerName = "automasssss"

Get the storage account context using the managed identity

$storageAccount = Get-AzStorageAccount -ResourceGroupName "Azurexxxxxx" -Name $storageAccountName
$storageContext = $storageAccount.Context

#Variables for Certificate
$cert = Get-AutomationCertificate -Name 'Azusdfsafasdfasdf'
$CertName = "Azurssssssss"

Retrieve the certificate from Azure Automation

try {
Write-Output "Getting Certificate Thumbprint"
$Cert = Get-AutomationCertificate -Name $CertName
Write-Output "Certificate Thumbprint: $($Cert.Thumbprint)"
Write-Output "Has Private Key: $($Cert.HasPrivateKey)"
}
catch {
Write-Error "Failed to connect to Certificate Thumbprint: $_"
exit
}

Variables for Files and Folders

$path = "$env:TEMP"
$Date = $(Get-Date -f yyyy-MMM-dd-HHMMtt)

Variables for App Registration

$AppId = "18a34eeww-d5sdafasdfasdfadsf"
$OrgName = "xxxxxx.onmicrosoft.com"
$TenantID = "xxxxxx.onmicrosoft.com"

#Start Exporting M365DSC Configuration and creating file
try {
"Exporting M365DSC Configuration"
Export-M365DSCConfiguration -Components @("SCDLPComplianceRule") -ApplicationId $AppId -CertificateThumbprint $Cert.Thumbprint -TenantId $TenantId -path $path -filename "purviewdlpconfig_$Date.ps1"
}
catch {
Write-Error "Failed to export M365DSC configuration: $_"
exit
}

$fileName = "$path\purviewdlpconfigreport_$Date.json"

#Start creating M365DSC report file from configuration file stored in Temp
try {
"Creating M365DSC report from the configuration file and store to temp file as json"
New-M365DSCReportFromConfiguration -Type JSON -ConfigurationPath "$path\purviewdlpconfig_$Date.ps1" -OutputPath "$fileName"
}
catch {
Write-Error "Failed to generate DSC report: $_"
exit
}

$tempFilePath = $fileName

Check if file exists and upload JSON file to Blob Storage

if (Test-Path -Path $tempFilePath) {
try {
Set-AzStorageBlobContent -File $tempFilePath -Container $containerName -Blob $fileName -Context $storageContext
Write-Output "JSON output uploaded to Blob Storage as $fileName"
}
catch {
Write-Error "Failed to upload JSON file to Blob Storage: $_"
throw
}
}
else {
Write-Error "The file $tempFilePath does not exist. Check if the Export-M365DSCConfiguration command completed successfully."
}

Verbose logs showing the problem

Verbose
Exception calling "SourceExists" with "1" argument(s): "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security, State."
06/11/2024, 7:09:01 pm
Verbose
Could not write to event log Source {M365DSCReverse::Test-M365DSCModuleValidity} EntryType {Information} Message {Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\ContainerUser\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?"} { Exception calling "SourceExists" with "1" argument(s): "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security, State." } \ at Add-M365DSCEvent, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCLogEngine.psm1: line 193 \ at Start-M365DSCConfigurationExtract, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCReverse.psm1: line 107 \ at Export-M365DSCConfiguration, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCUtil.psm1: line 1460 \ at , : line 47
06/11/2024, 7:09:01 pm
Verbose
Exception calling "SourceExists" with "1" argument(s): "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security, State."
06/11/2024, 7:09:01 pm
Verbose
Could not write to event log Source {[M365DSCLogEngine]} EntryType {Error} Message {Could not write to event log Source {M365DSCReverse::Test-M365DSCModuleValidity} EntryType {Information} Message {Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\ContainerUser\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?"} { Exception calling "SourceExists" with "1" argument(s): "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security, State." } \ at Add-M365DSCEvent, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCLogEngine.psm1: line 193 \ at Start-M365DSCConfigurationExtract, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCReverse.psm1: line 107 \ at Export-M365DSCConfiguration, C:\usr\src\PSModules\Microsoft365DSC\Modules\M365DSCUtil.psm1: line 1460 \ at , : line 47}
06/11/2024, 7:09:07 pm
Verbose
Access denied
06/11/2024, 7:09:07 pm
Verbose
Error Log created at {file://C:/app/1348-M365DSC-ErrorLog.log}
06/11/2024, 7:09:07 pm
Verbose
Loading module from path 'C:\usr\src\PSModules\Microsoft365DSC\DSCResources\MSFT_SCDLPComplianceRule\MSFT_SCDLPComplianceRule.psm1'.
06/11/2024, 7:09:07 pm
Verbose
Exporting function 'Get-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Exporting function 'Set-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Exporting function 'Test-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Exporting function 'Export-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Importing function 'Export-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Importing function 'Get-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Importing function 'Set-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Importing function 'Test-TargetResource'.
06/11/2024, 7:09:07 pm
Verbose
Attempting connection to {MicrosoftGraph} with:
06/11/2024, 7:09:07 pm
Verbose
Name Value ---- ----- CertificatePath CertificateThumbprint 9xxxxxxxxxxxx8 ApplicationSecret ApplicationId 18xxxxxxxxxxxxxt TenantId xxxxxx.onmicrosoft.com
06/11/2024, 7:09:07 pm
Verbose
Initializing the Connected To Workloads List.
06/11/2024, 7:09:07 pm
Verbose
ApplicationId, TenantId, CertificateThumbprint were specified. Connecting via Service Principal
06/11/2024, 7:09:07 pm
Verbose
Calling into Connect-M365Tenant

Write-Error: Failed to export M365DSC configuration: The term 'Connect-M365Tenant' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Environment Information + PowerShell Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions