gpt4 book ai didi

powershell - 如何使用MOF创建自定义DSC资源

转载 作者:行者123 更新时间:2023-12-03 00:48:52 24 4
gpt4 key购买 nike

我正在尝试使用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

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

最佳答案

解决方案不是尝试手动创建资源,而是使用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将我推向正确的方向。

关于powershell - 如何使用MOF创建自定义DSC资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35848371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com