【问题标题】:How to create a custom DSC resource with MOF如何使用 MOF 创建自定义 DSC 资源
【发布时间】:2016-03-07 16:03:01
【问题描述】:

我正在尝试使用 MOF 创建自定义 DSC 资源,按照此处的说明:https://msdn.microsoft.com/en-us/powershell/dsc/authoringresourcemof。我已经创建了描述的 3 个文件并将它们放在 C:\Program Files\WindowsPowerShell\Modules\MyDscResources\DSCResources\MyCustomResource

Import-DscResource 似乎工作正常,但是当我尝试在节点中使用它时,它说它未定义。

Configuration MyServerConfig
{
    Import-DscResource -ModuleName 'MyCustomResource' # this line succeeds

    Node MyServer
    {
        MyCustomResource MyCustomResource  # this line fails
        {
           Ensure = "Present"
           # ...
        }
    }
}

结果:

Undefined DSC resource 'MyCustomResource'. Use Import-DSCResource to import the resource.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ResourceNotDefined

有什么方法可以获得更多有用的错误信息来找出我的自定义资源出了什么问题?

【问题讨论】:

    标签: powershell dsc


    【解决方案1】:

    解决方案不是尝试手动创建资源,而是使用 xDSCResourceDesigner 生成所有文件,根据本文http://www.powershellmagazine.com/2015/07/02/creating-a-simple-dsc-resource/

    New-Item -Path "$env:ProgramFiles\WindowsPowerShell\Modules" -Name MyDSCResources -ItemType Directory
    New-ModuleManifest -Path "$env:ProgramFiles\WindowsPowerShell\Modules\MyDSCResources\MyDSCResources.psd1" -Guid (([guid]::NewGuid()).Guid) -Author 'Me' -ModuleVersion 1.0 -Description 'My DSC Resource Module' -PowerShellVersion 4.0 -FunctionsToExport '*.TargetResource'
    
    $Name = New-xDscResourceProperty -Type String -Name Name -Attribute Key
    $Ensure = New-xDscResourceProperty -Name Ensure -Type String -Attribute Write -ValidateSet "Present", "Absent"
    #... more properties
    
    New-xDscResource -Name MyCustomResource -Property $Name,$Ensure -Path "$env:ProgramFiles\WindowsPowerShell\Modules\MyDSCResources" -ClassVersion 1.0 -FriendlyName MyCustomResource –Force
    

    似乎有很多繁琐的设置需要正确设置,并且没有足够的错误消息来指导您,因此如果您是 DSC 新手,则不值得手动操作。

    感谢@Nana 将我推向正确的方向。

    【讨论】:

      【解决方案2】:

      MyCustomResource 是资源的名称。您需要指定模块的名称 - 在您的情况下为 MyDscResources。所以 Import-DscResource 语句应该是这样的

      导入-DscResource -ModuleName MyDscResources

      【讨论】:

      • 谢谢,我试过了,但在导入行失败:+ Import-DscResource -ModuleName 'MyDscResources' Could not find the module 'MyDscResources'.
      • 在 c:\program files\WindowsPowerShell\Modules\MyDscresources 中是否有可用的 MyDscResources.psd1?如果没有,你能确保有一个。您可以使用 New-moduleManifest cmdlet 生成一个
      • 我刚刚尝试过。 Import-DscResource -ModuleName 'MyDscResources' 现在成功了,但我又回到了原来的问题 Undefined DSC resource 'MyCustomResource'. Use Import-DSCResource to import the resource.
      【解决方案3】:

      您应该在 C:\Program Files\WindowsPowerShell\Modules\MyDscResources\DSCResources\MyCustomResource 文件夹下拥有 MyCustomResource.schema.mof 和 MyCustomResource.psm1 文件。如果“Get-DscResource -module MyDscResources”未显示此资源,则说明该资源有问题。在这种情况下,您能否从 MyCustomResource.psm1 共享 MyCustomResource.schema.mof 的内容以及 Get-TargetResource、Test-TargetResource 和 Set-TargetResource 函数的签名。

      【讨论】:

      • 谢谢 - 我确实将 schema.mof 和 psm1 文件放在了正确的位置,但我预计清单有问题。改为使用 xDSCResourceDesigner 解决。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多