File resource in for loop for dynamic sources

Welcome Forums DSC (Desired State Configuration) File resource in for loop for dynamic sources

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
2 years, 3 months ago.

  • Author
    Posts
  • #55465

    Participant
    Points: 0
    Rank: Member

    I have been trying to use File resource inside foreach loop for multiple sourcepath and single destination but it errors out stating that:

    Resources have identical key properties but there are differences in the following non-key properties: 'SourcePath'. Values '\\IM' don't match values '\\PowerShellGallery'. Please update these property values so that they are identical in both cases.

    Here is my POSH:

    foreach ($folder in Get-ChildItem -Recurse -Path "\\$ModulesDirectory" -Depth 0 -Directory)
            {
                $folderName = $folder.Name
                $ctr++
                File "DuplicateModulesFolder$ctr"
                {
                    DestinationPath = "C:\Temp\1"
                    Checksum        = "ModifiedDate"
                    Ensure          = "Present"
                    Recurse         = $true
                    SourcePath      = "$ModulesDirectory\$folderName"
                    Type            = "Directory"
                    MatchSource     = $true
                    Credential = $runAsUserCred
                }
            }

    In this sample, $ModulesDirectory is UNC containing 2 nested folders IM and PowerShellGallery. I want to pull down files from UNC path into local drive. In future there may be multiple folders under $ModulesDirectory root dir.

    Thanks for your help!

  • #55472

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    Every resource defines specific properties that are its "keys," and each instance of that resource must have a unique key. I'm not in front of a computer to check, but I believe for the File resource it's DestinationPath. You can't have multiple blocks with the same DestinationPath.

The topic ‘File resource in for loop for dynamic sources’ is closed to new replies.