{"id":6221,"date":"2015-05-11T00:01:00","date_gmt":"2015-05-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/05\/11\/anatomy-of-a-powershell-dsc-resource\/"},"modified":"2019-02-18T10:29:38","modified_gmt":"2019-02-18T17:29:38","slug":"anatomy-of-a-powershell-dsc-resource","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/anatomy-of-a-powershell-dsc-resource\/","title":{"rendered":"Anatomy of a PowerShell DSC Resource"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Guest blogger and Microsoft PFE, Jason Walker, talks about the anatomy of a Windows PowerShell DSC resource.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today is Part 1 of a two-part series written by Microsoft PFE and Honorary Scripting Guy, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/tag\/jason-walker\/\" target=\"_blank\">Jason Walker<\/a>.<\/p>\n<p>The majority of my &quot;PowerShelling&quot; lately has been around Desired State Configuration (DSC). However, the gotcha with DSC is if there isn&rsquo;t a resource for the configuration area that you want to configure, you can&rsquo;t use DSC for that task.<\/p>\n<p>Now let&rsquo;s not jump to conclusions and eliminate DSC as a solution. If you can write a Windows PowerShell script, you can write a DSC resource. A DSC resource is a Windows PowerShell module with one addition. A DSC resource has a MOF file that defines each of the configurable properties. It&rsquo;s that simple.<\/p>\n<p>When designing a resource (see <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dn956964.aspx\" target=\"_blank\">Writing a custom DSC resource with MOF<\/a>), it should be written to configure one thing&mdash;and one thing only. In most cases, a resource should correlate to a cmdlet noun. For example, if you run <b>Get-Command *ADUser<\/b> you will find the following cmdlets:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Windows\\system32&gt; Get-Command *ADUser<\/p>\n<p style=\"margin-left:30px\">CommandType&nbsp; &nbsp;Name<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp; &nbsp;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp; Get-ADUser<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp; New-ADUser<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp; Remove-ADUser<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp; Set-ADUser<\/p>\n<p>These cmdlets enable you to create a user (<b>New-ADUser<\/b>), modify the attributes of a user (<b>Set-ADUser<\/b>), delete a user (<b>Remove-ADUser<\/b>), and find a user and return information about that user (<b>Get-ADUser<\/b>). Now if I wanted to put that user in a specific group, I would have to use a cmdlet that has a different noun (<b>Add-ADGroupMember<\/b>). Therefore, if I was going to use DSC to create a user and place that user in a specific group, I would need two resources (<b>xADUser<\/b> and <b>xADGroupMember<\/b>).<\/p>\n<p>To demonstrate the ease of writing a resource, I will walk you through creating a resource to create a file or a folder. I will call the resource <b>xItem<\/b> because I will be using the <b>New-Item<\/b> and <b>Get-Item<\/b> cmdlets in my resource. In addition, this resource will live in the <b>xFileFolder<\/b> module.<\/p>\n<p>By reviewing the examples in the <b>New-Item<\/b> Help file, we can determine that the resource will need the following properties: <b>Name<\/b>, <b>ItemType<\/b>, and <b>Path<\/b>. (I recommend that when you use native cmdlets in your resources, you make the resource parameter names match the parameter names of the cmdlets that are used in your resource. I will expand on this later.) Because I want the resource to be able to remove a file or folder if needed, I will also need the <b>Ensure<\/b> property.<\/p>\n<p>Now I have everything I need to create the schema (MOF file) for the resource. I will use the <b>xDscResourceDesigner<\/b> module for this. The <b>xDscResourceDesigner<\/b> module simplifies creating the foundation for a DSC resource. The module will create the folder structure for the resource, the MOF file, and a template of the psm1 file. For more instructions about using the <b>xDscResourceDesigner<\/b> module and the details on the MOF file, see the following blog by the Windows PowerShell team: <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2013\/11\/19\/resource-designer-tool-a-walkthrough-writing-a-dsc-resource.aspx\" target=\"_blank\">Resource Designer Tool &ndash; A walkthrough writing a DSC resource<\/a>. Here is my code:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-11-15-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-11-15-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In my example, I defined each property by using the <b>New-xDscResourceProperty<\/b> function. First, I have to define a property that will have the <b>Key<\/b> attribute. The key must be unique among like resources. Because I can&rsquo;t have two files with the same name and the same path, I&rsquo;m going to use the path as the key.<\/p>\n<p>You can also use multiple properties for the key when needed. For example, a resource to create DNS A records would need to have the host name and IP address as key properties because you can have two DNS A records with the same host name (such as in a DNS round-robin scenario), but the IP addresses must be different.<\/p>\n<p>When each property is defined, I pass them to the <b>New-xDscResource<\/b> function. When the <b>New-xDscResource<\/b> function is called, I also specify the module name, resource name, and where I want the resource to live. I left out giving the properties a description so the code would fit on one page. The description is optional, but it&rsquo;s nice if it&rsquo;s there, and I recommend you always include it.<\/p>\n<p>Now back to the code. When running my example, the <b>xDscResourceDesigner<\/b> creates the needed folder structure, schema.mof, and MSFT_xItem.psm1 file (which is where the Windows PowerShell lives). This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-11-15-2.png\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-11-15-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is enough instruction for one day. Tomorrow I will explain how to write the <b>Get<\/b>, <b>Set<\/b>, and <b>Test-Target<\/b> resource functions.<\/p>\n<p>~Jason<\/p>\n<p>Thank you, Jason. This is great information.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Guest blogger and Microsoft PFE, Jason Walker, talks about the anatomy of a Windows PowerShell DSC resource. Microsoft Scripting Guy, Ed Wilson, is here. Today is Part 1 of a two-part series written by Microsoft PFE and Honorary Scripting Guy, Jason Walker. The majority of my &quot;PowerShelling&quot; lately has been around Desired State Configuration [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,338,414,3,4,45],"class_list":["post-6221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-jason-walker","tag-providers","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Guest blogger and Microsoft PFE, Jason Walker, talks about the anatomy of a Windows PowerShell DSC resource. Microsoft Scripting Guy, Ed Wilson, is here. Today is Part 1 of a two-part series written by Microsoft PFE and Honorary Scripting Guy, Jason Walker. The majority of my &quot;PowerShelling&quot; lately has been around Desired State Configuration [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=6221"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6221\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=6221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}