Happy new year!
Ages since I blogged about DSC and found a little topic that might be something others also are wondering about.
I reached out to Chris Hunt (
The scheduled task
As you already probably guessed, this is just a task that executes an powershell command using the cmdet Invoke-CIMmethod with some parameters: The task starts an hidden powershell window and executes the following command:
I have copied the command and applied it to a splatting variable. That makes it much easier to read:
(GIST - Consistency.ps1)
My original thought was to use the Write-Verbose “override” by defining a function called Write-Verbose and capture the verbose output from that. That is possible because Powershell has an internal command resolver that tries to find the command from this priority list (see help about_Command_Precedence):
- Alias
- Function
- Cmdlet
- Native Windows Command
If you create a function with the identical name of an Cmdlet, your function will be executed instead of the real cmdlet. This is also how proxy functions work.
Sadly I must say (no I am kidding), the developers of the Invoke-CIMMethod used fully qualified paths in their call to Write-Verbose so that was a no go.
Redirect streams
June Blender (@juneb_get_help) has written a nice article about redirecting streams on the ScriptingGuys blog (Understanding Streams, Redirection, and Write-Host in PowerShell). Read up about it, it way come useful one day like this moment because we are going to redirect the verbose stream and send it to a file.
Changing the Consistency Scheduled task
We are going to change the action of the task. I prefer to have a powershell file that is launched by the task scheduler instead of a command parameter. Change the action to something like this (you may of course change the path and filename):The powershell file should have something like this:
(GIST - ConsistencyFULL.ps1)
I have added a $outputFile variable that is where the verbose stream will be written. In the foreach loop I write to the file each time a new item is added to the verbosestream/output. This way you can follow along with the DSC engine as is progress. As an alternative, you could just drop the pipe to the foreach loop and assign the output from the Invoke-CIMMethod and write that to the outputfile.
So how to you follow along with the verbose stream. You use the Get-Content Cmdlet with the wait parameter, like so:
Get-Content –Path "c:\temp\ConsistencyverboseStream.txt" -Wait
Of course the file has to exists before you run the Get-Content command. The Out-File cmdlet in the Consistency.ps1 script will create the ConsistencyverboseStream.txt file if it does not exists, however you may create it first and run the Get-Content with the wait flag to prepare yourself before you launch the Consistency Scheduled task.
That is all folks, cheers
How does that compare to the event log entries for DSC? http://blogs.msdn.com/b/powershell/archive/2014/01/03/using-event-logs-to-diagnose-errors-in-desired-state-configuration.aspx
ReplyDeletehi Kevin,
ReplyDeleteDepends on the context of course. If you are looking for an overview of multiple jobs, I would start with the eventlog to find out which of the nodes/configs that was failing. If you want to debug a single node, I would go with the approach listed there or something similar. The verbose output is as the name suggest much more verbose :-) and detailed. It gives you the results of the Test-DSCreource command and if the Set-DSCresource is skipped or executed. The eventlog gives you Information/warnings at the top level.
You can see an example of a verbose output from one of my old posts here: http://asaconsultant.blogspot.no/2014/04/desired-state-configuration-xcomputer.html
Thanks for the comment!
Cheers
Tore