【发布时间】:2014-05-14 16:16:04
【问题描述】:
我可以获得一个在非常基本的级别上工作的复合资源(感谢this SO question、this SO question、this MSDN blog 和the DSC e-book)。我遇到的问题是,每当我使用必须使用 Import-DscResource cmdlet 的资源时,复合资源就会停止工作。
我已经阅读了有关复合资源的所有信息,但我无法弄清楚为什么会发生这种情况。这是我要开始工作的资源示例,首先是模块目录结构:
C:\Program Files\WindowsPowerShell\Modules\TestComposite
TestComposite.psd1
DSCResources
TestResource
TestResource.schema.psm1
TestResource.psd1
TestComposite.psd1的内容
@{
ModuleVersion = '1.0'
GUID = '996a9793-dae7-4c25-8fb5-d3fad094d358'
Author = 'Joseph Alcorn'
CompanyName = 'unknown'
Copyright = '(c) 2014 Joseph Alcorn. All rights reserved.'
Description = 'Composite DSC Resource Test'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}
TestResource.psd1的内容
@{
RootModule = 'TestResource.schema.psm1'
ModuleVersion = '1.0'
GUID = '38ca17b0-7d69-4ad5-bb75-fe4de22290d
Author = 'Joseph Alcorn'
CompanyName = 'unknown'
Copyright = '(c) 2014 Joseph Alcorn. All rights reserved.'
Description = 'Composite DSC Resource Test'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}
如果我有TestResource.schema.psm1 的内容是这样的,则复合资源被识别并且一切正常。
Configuration TestResource
{
param
(
[Parameter(Mandatory=$true)]
[string]
$IPAddress
)
File TestFile1
{
DestinationPath = "C:\TestFile.txt";
Contents = $IPAddress
}
}
一旦我将配置更改为此,Get-DscResource 将不再识别它,任何尝试使用它的配置都会出错。
Configuration TestResource
{
param
(
[Parameter(Mandatory=$true)]
[string]
$IPAddress
)
Import-DscResource -ModuleName xNetworking
xIPAddress IPAddress
{
IPAddress = $IPAddress
InterfaceAlias = "Ethernet"
DefaultGateway = "192.168.0.1"
SubnetMask = "255.255.0.0"
AddressFamily = "IPv4"
Ensure = "Present"
}
}
现在,我已安装并可用 DSC 资源工具包第 1-3 波,我可以毫无问题地使用它们,事实上,我使用 xNetworking 资源创建了一个配置,没有任何问题。当TestResource.schema.psm1 文件设置为上述时,系统不再将其视为有效资源(Get-DscResource 不再列出它)。
如果我删除了 Import-DscResource 行,但不考虑其他所有内容,它会识别资源,但资源不可用,因为它不知道在哪里可以找到 xNetworking 模块。我尝试将Import-DscResource -ModuleName xNetworking 放在配置.ps1 中,希望导入会逐渐减少,但仍然没有运气。
我是否遗漏了文档中说明复合资源不能使用Import-DscResource cmdlet 的内容?如果您不能在其中使用其他自定义资源,我看不到复合资源的意义。
【问题讨论】:
标签: powershell dsc