Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

SC* resources fail with 'Object reference not set to an instance of an object' - Using Export-M365DSCConfiguration

Open Dekor86 opened this issue 1 month ago • 7 comments

Description of the issue

Similar to issue #7563 where all SC resources fail to export, seeing this behaviour in both version 2.26.1021.1 and 2.26.1028.1 of M365DSC.

Azure Pipelines being utilised with tasks that install M365 DSC in PowerShell 5, update dependencies and also perform update dependencies in PowerShell 7. In PowerShell 7 dependency update can see that newer version of M365DSC.mgx (2.1.10) is being updated:

Using Install-PSResource to install M365DSC.mgx with version {2.1.10}

However, even with the newer version, still seeing the issue with SC resource capture failing.

Have tested the connect-ippssession manually in PowerShell 7 using the service principal and certificate and connects without issue.

Microsoft 365 DSC Version

2.26.1028.1

Which workloads are affected

Security & Compliance Center

The DSC configuration

Export-M365DSCConfiguration `
        -ApplicationId "$(appID)" `
        -TenantId "$(tenantDomain)" `
        -CertificateThumbprint "$(certificateThumbprint)" `
        -Workloads @(
          "SC"
        ) `
        -Mode Full `
        -Validate `
        -Path $snapshotPath `
        -FileName "devTenantSnapshot.ps1" `
        -ConfigurationName "DevTenantSnapshot" `
        -WithStatistics

Verbose logs showing the problem

Exporting Microsoft 365 configuration for Workloads: SC
Finding all resources for workload {SC} and Mode {Full}
 
Authentication methods specified:
- Service Principal with Certificate Thumbprint
 
Starting export in sequential mode...
[1/40] Extracting [SCAdaptiveScope] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAdaptiveScope}: Object reference not set to an instance of an object.
[2/40] Extracting [SCAppRetentionCompliancePolicy] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAppRetentionCompliancePolicy}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.
[3/40] Extracting [SCAppRetentionComplianceRule] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAppRetentionComplianceRule}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.
[4/40] Extracting [SCAuditConfigurationPolicy] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAuditConfigurationPolicy}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.
[5/40] Extracting [SCAutoSensitivityLabelPolicy] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAutoSensitivityLabelPolicy}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.
[6/40] Extracting [SCAutoSensitivityLabelRule] using {CertificateThumbprint}...❌

Environment Information + PowerShell Version

Azure Pipelines - Windows-latest

PowerShell Task  2.281.0 - https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell

PowerShell version C:\Program Files\PowerShell\7\pwsh.exe

Dekor86 avatar Nov 02 '26 11:11 Dekor86

That's strange. I have a very similar setup using a self-hosted pool with the Microsoft provided image, installing M365DSC and the modules in WinPS, then exporting the workloads in different job steps after another (always a new step for each export to not cause "assembly conflicts"). Are you exporting several workloads in the same step, even if one after another? Is it possible for you to share a minimal pipeline setup with us so that we can try to reproduce it? That would be very helpful to determine where the error is.

FabienTschanz avatar Nov 05 '26 14:11 FabienTschanz

I think you've just helped me pin point the issue. We run a job that exports all workloads. We found a previous issue where OneDrive export would fail if we also included AAD workload. We fixed this by performing a pre authentication with PnP before the export so the assemblies were loaded. I've just tried removing the PnP step and this has actually now allowed SC to export.

So whilst I have a fix, it leads me back to the issue with PnP assemblies being incorrect loaded when combining AAD and OD workloads. Any thoughts?

# 7. Install required PowerShell modules
- task: PowerShell@2
  displayName: 'Install M365DSC modules'
  inputs:
    targetType: inline
    pwsh: false
    script: |
      $ErrorActionPreference = 'Stop'
      Install-Module Az.Accounts -Force -AllowClobber -Scope CurrentUser
      Install-Module Az.KeyVault -Force -AllowClobber -Scope CurrentUser
 
      Install-Module Microsoft365DSC `
        -RequiredVersion '2.26.1028.1' `
       -Force -AllowClobber -Scope AllUsers
       Import-Module Microsoft365DSC -RequiredVersion '2.26.1028.1' -Force
       Update-M365DSCDependencies
 
- task: PowerShell@2
  displayName: 'Update M365DSC dependencies for PowerShell 7'
  inputs:
    targetType: inline
    pwsh: true
    script: |
      $ErrorActionPreference = 'Stop'
      Import-Module Microsoft365DSC -RequiredVersion '2.26.1028.1' -Force
      Update-M365DSCDependencies

# 14. Capture the full Dev tenant configuration
- task: PowerShell@2
  displayName: 'Capture Dev tenant snapshot'
  inputs:
    targetType: inline
    pwsh: true
    script: |
      $snapshotPath = "$(Agent.TempDirectory)\M365DSC-MOF\$(snapshotName)"

      New-Item -ItemType Directory -Path $snapshotPath -Force | Out-Null

      Import-Module Microsoft365DSC -RequiredVersion '2.26.1028.1' -Force

      Import-Module PnP.PowerShell -Force
      
      Write-Host "Pre-loading PnP connection"

      Connect-PnPOnline `
          -Url "https://m365globaldev-admin.sharepoint.com" `
          -ClientId "$(appID)" `
          -Tenant "$(tenantDomain)" `
          -Thumbprint "$(certificateThumbprint)"

      Write-Host "PnP connected successfully"

      Export-M365DSCConfiguration `
        -ApplicationId "$(appID)" `
        -TenantId "$(tenantDomain)" `
        -CertificateThumbprint "$(certificateThumbprint)" `
        -Workloads @(
          "SC"
        ) `
        -Mode Full `
        -Validate `
        -Path $snapshotPath `
        -FileName "devTenantSnapshot.ps1" `
        -ConfigurationName "DevTenantSnapshot" `
        -WithStatistics

      if ($LASTEXITCODE -and $LASTEXITCODE -ne 0) {
        throw 'M365DSC export failed.'
      }

      if (!(Get-ChildItem -Path $snapshotPath -File -Recurse)) {
        throw 'No snapshot files were generated.'
      }

Dekor86 avatar Nov 05 '26 15:11 Dekor86

Oddly, I've just tested in 2.26.1028.1 and with just AAD and OD the connection error doesn't happen. So then tested with all my workloads:

      Export-M365DSCConfiguration `
        -ApplicationId "$(appID)" `
        -TenantId "$(tenantDomain)" `
        -CertificateThumbprint "$(certificateThumbprint)" `
        -Workloads @(
          "AAD", "ADO", "COMMERCE", "DEFENDER", "EXO",
          "FABRIC", "INTUNE", "O365", "OD", "PLANNER", "PP",
          "SC", "SENTINEL", "SH", "SPO", "TEAMS", "VIVA"
        ) `

OneDrive settings work, SharePoint settings work, but now Teams fails and Security and Compliance Center. Seems so odd behaviour with assemblies when trying to combine multiple workloads. Have you tested with all workloads?

Dekor86 avatar Nov 05 '26 15:11 Dekor86

Just confirmed it, testing with just OneDrive and Security and Compliance Center workloads breaks the export of SC:

      Export-M365DSCConfiguration `
        -ApplicationId "$(appID)" `
        -TenantId "$(tenantDomain)" `
        -CertificateThumbprint "$(certificateThumbprint)" `
        -Workloads @(
          "OD", "SC"
        ) `
        -Mode Full `
        -Validate `
        -Path $snapshotPath `
        -FileName "devTenantSnapshot.ps1" `
        -ConfigurationName "DevTenantSnapshot" `
        -WithStatistics

Produces these errors:

Finding all resources for workload {OD} and Mode {Full}
Finding all resources for workload {SC} and Mode {Full}
 
Authentication methods specified:
- Service Principal with Certificate Thumbprint
 
Starting export in sequential mode...
[1/41] Extracting [ODSettings] using {CertificateThumbprint}...✅
[2/41] Extracting [SCAdaptiveScope] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAdaptiveScope}: Object reference not set to an instance of an object.
[3/41] Extracting [SCAppRetentionCompliancePolicy] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAppRetentionCompliancePolicy}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.
[4/41] Extracting [SCAppRetentionComplianceRule] using {CertificateThumbprint}...❌
    An error occurred while exporting resource {SCAppRetentionComplianceRule}: Connection to SecurityComplianceCenter failed earlier in this session: Object reference not set to an instance of an object. Skipping.```

Dekor86 avatar Nov 05 '26 15:11 Dekor86

Thank you for pinpointing it. This most likely has to do with assembly conflicts regarding assembly isolation in PowerShell. PnP.PowerShell should have the isolation, but maybe it's not correct or missing something. And EXO doesn't have any, which means it will always leak and cause issues unfortunately. Will check if we can add an improvement to the PnP.PowerShell module through an upstream fix. Keeping you updated. For now, please run the exports in separate steps to work around that issue.

FabienTschanz avatar Nov 06 '26 14:11 FabienTschanz

After some more troubleshooting, I found that there is already a fix available for it in the nightly version of PnP.PowerShell. Unfortunately that fix isn't yet released, which means that even if we wanted to update, we aren't able to right now. We have to wait until a new version is released. This will most likely be the 4.0.0 release. The publish date is unknown.

FabienTschanz avatar Nov 06 '26 22:11 FabienTschanz

I checked the latest PnP.PowerShell release 3.5.0 and even though the fix is in, we still have some issues with modules clashing. I'll investigate further.

FabienTschanz avatar Nov 18 '26 18:11 FabienTschanz