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

Monitoring Orchestrator runbook events from Operations Manager

Today I will follow up on my colleague’s post Mr ITblog (Knut Huglen) about monitoring Orchestrator Runbook events.  He has build a nice double up SNMP loopback feature that does self monitoring in Orchestrator resulting in entries written to a special Windows Eventlog. Now we need to raise alerts in SCOM when one of his runbooks fails or sends a platform event, who knows there could be trouble lurking in his paradise.

We are not going to do anything fancy, however these are the steps we will be focusing on today:
Create a Management Pack for our customizations Create rules that collects the events from the orchestrator serverOff we go then and fire up the SCOM console and a powershell window. First we create a MP, I am going to use powershell to do this, however you may use the SCOM console as well (Administration – ManagementPacks – Action: Create Management Pack):



Import the Management Pack into SCOM and move on to the Authoring section in the SCOM console. Create a new rule:



Give the…

Powershell – Log like you mean it

How do you do logging in powershell? Why should you do logging? What should you log? Where do you put your log? How do you remove your log? How do you search your log? All important questions and how you answer then depends upon what your background is like and the preferences you have. This will be a 2 part blog post and this is part 1.


Why should you log?

Well it is not mandatory, however I have 2 reasons:
Help with debugging a script/module/functionSelf documenting script/module/function
Firstly; Do you know any program that does not contain any bugs? Working with IT for the last 2 decades, I cannot name one. When you create scripts/modules/functions, you will create bugs, that is where they live and try to make your life a living mess.

Secondly: Adding a little extra information to your logging will make them self documenting. Do you like writing documentation? Well I normally am not fond of it and use logging while debugging to get two birds with one stone.


What should you log?

Anyt…

Powershell - List information about your WIFI networks

This is just a quick post about this new function I have created. Basically this is a text-output to powershell object output function that uses netsh to query the WIFI information. This illustrates the importance of changing the authentication level on your WIFI-network. No matter if you use WEP/WPA/WPA2 your password is available in clear text in your profile.



Cheers

Tore