Showing posts with label LOG DSC Resource. Show all posts
Showing posts with label LOG DSC Resource. Show all posts

Thursday, May 29, 2014

PowerShell and DSC : Learn How to use Windows PowerShell DSC Log Resource

 

1.  Introduction of Windows PowerShell “Desired State Configuration”.

2.  Installing Windows PowerShell 4.0 [Windows Management Framework 4.0].

3.  Getting Started With Desired State Configuration: DSC Syntax.

4.  Review of Desired State Configuration: The 3 easy steps.

5.  Write your First Desired State Configuration Script using ROLE Resource.

6.  Run your first "DSC" PowerShell Script.

7. Configuring Dependencies in "Desired State Configuration" script in PowerShell.

8. PowerShell and DSC : Using File Resource

9. Using Registry Resource

10. Using Archive Resource.

11. Using Group Resource.

12. Using User Resource

13. Using Service Resource.

14. Configure the Service to run under different account name in Service Resource.

15. How to use Windows PowerShell Desired State Configuration Environment Resource.

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 :

29-05-2014 13-21-44

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

But Wait!!! 

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

Cool, Isn't?

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 :

Configuration logDemo
{
Node 'Posh-Demo'
{
Environment serverCoLocation
{
Name = 'ServerLoc'
Value = 'New Delhi, India'
Ensure = 'Present'
}

Log loggingInfo
{
DependsOn = "[Environment]serverCoLocation"
Message = " Windows Environment variable set successfully."
}
}
}


logDemo



 


29-05-2014 13-22-43


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.


29-05-2014 12-48-08


To see the "Analytic and Debug Logs", Click on "View" option of "Event Viewer" and tick on "Show Analytics and Debug Logs".


29-05-2014 13-10-24


When, "Analytic Log" appeared beneath "Desired State Configuration" , right click on "Analytics Log" and click on ""Enable Log".


29-05-2014 13-08-01


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.


29-05-2014 12-41-33


Run the script and it is export the MOF file.


29-05-2014 12-42-12


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.


29-05-2014 12-43-25


Check the server, and you can see that , "ServerLoc" variable is set perfectly.


29-05-2014 12-46-37


Open event viewer and you can check the "Analytic Log" and see that , our Log Resource message is logged there too.


29-05-2014 13-09-40


Perfect, Isn't?


That's all for this month.


Thanks a lot for reading through all of this series.  I hope you have enjoyed it.


thanks


See you next month with something new to share with. ;o) .


With Regards


Aman Dhally


 


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.


 


Thursday, April 24, 2014

Configuring Dependencies in "Desired State Configuration" script in PowerShell

 

1.  Introduction of Windows PowerShell “Desired State Configuration”.

2.  Installing Windows PowerShell 4.0 [Windows Management Framework 4.0].

3.  Getting Started With Desired State Configuration: DSC Syntax.

4.  Review of Desired State Configuration: The 3 easy steps.

5.  Write your First Desired State Configuration Script using ROLE Resource.

6.  Run your first "DSC" PowerShell Script.

 

 

w-alphabet-kids-coloring-pictures-printablee learn the basics of the “DSC”, we learn how to write our first DSC script and how to run and deploy it.

The one of the most uber-cool feature of the "Desired State Configuration" is the power of setting dependencies. Yes, you read right, set dependencies.

In "Desired State Configuration" scripts , we can set dependencies of one resource  to another resource.

For example : If you want to install IIS services on your server, and you want to install IIS services after the installation of DNS server roles, in that case you set dependency on the IIS windows feature script block to check for DNS first and before installing, if DNS role is not installed for for it's installation and then run it.

 

How to set the dependencies?

Most of the DSC resource has the parameter “DependsOn”. We need to use this parameter  to set dependencies.

 

What is the Syntax of the “DependsOn”?

The Syntax of the DependsOn is.

 

DepedsON = [DSC Resource Type]Name of DSC Resource Block Name

 

DSC

 

Let’s get started.

 

To check and set the dependencies, I written a small script. This script install FAX server role on our server, and once the Roles in installed, it will show us a small message that FAX server roles is installed successfully.

For showing this message, we are using LOG DSC resource, this LOG resource do support only two parameters, "Message" and "DependsON".

 

clip_image003

This is our script

Configuration settingDepend {

   

    Node 'Posh-Demo'

        {

   

           WindowsFeature installFax

            {

           

            Name = 'Fax'

            Ensure = 'Present'

                       

            }

 

 

            Log myMessage

             {

               

                Message = "The Fax Service Role is installed, Now we can start using fax."

                DependsOn = "[WindowsFeature]installFax"

            

            

             }

 

   

        }

 

 

}

 

settingDepend

 

 

 

I am not going in details on how this script is written and we already had discussed in our previous blog posts.

The only new in this script is that we are using the “DependsOn” parameter in the LOG resource block.

 

 

24-04-2014 12-41-16

 

You can see how we have set dependency in the Log Resource.

Picture2

 

 

You can see that “Fax Server” service is not installed on the server yet.

 

clip_image008

 

Run the script, and you can see that it created the folder and the MOF file is created too.

24-04-2014 11-35-00

 

Not’ let’s start the deployment of the MOF file using the below Start-DscConfiguration command.

 

Start-DscConfiguration .\settingDepend -Wait -Verbose 

 

After running the command it has started the deployment of the MOF files.

24-04-2014 11-35-49

 

And you can see it “Log” the message after the installation of the Fax server roles.

 

24-04-2014 12-35-56

 

And if you check our server, the FAX role is installed too.

24-04-2014 12-17-03

 

That’s all for today  J , See you in next blog post.

 

loki-2 

 

Regards

Aman Dhally

If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.