Skip to content

Commit

Permalink
Merge pull request #3091 from NikCharlebois/Logging-Improvements
Browse files Browse the repository at this point in the history
Fixes #2981 - Logging improvements
  • Loading branch information
NikCharlebois authored Mar 29, 2023
2 parents 806c726 + c394c8a commit 23c0578
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
* Major changes to the export process where resource instances will now be assigned a meaningful nam
that will follow the ResourceName-PrimaryKey convention.
* Added a fix making sure that the progress bar "Scanning dependencies" is no longer displayed after the operation is completed.

* Changed configuration drift reporting to event log to include the instance name as the source.
FIXES [#2981](https://github.com/microsoft/Microsoft365DSC/issues/2981)

# 1.23.322.1

Expand Down
39 changes: 39 additions & 0 deletions Modules/Microsoft365DSC/Modules/M365DSCLogEngine.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,48 @@ function Assert-M365DSCIsNonInteractiveShell
return $true
}

<#
.Description
This function retrieves the name of the last resource instance being processed in the log files.
.Functionality
Private
#>
function Get-M365DSCCurrentResourceInstanceNameFromLogs
{
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter()]
[System.String]
$ResourceName
)
try
{
$allEvents = Get-WinEvent -LogName "Microsoft-windows-dsc/operational" -MaxEvents 10
foreach ($event in $allEvents)
{
$message = $event.Message
$stringToFind = "Resource execution sequence :: [$($ResourceName.Split('_')[1])]"
$start = $message.IndexOf($stringToFind)
if ($start -ge 0)
{
$end = $message.IndexOf(".", $start)
return $message.Substring($start + 31, $end-($start + 31))
}
}
}
catch
{
Write-Verbose -Message $_
}
return $null
}

Export-ModuleMember -Function @(
'Add-M365DSCEvent',
'Export-M365DSCDiagnosticData',
'Get-M365DSCCurrentResourceInstanceNameFromLogs',
'New-M365DSCLogEntry',
'Get-M365DSCNotificationEndPointRegistration',
'New-M365DSCNotificationEndPointRegistration',
Expand Down
13 changes: 9 additions & 4 deletions Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,13 @@ function Test-M365DSCParameterState

if ($returnValue -eq $false)
{
$currentInstanceName = Get-M365DSCCurrentResourceInstanceNameFromLogs -ResourceName $Source
if ([System.String]::IsNullOrEMpty($currentInstanceName))
{
$currentInstanceName = $Source
}
$EventMessage = "<M365DSCEvent>`r`n"
$EventMessage += " <ConfigurationDrift Source=`"$Source`">`r`n"
$EventMessage += " <ConfigurationDrift Source=`"$Source`" InstanceName=`"$currentInstanceName`">`r`n"

$EventMessage += " <ParametersNotInDesiredState>`r`n"
foreach ($key in $DriftedParameters.Keys)
Expand Down Expand Up @@ -905,7 +910,7 @@ function Test-M365DSCParameterState
$EventMessage += '</M365DSCEvent>'

Add-M365DSCEvent -Message $EventMessage -EventType 'Drift' -EntryType 'Warning' `
-EventID 1 -Source $Source
-EventID 1 -Source $currentInstanceName
}

#region Telemetry
Expand Down Expand Up @@ -2614,7 +2619,7 @@ function Test-M365DSCDependenciesForNewVersions
}
$i++
}

# The progress bar seems to hang sometimes. Make sure it is no longer displayed.
Write-Progress -Activity 'Scanning Dependencies' -Completed
}
Expand Down Expand Up @@ -2691,7 +2696,7 @@ function Update-M365DSCDependencies
}
$i++
}

# The progress bar seems to hang sometimes. Make sure it is no longer displayed.
Write-Progress -Activity 'Scanning Dependencies' -Completed

Expand Down

0 comments on commit 23c0578

Please sign in to comment.