在 C# 中创作 DSC 资源
适用于:Windows PowerShell 4.0 和 Windows PowerShell 5.0
通常在 PowerShell 脚本中实现 Windows PowerShell Desired State Configuration (DSC) 自定义资源。 但是你也可以通过在 C# 中编写 cmdlet 来实现 DSC 自定义资源的功能。 有关在 C# 中编写 cmdlet 的介绍,请参阅编写 Windows PowerShell Cmdlet。
除在 C# 中用 cmdlet 实现资源外,创建 MOF 架构、创建文件夹结构、导入和使用自定义 DSC 资源的过程都与使用 MOF 编写自定义 DSC 资源中介绍的相同。
编写基于 cmdlet 的资源
此示例中,我们将实现一个管理文本文件及其内容的简单资源。
编写 MOF 架构
下面是 MOF 资源的定义。
[ClassVersion("1.0.0"), FriendlyName("xDemoFile")]
class MSFT_XDemoFile : OMI_BaseResource
{
[Key, Description("path")] String Path;
[Write, Description("Should the file be present"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Write, Description("Contentof file.")] String Content;
};
设置 Visual Studio 项目
设置 cmdlet 项目
- 打开 Visual Studio。
- 创建 C# 项目并命名。
- 从可用的项目模板中选择类库。
- 单击“确定” 。
- 将 System.Automation.Management.dll 的程序集引用添加到项目。
- 更改程序集名称,使其与资源名称一致。 在本示例中,程序集应该被命名为 MSFT_XDemoFile。
编写 cmdlet 代码
以下 C# 代码实现 Get-TargetResource
、Set-TargetResource
和 Test-TargetResource
cmdlet。
namespace cSharpDSCResourceExample
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation; // Windows PowerShell assembly.
#region Get-TargetResource
[OutputType(typeof(System.Collections.Hashtable))]
[Cmdlet(VerbsCommon.Get, "TargetResource")]
public class GetTargetResource : PSCmdlet
{
[Parameter(Mandatory = true)]
public string Path { get; set; }
/// <summary>
/// Implement the logic to return the current state of the resource as a hashtable with
/// keys being the resource properties and the values are the corresponding current
/// value on the machine.
/// </summary>
protected override void ProcessRecord()
{
var currentResourceState = new Dictionary<string, string>();
if (File.Exists(Path))
{
currentResourceState.Add("Ensure", "Present");
// read current content
string CurrentContent = "";
using (var reader = new StreamReader(Path))
{
CurrentContent = reader.ReadToEnd();
}
currentResourceState.Add("Content", CurrentContent);
}
else
{
currentResourceState.Add("Ensure", "Absent");
currentResourceState.Add("Content", "");
}
// write the hashtable in the PS console.
WriteObject(currentResourceState);
}
}
# endregion
#region Set-TargetResource
[OutputType(typeof(void))]
[Cmdlet(VerbsCommon.Set, "TargetResource")]
public class SetTargetResource : PSCmdlet
{
[Parameter(Mandatory = true)]
public string Path { get; set; }
[Parameter(Mandatory = false)]
[ValidateSet("Present", "Absent", IgnoreCase = true)]
public string Ensure {
get
{
// set the default to present.
return (this._ensure ?? "Present") ;
}
set
{
this._ensure = value;
}
}
[Parameter(Mandatory = false)]
public string Content {
get { return (string.IsNullOrEmpty(this._content) ? "" : this._content); }
set { this._content = value; }
}
private string _ensure;
private string _content;
/// <summary>
/// Implement the logic to set the state of the machine to the desired state.
/// </summary>
protected override void ProcessRecord()
{
WriteVerbose(string.Format("Running set with parameters {0}{1}{2}", Path, Ensure, Content));
if (File.Exists(Path))
{
if (Ensure.Equals("absent", StringComparison.InvariantCultureIgnoreCase))
{
File.Delete(Path);
}
else
{
// file already exist and ensure "present" is specified. start writing the content to a file
if (!string.IsNullOrEmpty(Content))
{
string existingContent = null;
using (var reader = new StreamReader(Path))
{
existingContent = reader.ReadToEnd();
}
// check if the content of the file mathes the content passed
if (!existingContent.Equals(Content, StringComparison.InvariantCultureIgnoreCase))
{
WriteVerbose("Existing content did not match with desired content updating the content of the file");
using (var writer = new StreamWriter(Path))
{
writer.Write(Content);
writer.Flush();
}
}
}
}
}
else
{
if (Ensure.Equals("present", StringComparison.InvariantCultureIgnoreCase))
{
// if nothing is passed for content just write "" otherwise write the content passed.
using (var writer = new StreamWriter(Path))
{
WriteVerbose(string.Format("Creating a file under path {0} with content {1}", Path, Content));
writer.Write(Content);
}
}
}
/* if you need to reboot the VM. please add the following two line of code.
PSVariable DscMachineStatus = new PSVariable("DSCMachineStatus", 1, ScopedItemOptions.AllScope);
this.SessionState.PSVariable.Set(DscMachineStatus);
*/
}
}
# endregion
#region Test-TargetResource
[Cmdlet("Test", "TargetResource")]
[OutputType(typeof(Boolean))]
public class TestTargetResource : PSCmdlet
{
[Parameter(Mandatory = true)]
public string Path { get; set; }
[Parameter(Mandatory = false)]
[ValidateSet("Present", "Absent", IgnoreCase = true)]
public string Ensure
{
get
{
// set the default to present.
return (this._ensure ?? "Present");
}
set
{
this._ensure = value;
}
}
[Parameter(Mandatory = false)]
public string Content
{
get { return (string.IsNullOrEmpty(this._content) ? "" : this._content); }
set { this._content = value; }
}
private string _ensure;
private string _content;
/// <summary>
/// Return a boolean value which indicates wheather the current machine is in desired state or not.
/// </summary>
protected override void ProcessRecord()
{
if (File.Exists(Path))
{
if( Ensure.Equals("absent", StringComparison.InvariantCultureIgnoreCase))
{
WriteObject(false);
}
else
{
// check if the content matches
string existingContent = null;
using (var stream = new StreamReader(Path))
{
existingContent = stream.ReadToEnd();
}
WriteObject(Content.Equals(existingContent, StringComparison.InvariantCultureIgnoreCase));
}
}
else
{
WriteObject(Ensure.Equals("Absent", StringComparison.InvariantCultureIgnoreCase));
}
}
}
# endregion
}
部署资源
应将已编译的 dll 文件保存在与基于脚本的资源相类似的文件结构中。 下面是此资源的文件夹结构。
$env: psmodulepath (folder)
|- MyDscResources (folder)
|- MyDscResources.psd1 (file, required)
|- DSCResources (folder)
|- MSFT_XDemoFile (folder)
|- MSFT_XDemoFile.psd1 (file, optional)
|- MSFT_XDemoFile.dll (file, required)
|- MSFT_XDemoFile.schema.mof (file, required)