Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the File provider in DSC - Ensure Destination only contains files from Source

Tags:

powershell

dsc

I've created a DSC resource to copy a Modules directory from a certain source. I'm testing it for a broader deployment in my environment. The resource does a great job ensuring all the files are there and that they match the source content, so far so good...

The problem is this; I want to ensure that if there are any additional files in the target, or destination, a folder that they get removed.

Here's my code:

Configuration TestRun
{
  Param 
    (
      $ComputerName = 'Localhost'
    )
  Node $ComputerName
    {
      File LoadModules
        {
          Ensure = 'Present'
          Type = 'Directory'
          Force = $true
          Recurse = $true
          SourcePath = "C:\git\Modules"
          DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
          Checksum = "SHA-256"
          MatchSource = $true
        }
    }
}

I've been testing by creating a file in the destination directory after running the config the first time called Deleteme.flag. So far I haven't had any luck getting it actually to be deleted.

I tried adding an additional File provider requirement to remove the directory before it runs:

 File RemoveModules
    {
      Ensure = 'absent'
      Type = 'Directory'
      Force = $true
      Recurse = $true
      DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
    }

Unfortunately, this fails with the following error:

The key properties combination 'C:\users\Jason\Documents\WindowsPowerShell\Modules' is duplicated for keys 'DestinationPath' of resource 'File' in node 'Localhost'. Please make sure key properties are unique for each resource in a node.

Anyway, I'd like to do it with the file resource, but obviously, it would be easy to do it with the script provider or some other custom resource. Thanks in advance for all your help!

like image 641
Jason Morgan Avatar asked Oct 01 '22 00:10

Jason Morgan


1 Answers

I am new to DSC. Spent the best part of Sunday afternoon looking at the resources and trying to figure out how to solve this. So, I sincerely thank you for that. It was fun looking up on DSC.

I think, this could work:

Configuration TestRun
{
  Param 
    (
      $ComputerName = 'Localhost'
    )
    Node $ComputerName
    {
        Script RemoveModules { 
            GetScript = {#needs to return hashtable.}
            SetScript = { 
                $ump = "$HOME" + "\Documents\WindowsPowerShell\Modules\"
                Remove-Item -Path $ump -Recurse -Force
            }
            TestScript = { 
                $ump = "$HOME" + "\Documents\WindowsPowerShell\Modules\"
                $mp = "C:\git\Modules"
                if((Compare-Object $(gci $mp) $(gci $ump))){
                    $false #at least one difference exists, SetScript will be called.
                }else{
                    $true #nothing is different
                }

            }
        }
        File LoadModules
        {
            Ensure = 'Present'
            Type = 'Directory'
            Force = $true
            Recurse = $true
            SourcePath = "C:\git\Modules"
            DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
            DependsOn = "[Script]RemoveModules"
            Checksum = "SHA-256"
            MatchSource = $true
        }
    }
}

References:

  • DSC Script Resource
  • DSC File Resource
  • PowerShellMagazine - Tag DSC
like image 139
sqlchow Avatar answered Oct 05 '22 12:10

sqlchow