From a2cc984e32c15fad32df8903f0dcfbc4281eda6e Mon Sep 17 00:00:00 2001 From: Nik Charlebois Date: Thu, 11 Jul 2024 08:40:03 -0400 Subject: [PATCH] Release 1.24.710.2 --- CHANGELOG.md | 5 + Modules/Microsoft365DSC/Microsoft365DSC.psd1 | 4 +- .../Modules/M365DSCTelemetryEngine.psm1 | 158 ++++++++++-------- 3 files changed, 91 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9295b7f91..a91791ba43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change log for Microsoft365DSC +# 1.24.710.2 + +* Telemetry + * Fixed error handling on getting roles. + # 1.24.710.1 * AADApplication diff --git a/Modules/Microsoft365DSC/Microsoft365DSC.psd1 b/Modules/Microsoft365DSC/Microsoft365DSC.psd1 index 8eb340653c..b562ab2eab 100644 --- a/Modules/Microsoft365DSC/Microsoft365DSC.psd1 +++ b/Modules/Microsoft365DSC/Microsoft365DSC.psd1 @@ -3,7 +3,7 @@ # # Generated by: Microsoft Corporation # -# Generated on: 2024-07-10 +# Generated on: 2024-07-11 @{ @@ -11,7 +11,7 @@ # RootModule = '' # Version number of this module. - ModuleVersion = '1.24.710.1' + ModuleVersion = '1.24.710.2' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/Modules/Microsoft365DSC/Modules/M365DSCTelemetryEngine.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCTelemetryEngine.psm1 index cdfef0434a..6ac0e57bfb 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCTelemetryEngine.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCTelemetryEngine.psm1 @@ -66,89 +66,61 @@ function Add-M365DSCTelemetryEvent $TelemetryEnabled = [System.Environment]::GetEnvironmentVariable('M365DSCTelemetryEnabled', ` [System.EnvironmentVariableTarget]::Machine) - if ($Type -eq 'DriftEvaluation') - { - try - { - $hostId = (Get-Host).InstanceId - if ($null -eq $Script:M365DSCCountResourceInstance -or $hostId -ne $Script:M365DSCExecutionContextId) - { - $Script:M365DSCCountResourceInstance = 1 - } - else - { - $Script:M365DSCCountResourceInstance++ - } - - if ($hostId -ne $Script:M365DSCExecutionContextId) - { - $Script:M365DSCExecutionContextId = $hostId - } - $Data.Add('ResourceInstancesCount', $Script:M365DSCCountResourceInstance) - $Data.Add('M365DSCExecutionContextId', $hostId) - } - catch - { - Write-Verbose -Message $_ - } - } - - try + if ($null -eq $TelemetryEnabled -or $TelemetryEnabled -eq $true) { - if ($null -ne $Data.ConnectionMode -and $Data.ConnectionMode.StartsWith('Credential')) + if ($Type -eq 'DriftEvaluation') { - if ($null -eq $Script:M365DSCCurrentRoles -or $Script:M365DSCCurrentRoles.Length -eq 0) + try { - try + $hostId = (Get-Host).InstanceId + if ($null -eq $Script:M365DSCCountResourceInstance -or $hostId -ne $Script:M365DSCExecutionContextId) { - Connect-M365Tenant -Workload 'MicrosoftGraph' @Global:M365DSCTelemetryConnectionToGraphParams -ErrorAction SilentlyContinue + $Script:M365DSCCountResourceInstance = 1 } - catch + else { - Write-Verbose -Message $_ + $Script:M365DSCCountResourceInstance++ } - $Script:M365DSCCurrentRoles = @() - - $uri = $Global:MSCloudLoginConnectionProfile.MicrosoftGraph.ResourceUrl + 'v1.0/me?$select=id' - $currentUser = Invoke-MgGraphRequest -Uri $uri -Method GET - $currentUserId = $currentUser.id - $assignments = Get-MgBetaRoleManagementDirectoryRoleAssignment -Filter "principalId eq '$currentUserId'" ` - -Property @('RoleDefinitionId', 'DirectoryScopeId') -All - - $roles = Get-MgBetaRoleManagementDirectoryRoleDefinition -All ` - -Property @('Id', 'DisplayName') - foreach ($assignment in $assignments) + if ($hostId -ne $Script:M365DSCExecutionContextId) { - $role = $roles | Where-Object -FilterScript {$_.Id -eq $assignment.RoleDefinitionId} - if ($null -ne $role) - { - $Script:M365DSCCurrentRoles += $role.DisplayName + '|' + $assignment.DirectoryScopeId - } + $Script:M365DSCExecutionContextId = $hostId } - $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') + $Data.Add('ResourceInstancesCount', $Script:M365DSCCountResourceInstance) + $Data.Add('M365DSCExecutionContextId', $hostId) } - else + catch { - $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') + Write-Verbose -Message $_ } } - elseif ($null -ne $Data.ConnectionMode -and $Data.ConnectionMode.StartsWith('ServicePrincipal')) + + try { - if ($null -eq $Script:M365DSCCurrentRoles -or $Script:M365DSCCurrentRoles.Length -eq 0) + if ($null -ne $Data.ConnectionMode -and $Data.ConnectionMode.StartsWith('Credential')) { - try + if ($null -eq $Script:M365DSCCurrentRoles -or $Script:M365DSCCurrentRoles.Length -eq 0) { - Connect-M365Tenant -Workload 'MicrosoftGraph' @Global:M365DSCTelemetryConnectionToGraphParams -ErrorAction Stop + try + { + Connect-M365Tenant -Workload 'MicrosoftGraph' @Global:M365DSCTelemetryConnectionToGraphParams -ErrorAction SilentlyContinue + } + catch + { + Write-Verbose -Message $_ + } $Script:M365DSCCurrentRoles = @() - $sp = Get-MgServicePrincipal -Filter "AppId eq '$($Global:M365DSCTelemetryConnectionToGraphParams.ApplicationId)'" ` - -ErrorAction 'SilentlyContinue' - if ($null -ne $sp) + $uri = $Global:MSCloudLoginConnectionProfile.MicrosoftGraph.ResourceUrl + 'v1.0/me?$select=id' + $currentUser = Invoke-MgGraphRequest -Uri $uri -Method GET + $currentUserId = $currentUser.id + + $assignments = Get-MgBetaRoleManagementDirectoryRoleAssignment -Filter "principalId eq '$currentUserId'" ` + -Property @('RoleDefinitionId', 'DirectoryScopeId') -All -ErrorAction 'SilentlyContinue' + + if ($null -ne $assignments) { - $assignments = Get-MgBetaRoleManagementDirectoryRoleAssignment -Filter "principalId eq '$($sp.Id)'" ` - -Property @('RoleDefinitionId', 'DirectoryScopeId') -All - $roles = Get-MgBetaRoleManagementDirectoryRoleDefinition -All ` + $roles = Get-MgBetaRoleManagementDirectoryRoleDefinition -All ` -Property @('Id', 'DisplayName') foreach ($assignment in $assignments) { @@ -161,24 +133,58 @@ function Add-M365DSCTelemetryEvent $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') } } - catch + else { - Write-Verbose -Message $_ + $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') } } - else + elseif ($null -ne $Data.ConnectionMode -and $Data.ConnectionMode.StartsWith('ServicePrincipal')) { - $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') + if ($null -eq $Script:M365DSCCurrentRoles -or $Script:M365DSCCurrentRoles.Length -eq 0) + { + try + { + Connect-M365Tenant -Workload 'MicrosoftGraph' @Global:M365DSCTelemetryConnectionToGraphParams -ErrorAction Stop + $Script:M365DSCCurrentRoles = @() + + $sp = Get-MgServicePrincipal -Filter "AppId eq '$($Global:M365DSCTelemetryConnectionToGraphParams.ApplicationId)'" ` + -ErrorAction 'SilentlyContinue' + if ($null -ne $sp) + { + $assignments = Get-MgBetaRoleManagementDirectoryRoleAssignment -Filter "principalId eq '$($sp.Id)'" ` + -Property @('RoleDefinitionId', 'DirectoryScopeId') -All -ErrorAction 'SilentlyContinue' + if ($null -ne $assignments) + { + $roles = Get-MgBetaRoleManagementDirectoryRoleDefinition -All ` + -Property @('Id', 'DisplayName') + foreach ($assignment in $assignments) + { + $role = $roles | Where-Object -FilterScript {$_.Id -eq $assignment.RoleDefinitionId} + if ($null -ne $role) + { + $Script:M365DSCCurrentRoles += $role.DisplayName + '|' + $assignment.DirectoryScopeId + } + } + $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') + } + } + } + catch + { + Write-Verbose -Message $_ + } + } + else + { + $Data.Add('M365DSCCurrentRoles', $Script:M365DSCCurrentRoles -join ',') + } } } - } - catch - { - Write-Verbose -Message $_ - } + catch + { + Write-Verbose -Message $_ + } - if ($null -eq $TelemetryEnabled -or $TelemetryEnabled -eq $true) - { $TelemetryClient = Get-M365DSCApplicationInsightsTelemetryClient try @@ -433,6 +439,10 @@ function Add-M365DSCTelemetryEvent } } } + else + { + return + } } <#