Building a custom DSC Resource using C #

Source: Internet
Author: User

Prepare Prerequisites
    1. 1. VisualStudio recommended 2012 or higher

    2. 2. PowerShell4.0

    3. 3. MOF file

Overview
  1. 1. when building a DSC resource using C #, you also need the MOF (the DSC resource schema file) to generate the file we can build using the Xdscresource tool published on TechNet

  2. 2. The following three cmdlets are also required when building DSC resources using C # Get-targetresource,set-targetresource,test-targetresource

  3. 3. Get-targetresource used to get the current resource state, this command must return a hash dictionary that is System.Collections.Hashtable

  4. 4. Test-targetresource used to detect whether the current resource state is consistent with user input, this command must return a Boolean type that is System.Boolean

  5. 5. Set-targetresource used to complete the user input state, this command does not need to return

Create a MOF schema file

In this example, we will implement the file resource resource name of the system Xfiles

The following is the schema file (file name XFiles.schema.mof)

[Classversion ("1.0.0.0"), FriendlyName ("Xfiles")]

Classxfiles:omi_baseresource

{

[Key] String ensure;

[Required] String Name;

[Required] String Path;

};

Only three parameter ensure,name,path are defined in this architecture and are required

Implementing the Schema

Open vs Create C # class library project

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4F/1D/wKioL1RhcTrxfMUMAAI39fbVxNI503.jpg "title=" 1.png " alt= "Wkiol1rhctrxfmumaai39fbvxni503.jpg"/>

Simultaneous renaming of the assembly name is the DSC resource name, which is Xfiles

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4F/1D/wKioL1RhcUOS8YX5AAEXW5zR198136.jpg "title=" 2.png " alt= "Wkiol1rhcuos8yx5aaexw5zr198136.jpg"/>

Adding a reference to a System.Management.Automation.dll

Path to (. NET version different file path location may vary slightly)

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\ System.Management.Automation.dll

Implement Get-targetresource

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4F/1D/wKioL1RhcU2ThbpPAAIHS9gtGyw273.jpg "title=" 3.png " alt= "Wkiol1rhcu2thbppaaihs9gtgyw273.jpg"/>

Implement Test-targetresource Note that Test-targetresource requires a Boolean type to be returned

However, because the rewrite Processrecord () method must require void so that when we judge the state of the resource

Use the WriteObject () method to return a Boolean type

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4F/1E/wKiom1RhcOuCOgqFAAIdC27D5e0328.jpg "title=" 4.png " alt= "Wkiom1rhcoucogqfaaidc27d5e0328.jpg"/>

Implement Set-targetresource

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4F/1E/wKiom1RhcPSgYxBgAAI7sTf6gOY972.jpg "title=" 5.png " alt= "Wkiom1rhcpsgyxbgaai7stf6goy972.jpg"/>

Then build the solution

Place the generated XFiles.DLL with the XFiles.schema.mof

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\xFiles under

Such as

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4F/1E/wKiom1RhcP-C7I4TAADfFG5tVIs435.jpg "title=" 6.png " alt= "Wkiom1rhcp-c7i4taadffg5tvis435.jpg"/>

Then write the DSC configuration file

Configuration AAA

{

Node LocalHost

{

Xfiles BBB

{

Name = "1.txt"

Path = "C:\2\"

ensure = "absent"

}

}

}

Test

Test resource Status separately

Application configuration (all configuration requirements are not currently satisfied)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4F/1D/wKioL1RhcXbjL1OjAAUc2jGR_RE622.jpg "title=" 7.png " alt= "Wkiol1rhcxbjl1ojaauc2jgr_re622.jpg"/>

Application configuration (partial configuration status satisfies the C:\2 directory in this example)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4F/1E/wKiom1RhcRbwlw3AAAQ4QRLHmvo784.jpg "title=" 8.png " alt= "Wkiom1rhcrbwlw3aaaq4qrlhmvo784.jpg"/>

Undo configuration (All configurations require that all required undo configuration currently exists)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4F/1D/wKioL1RhcYzCDfygAAXSmHj46x4374.jpg "title=" 9.png " alt= "Wkiol1rhcyzcdfygaaxsmhj46x4374.jpg"/>

Undo configuration (Partial configuration state satisfied that C:\2\1.txt does not exist)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4F/1E/wKiom1RhcS7T8sMOAATyXJo4FjU622.jpg "title=" 10.png "alt=" Wkiom1rhcs7t8smoaatyxjo4fju622.jpg "/>

This document is provided by Huang Jinhui Education-----if you have any questions, please contact: QQ2632942715

From "Bright Future" blog 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/ Lang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>http:// Stephen1991.blog.51cto.com



This article from "The Summer of the joint section of Technology" blog, reproduced please contact the author!

Building a custom DSC Resource using C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.