Skip to main content

Desired State Configuration - Consistency


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 (@LogicalDiagram) on twitter. If I understand him correctly, he was wondering about capturing the verbose stream during a system invoked (Consistency Scheduled task) check. DSC has a scheduled task called Consistency (full Task Schedule path is \Microsoft\Windows\Desired State Configuration) which launch every 30 minutes. This task does the equivalent of running the Start-DSCconfiguration cmdlet and making sure that the configuration does not drift from the desired state. Did this a long time ago, however I had forgotten how I could do it.



The scheduled task




image


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:


image

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):
  1. Alias
  2. Function
  3. Cmdlet
  4. 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):


image


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

Comments

  1. 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

    ReplyDelete
  2. hi Kevin,

    Depends 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData. Other Serializing methods In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in: Export-CliXml ConvertTo-JSON Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types: (Hashdata.Object.ps1) You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate t

Toying with audio in powershell

Controlling mute/unmute and the volume on you computer with powershell. Add-Type -TypeDefinition @' using System.Runtime.InteropServices; [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IAudioEndpointVolume { // f(), g(), ... are unused COM method slots. Define these if you care int f(); int g(); int h(); int i(); int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext); int j(); int GetMasterVolumeLevelScalar(out float pfLevel); int k(); int l(); int m(); int n(); int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext); int GetMute(out bool pbMute); } [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IMMDevice { int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev); } [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), Inte

Creating Menus in Powershell

I have created another Powershell module. This time it is about Console Menus you can use to ease the usage for members of your oranization. It is available on GitHub and published to the PowershellGallery . It is called cliMenu. Puppies This is a Controller module. It uses Write-Host to create a Menu in the console. Some of you may recall that using Write-Host is bad practice. Controller scripts and modules are the exception to this rule. In addition with WMF5 Write-Host writes to the Information stream in Powershell, so it really does not matter anymore. Design goal I have seen to many crappy menus that is a mixture of controller script and business logic. It is in essence a wild west out there, hence my ultimate goal is to create something that makes it as easy as possible to create a menu and change the way it looks. Make it easy to build Menus and change them Make it as "declarative" as possible Menus The module supports multiple Men