当 PowerShell DSC 位于不同节点时,为什么说我有重复的资源标识符?

Why is PowerShell DSC saying I have a duplicate resource identifier when they are in different nodes?

我正在尝试在 PowerShell DSC 中做一些相对简单的事情。我想确保同一个文件在两台服务器上:

配置。ps1:

Configuration MyConfig {
    # I want this block to be common to both nodes
    Node $AllNodes {
        File DirectoryCopy {
            Ensure = "Present"
            Type = "File"
            Recurse = $true
            SourcePath = ".\example.txt"
            DestinationPath = "%userprofile%\example.txt"
            PsDscRunAsCredential = Get-Credential
        }
    }
}

Agents -ConfigurationData .\data.psd1

data.psd1:

@{
    AllNodes = @(
        @{
            NodeName = "server1"
            Role = "ExampleRole1NotUsedYet"
        },
        @{
            NodeName = "server2"
            Role = "ExampleRole2NotUsedYet"
        }
    )
}

这不起作用,并产生错误:

PSDesiredStateConfiguration\File : A duplicate resource identifier '[File]DirectoryCopy' was found while processing the specification for node 'System.Collections.Hashtable'. Change the name of this resource so that it is unique within the node specification.

我认为我遗漏了一些关于 PowerShell DSC 的基本概念。有没有办法将此文件应用于两个节点?理想情况下,我想在全球范围内应用一些资源,然后仅将一些资源应用到 dev/prod 个系统。

$AllNodes是一个包含[hashtable]的数组,所以直接使用的时候是枚举,然后每个元素(一个[hashtable])引用的时候作为节点名正在转换为字符串,即将显示 class 名称;这就是为什么错误说您的节点被称为 System.Collections.Hashtable 而不是您期望的名称。

由于两个哈希表最终将成为相同的字符串(无论其内容如何),您正在尝试为同一节点创建 2 个具有相同名称的 File 资源,这将不起作用。

你想要的是引用每个哈希表的元素,在本例中是 NodeName:

Configuration MyConfig {
    # I want this block to be common to both nodes
    Node $AllNodes.NodeName {
        File DirectoryCopy {
            Ensure = "Present"
            Type = "File"
            Recurse = $true
            SourcePath = ".\example.txt"
            DestinationPath = "%userprofile%\example.txt"
            PsDscRunAsCredential = Get-Credential
        }
    }
}