How do I make a PowerShell DSC foreach loop around File resource for copying multiple files defined in the config?
It looks like you never define or change the value of $Number
so each File
resource ends up with the same name. Try something like this.
Configuration MachineToolsFilesAndDirectories{# Copy files on all machinesNode $AllNodes.NodeName{ $Number = 0 foreach ($FileToCopy in $Node.FilesToCopy) { $Number += 1 $thisFile = "$FileToCopy$Number" File $thisFile { Ensure = "Present" Type = "File" Recurse = $false SourcePath = $FileToCopy.SourcePath DestinationPath = $FileToCopy.TargetPath } }}
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).