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):
- 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.
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.
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):
Changing the Consistency Scheduled task
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