Welcome to the last blog post of the "PowerShell and Desired State Configuration" tutorial series.
I am hoping that you have enjoyed the all of my previous blog posts which I have written on the various "Desired State Configuration Resources".
Today, we are using "Log Resource" in our "Desired State Configuration" script.
We had see a small demos of usage of "Log Resource" in one of my previous post, but at that time I didn't described it in a detail.
The Syntax of "Log Resource" is :
"Log Resource" have only two parameters, "Message" and "DependsOn" and I hope both of them are very much self-explanatory and there are no need to explain them.
In our below script, we are using two resources boxes, the first one is "Environment Resource" and the second one is the "Log Resource".
In the script the "Log Resource" is depends on "Environment Resource" block, When the "Environment Resource" block run successfully, after that "Log Resource" block will run and will show us a message , that "Windows Environment variable set successfully".
"Log Resource" shows the message configured in the PowerShell console as well as it logged the message in the Microsoft-Windows-Desired State Configuration/Analytic Event log too.".
In our script, we are creating a new Windows Environmental variable , named as "ServerLoc" and I am using the "New Delhi,India" as the variable value, If our Windows Environment variable successfully created, after that "Log Resource" block, will show the message that , "windows Environment variable is set successfully" in the console as well as in Event Log.
Our Script is :
Name = 'ServerLoc'
Value = 'New Delhi, India'
Ensure = 'Present'
DependsOn = "[Environment]serverCoLocation"
Message = " Windows Environment variable set successfully."
The name of Our .ps1 script is , "Demo_Log.ps1" , the name of the DSC Configuration is "logDemo". In our Script we have used "Log and Environment Resources".
But , if you want to see the Analytics Event Logs of Windows-Desired State Configuration , we have to enable them first.
How to do that?
In the below event viewer screenshot, you can see that, Currently we have only "Operational" log enabled.
To see the "Analytic and Debug Logs", Click on "View" option of "Event Viewer" and tick on "Show Analytics and Debug Logs".
When, "Analytic Log" appeared beneath "Desired State Configuration" , right click on "Analytics Log" and click on ""Enable Log".
Everything is set.
Now let's check on our "Posh-Demo server, if the "ServerLoc" variable do exists.
Now can see that , current the "ServerLoc" variable is not yet created.
Run the script and it is export the MOF file.
Start the pushing of the MOF file, using the Start-DscConfiguration cmdlet.
Start-DscConfiguration -path .\logDemo -Wait -Verbose
You can see that, the pushing of the file is successfully, and it also shows us the logged messaged.
Check the server, and you can see that , "ServerLoc" variable is set perfectly.
Open event viewer and you can check the "Analytic Log" and see that , our Log Resource message is logged there too.
That's all for this month.
Thanks a lot for reading through all of this series. I hope you have enjoyed it.
See you next month with something new to share with. ;o) .