Desired State Configuration and Hyper-V – Part 3: Generating MOF files

Hello all, and welcome back to part 3 of our series on Microsoft’s new technology: PowerShell Desired State Configuration!

Part one in this series introduced the concept of Desired State Configuration (or DSC for short) and why you should be using it inside your environment. Part two covered the concept of DSC resources. These resources determine what aspects of the target system DSC is able to modify and control.

Part 1 – DSC Intro and Requirements | Part 2 – DSC Resources

High Level Overview

In today’s post we’re going to take those resources and we’re going to use them to generate a MOF file that we can use to push the DSC config to our target nodes. Before I start explaining the concept of generating MOF files, I think it’s important to view the entire DSC process at a high level…..

 

DSC_Workflow

 

As you can see in the above image, it really boils down to a five step process. The first two sections were covered in parts 1 and 2 of this series respectively, so I won’t bother you with those details again. This particular post will focus on the 3rd and 4th section of this process. The first thing we’re going to do is generate a basic configuration file. We’ll then execute that file and the DSC logic built into PowerShell will spit out a MOF file that we can use to target specific changes or configurations on our target nodes.

Let’s get started with the configuration Script and you’ll see how this comes together.

Configuration Script Generation

As I stated in a previous post, people who have some pre-working knowledge of Windows PowerShell can leverage that knowledge to make the following configuration files as complex or as simple as they would like to complete the specific tasks at hand, but as the possibilities are essentially endless, it becomes nearly impossible to cover every possible outcome. This series really serves as an introduction to DSC in that it shows you enough to get started making your own MOF files. With that said, I’ll be demoing a very basic configuration that should be easy to follow, even for the folks with little PowerShell experience.

If you are interested in deeper resources on this subject, the team over at PowerShell.org has a great forum on this subject, and it can be referenced for more advanced configurations as needed.

Now that we have that out of the way, let’s look at a basic DSC configuration that will insure that the Hyper-V and Failover Clustering roles/features are installed on a target node. You are reading this on a Hyper-V blog after all!

So in order to generate the MOF file necessary to enforce those needs, we need to create a configuration file using PowerShell. As you’ll see in the below script I’ve generated, we really have a couple of key sections that are needed in order for the MOF file to generate properly.

Configuration HVHostRoles
{
        Node ANDO-HV01.andonet.lcl
        {
            WindowsFeature HyperVRole
                {
                     Name = "Hyper-V"
                     Ensure = "Present"
                     LogPath = "C:\DSCLog_HyperV.txt"
                }
            WindowsFeature FailoverClusterRole
                {
                    Name = "Failover-Clustering"
                    Ensure = "Present"
                    LogPath = "C:\DSCLog_FailoverClustering.txt"
                }
        }
}
HVHostRoles

The very top section is the “Configuration” section. This is the general container for everything contained within the config. In addition, the string HVHostRoles, is completely changeable. It’s just a name for you’re configuration. In this case “HVHostRoles”.

The next section of note you’ll see is the “Node” Section. You’ll see that in this particular example, I’m targeting the “ANDO-HV01.andonet.lcl” host specifically. You can work some PowerShell magic here if you’d like and put in some parameterization of the hostname so you can define multiple nodes in the script, but for the sake of simplicity in this example, I’ve decided to target a specific machine.

Finally you’ll see the section mentioning “WindowsFeature”. This is the section that defines what the configuration is going to be modifying. This is where the DSC Resources from part 2 of this series come in. In this example I’ve called out the WindowsFeature DSC Resource specifically as I want to ensure that the Hyper-V role and the Failover-Clustering features are installed and present on these machines at all times. Much like the configuration section listed above, you can see some modifiable strings for naming purposes in this area of the script. In this case I’ve labeled each section with the Windows role/feature that it’s working with.

Contained within this section, you’ll need to define the proper syntax for each DSC Resource in use. The syntax for each configuration item can be pulled by utilizing the Get-DscResource cmdlet in PowerShell as mentioned in part 2.

In this case the syntax for the WindowsFeature DSC Resource can be determined by issuing the below command.

DSC_Part3_1

As you can see I’ve pulled the pieces that I needed out of the syntax. I’ve told DSC that I want it to look at the “Hyper-V” role and the “Failover-Clustering” feature. I then told it that it needs to Ensure that those items are “Present”.

Really those are the only two options that are needed, but I took an extra step in defining a log path as well. This log path is not going to be used by the role/feature installer for logging purposes, but it will be used by DSC itself at the time of use. This allows us to troubleshoot any DSC related issues that may arise in the future.

Should you want to utilize any other DSC Resource in your own custom scripts, you only need to replace the “WindowsFeature” sections with the applicable DSC Resource and the appropriate syntax.

Finally the last line of the configuration is exactly the same string of text as we used for the configuration name on the very first line. What this last line is doing, is executing the configuration defined above, which in turn, will generate a MOF file and place it in a subfolder in whatever directory the configuration script was executed from.

Finishing Up

Once your script is complete, save it, and execute it using PowerShell.

Congrats! You’ve just generated your first MOF file!

DSC_Part3_2

At this point, we have a MOF file that we can apply to a target node that will enforce the configuration we’ve defined. Upcoming part 4 will discuss how we deploy this MOF file to the target node.

Stay tuned for more and as always, thanks for reading!

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

8 thoughts on "Desired State Configuration and Hyper-V – Part 3: Generating MOF files"

  • Yanni says:

    Hi Andy

    I am trying to create my first .mof file using your script file “HVHostRoles” configuration file but nothing is created. No errors or warnings either.

    Any ideas why?

    Many thanks

  • Yanni says:

    Hi Andy

    I am trying to create my first .mof file using your script file “HVHostRoles” configuration file but nothing is created. No errors or warnings either.

    Any ideas why?

    Many thanks

  • Kurt Johnson says:

    Andy, I have been looking for an answer to this and have yet to find it… when generating a MOF file, it creates itself in the same path from which your Powershell script is run. Is there a way to -Path out the location of the MOF file so I can centrally manage them? I am all over the place in PowerShell (sometimes in c:users and sometimes in C:WindowsSystem32, etc.) and I don’t want to be worried about where my MOF files are going to drop. But I can find no documentation on how to force a particular path on the output. Or am I making it too complicated?

    • Hi Kurt!

      You’re right… there seems to be very little documentation on that.

      Personally, whenever I work with MOF files, I’m always executing the scripts in a central location.

      I could think of a few things that might help you…

      1. You could set a $MOFConfigPath variable with the path where you would like your MOF files to reside, place your configs there and call them from wherever you are using the variable.
      2. You could use powershell to move stuff around after the fact.

      Either option should work fairly well.

      Hope this helps!

  • Kurt Johnson says:

    Andy, I have been looking for an answer to this and have yet to find it… when generating a MOF file, it creates itself in the same path from which your Powershell script is run. Is there a way to -Path out the location of the MOF file so I can centrally manage them? I am all over the place in PowerShell (sometimes in c:users and sometimes in C:WindowsSystem32, etc.) and I don’t want to be worried about where my MOF files are going to drop. But I can find no documentation on how to force a particular path on the output. Or am I making it too complicated?

    • Hi Kurt!

      You’re right… there seems to be very little documentation on that.

      Personally, whenever I work with MOF files, I’m always executing the scripts in a central location.

      I could think of a few things that might help you…

      1. You could set a $MOFConfigPath variable with the path where you would like your MOF files to reside, place your configs there and call them from wherever you are using the variable.
      2. You could use powershell to move stuff around after the fact.

      Either option should work fairly well.

      Hope this helps!

Leave a comment or ask a question

Your email address will not be published. Required fields are marked *

Your email address will not be published. Required fields are marked *

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.