I’ve hacked up a bit of code I’ve been using inside a composite resource.
Basically, using the ConfigurationData parameter of the DSC configuration object, we can pass an object containing all of our config to the internal resources.
So, here is our configuration.
Configuration MyConfiguration {
Node "Localhost" {
ForEach ($Folder in $Node.FolderStructure) {
# Each of our 'file' resources will be named after the path, but...
# we have to replace : with __ as colons aren't allowed in resource names
File $Folder.Path.Replace(':','__') {
DestinationPath = $Folder.Path
Ensure = $Folder.Ensure
}
} # ForEach
} # Node "Localhost"
} # configuration MyConfiguration
Now, our configuration data variable.
$ConfigurationData =
@{
AllNodes = @(
@{
NodeName = "localhost"
FolderStructure = @(
@{
Path = "D:\Management\Packages"
Ensure = "Present"
}
@{
Path = "D:\Management\Wallpaper"
Ensure = "Present"
}
) #FolderStruture = @(...
} # localhost
) # AllNodes = @(...
}
As you can see, the configuration contains a File resource that uses the information in the ConfigurationData variable. It loops through ‘FolderStructure’ in $ConfigurationData, so if we need to add any more, we just add them there. You don’t have to do it exactly like this, but this allows you to add more folders without having to repeat a lot of code (Plus, you can keep your ConfigurationData variable separate to the rest of the actual DSC configuration gubbins).
Also, another thing you can do is - And I don’t know if this is really the proper way to do it - use the Recurse option in the File resource to automatically create all folders in the path you supply. Its real use is to copy full file paths, but if you specify an empty folder as the ‘SourcePath’, it will create each folder in turn. For example…
File MyFile {
DestinationPath = 'D:\A\Really\Long\File\Path'
SourcePath = 'C:\Windows\Temp\ABlankFolder'
Recurse = $true
Ensure = 'Present'
}
It’s probably best to add some logic to create ‘C:\Windows\Temp\ABlankFolder’ before you use it though.