As a PSDSC resource author and DSCv3 user, I want the PowerShell adapter in DSCv3 to be able to automatically handle message stream objects from PSDSC resource invocations, so that I can see the messages in trace output.
Currently, any messages other than errors thrown by the DSC Resource get swallowed instead of bubbled up to the caller.
The Invoke-DscOperation function handles invoking the DSC Resource depending on its implementation details. It also handles writing JSON errors and exiting for DSC from the invocation, like when a user specifies a script-based PSDSC resource on a Linux machine:
I think we could write a small handler that uses stream redirection to capture all emitted stream messages, converts them to JSON[^1], and bubbles them up to DSC before continuing. Something like:
$invokeParams = @{
Method = $Operation
ModuleName = $cachedDscResourceInfo.ModuleName
Name = $cachedDscResourceInfo.Name
Property = $property
}
# Invoke-DscResource only ever returns one object for output, so use
# Select-Object after resolving the invocation output to ensure that
# $invokeResult is the actual output from the command.
$invokeResult = Invoke-DscResource @invokeParams *>&1 |
Resolve-InvocationOutput |
Select-Object -First 1
# continue as normal
This would automatically plumb messages from PSDSC resources up to DSC without requiring resource authors to use any special code in their resources - they just emit messages to the appropriate streams and the adapter bubbles them up in the way that DSC expects.
Summary of the new feature / enhancement
Currently, any messages other than errors thrown by the DSC Resource get swallowed instead of bubbled up to the caller.
Proposed technical implementation details (optional)
In the adapter, we're using the
Invoke()
method on the adapter module to invoke the appropriate DSC operation on a resource:https://github.com/PowerShell/DSC/blob/9ee037ae9a4491d1c8a932a3224c3a281a313cc0/powershell-adapter/psDscAdapter/powershell.resource.ps1#L116
The
Invoke-DscOperation
function handles invoking the DSC Resource depending on its implementation details. It also handles writing JSON errors and exiting for DSC from the invocation, like when a user specifies a script-based PSDSC resource on a Linux machine:https://github.com/PowerShell/DSC/blob/9ee037ae9a4491d1c8a932a3224c3a281a313cc0/powershell-adapter/psDscAdapter/psDscAdapter.psm1#L228-L232
I think we could write a small handler that uses stream redirection to capture all emitted stream messages, converts them to JSON[^1], and bubbles them up to DSC before continuing. Something like:
This would automatically plumb messages from PSDSC resources up to DSC without requiring resource authors to use any special code in their resources - they just emit messages to the appropriate streams and the adapter bubbles them up in the way that DSC expects.
Here I'm mapping the PowerShell output streams to the DSC trace levels as described in the following table:
[^1]: See #421 for the definition of the helper function for emitting JSON messages from the adapter