Building a custom DSC Resource using C #

Source: Internet
Author: User

Prepare Prerequisites
1. Visual Studio recommended 2012 or higher
2. PowerShell 4.0
3. mof file
Overview
1. The MOF (i.e. DSC resource schema file) is also required to build the DSC resource when using C # to generate the file we can build it using the Xdscresource tool published on TechNet
2. The following three cmdlets are required to build DSC resources using C # Get-targetresource,set-targetresource,test-targetresource
3. Get-targetresource is used to get the current resource state, this command must return a hash dictionary that is System.Collections.Hashtable
4. Test-targetresource is 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. Set-targetresource is 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")]
class Xfiles: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 itVS Create C # class library project

650) this.width=650; "Width=" 646 "height=" 472 "title=" Open vs Create C # class Library project "style=" WIDTH:504PX;HEIGHT:347PX; "alt=" Open vs Create a C # class library project "src=" Http://www.ilync.cn/photos/attached/2bac4982a7bc771ceb280c8f78fdc6dd.png "border=" 0 "/>
 

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

650) this.width=650; "Width=" 561 "height=" 268 "title=" at the same time renaming the assembly name as the DSC resource name is Xfiles "style=" width:506px;height:245px; " alt= "Renaming the assembly name at the same time as the DSC resource name is Xfiles" src= "Http://www.ilync.cn/photos/attached/74d2674a50a32dfac9cdc204f07168da.png" border= "0"/>
Adding a reference to a System.Management.Automation.dll
Path to (. NET version different file path location may vary slightly)
C:windowsmicrosoft.netassemblygac_msilsystem.management.automationv

4.0_3.0.0.0__31bf3856ad364e35system.management.automation.dll

Implement Get-targetresource

650) this.width=650; "Width=" 435 "height=" 489 "title=" Implementation Get-targetresource "alt=" Implementation Get-targetresource "src="/HTTP/ Www.ilync.cn/photos/attached/bebcb20e1370b53ae53e59ea74b4fa52.png "border=" 0 "/>


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; "Title=" uses the WriteObject () method to return the Boolean type "alt=" using the WriteObject () method to return the Boolean type "src=" http://www.ilync.cn/ Photos/attached/ace2436eebb15e1d70ed217778ab6eb3.png "border=" 0 "/>

Use the WriteObject () method to return a Boolean type
650) this.width=650; "title=" Implementation Set-targetresource "alt=" Implementation Set-targetresource "src=" http://www.ilync.cn/photos/ Attached/c06b6f23a32cab2b9d59d46183095656.png "border=" 0 "/>

implement Set-targetresource
Then build the solution
will generate XFiles.DLL and XFiles.schema.mof together in
c: Windowssystem32windowspowershellv1.0modulespsdesiredstateconfigurationdscresourcesxfiles:

650) this.width=650; "Width=" 725 "height=" 156 "title=" puts the generated XFiles.DLL with xFiles.schema.mof in "style=" width:561px ; height:103px; "alt=" puts the generated XFiles.DLL with xFiles.schema.mof in "src=" http://www.ilync.cn/photos/attached/ 4c40d0448e5ba48264d17809be413e58.png "border=" 0 "/>  

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; "Width=" 896 "height=" 434 "title=" Application configuration (all configuration requirements are not currently satisfied) "style=" WIDTH:614PX;HEIGHT:239PX; "alt=" Application configuration (all configuration requirements are not currently met) "Src=" http://www.ilync.cn/photos/attached/a203728d3a29f3e1e1fb113a2e3a2182.png "border=" 0 "/ >

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

650) this.width=650; "Width=" 1119 "height=" 441 "title=" Application configuration (partial configuration status satisfies C:2 directory present in this example) "style=" WIDTH:619PX;HEIGHT:272PX; " alt= "Application configuration (partial configuration status is present in C:2 directory in this example)" Src= "Http://www.ilync.cn/photos/attached/9b7b377e313a5079825170afb8cd83b2.png" border= "0"/>

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

650) this.width=650; "Width=" 794 "height=" 549 "title=" Undo configuration (all configurations required to meet all needs undo configuration currently exists) "style=" width:620px;height:399px , "alt=" revocation configuration (all configurations require that all required undo configuration currently exists) "src=" http://www.ilync.cn/photos/attached/ 5344a4c4967b432813e3715f2c155928.png "border=" 0 "/>


Undo configuration (Partial configuration state satisfied that C:21.txt does not exist)

650) this.width=650; "Width=" 751 "height=" 417 "title=" Undo configuration (partial configuration state satisfied that C:21.txt does not exist) "style=" WIDTH:621PX;HEIGHT:298PX; " alt= "Undo configuration (partial configuration state satisfied that C:21.txt does not exist)" src= "Http://www.ilync.cn/photos/attached/23161425b02bbac6a4c2af418296356a.png" border= "0"/>


This article was released in www.ilync.cn in 2014-08-06

This article from the "Automation World" blog, declined reprint!

Building a custom DSC Resource using C #

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.