7

PowerShell DSC を使用して複数のファイル コピーを実行しようとしています。私の構成には、コピーする必要があるソース/ターゲット ファイルのリストがあります。ただし、リソースへの依存関係を作成できるように、File リソースには一意の名前が必要です。

私は PowerShell を初めて使用し、ファイル リソースの foreach を許可する DSC スクリプト (.ps1) の適切な形式を見つけようとしています。現在、ファイル リソースが一意の名前を取得していないように見えるため、私のコードでは "Duplicate Resource Identifier" エラーが表示されます。

構成 (psd1 ファイル):

{
AllNodes = @(
@{
  NodeName = '*'
  BuildOutputRoot = 'C:\_BuildDrop\'
  FilesToCopy = @(
    @{
      SourcePath = 'C:\_BuildDrop\SampleConfig.xml'
      TargetPath = 'C:\SampleCode\SampleConfig.xml'
    },
    @{
      SourcePath = 'C:\_BuildDrop\SampleConfig2.xml'
      TargetPath = 'C:\SampleCode\SampleConfig2.xml'
    },

DSC 用の Powershell ps1 ファイル (スニペット):

Configuration MachineToolsFilesAndDirectories
{
# Copy files on all machines
Node $AllNodes.NodeName
{
    foreach ($FileToCopy in $Node.FilesToCopy)
    {
        File $FileToCopy$Number
        {
            Ensure = "Present"
            Type = "File"
            Recurse = $false
            SourcePath = $FileToCopy.SourcePath
            DestinationPath = $FileToCopy.TargetPath
        }
    }
4

2 に答える 2

1

I'm not sure if it's what everyone does, but I alway name my resources after the key value in the resource so in the MOF each resource is obviously named after what it does. The only thing to remember is that you have to sanitise the resource name as only alphanumeric and a few other characters are allowed (Specifically not colons in the case for file paths).

For example:

File $FileToCopy.TargetPath.Replace(':','\')
{
    Ensure = "Present"
    Type = "File"
    Recurse = $false
    SourcePath = $FileToCopy.SourcePath
    DestinationPath = $FileToCopy.TargetPath
}

Which would equate to:

File 'C\\SampleCode\SampleConfig.xml'
{
    Ensure = "Present"
    Type = "File"
    Recurse = $false
    SourcePath = 'C:\_BuildDrop\SampleConfig.xml'
    DestinationPath = 'C:\SampleCode\SampleConfig.xml'
}

If it was populated from the following:

@{
  SourcePath = 'C:\_BuildDrop\SampleConfig.xml'
  TargetPath = 'C:\SampleCode\SampleConfig.xml'
}

I realise using the .Replace method is a bit of a sucky way to do it - I should probably build a regex to catch all of the possibilities I occur (Which has been $ for shares and colons in file paths so far).

于 2016-01-10T15:21:49.650 に答える