computer files

Use the Windows PowerShell DSC File Provider to Include Updated Folder Content

Q: I'm using the File provider as part of a Windows PowerShell Desired State Configuration. Only the files in the source file at the time of the configuration are applied—no new files. How can I fix this?

A: Several providers are available as part of the Windows PowerShell Desired State Configuration (DSC). One of them is the File provider, which you can use to specify a source file or a folder that is then copied to the target folder on the target server. By default, only the files present in the source folder at the time the configuration is applied are copied to the target folder during the state checks. Therefore, if new files are added to the source, they aren't copied to the target. The File resource is specifically designed to perform a 'diff' between the source and destination; to optimize the process, the consistency engine copies only files that have changed. You can modify this behavior by adding the MatchSource = $true property to the configuration, which tells the consistency engine that an exact match is required between folders. The following code contains an example of this approach:

Configuration MyTestFileConfig
{
    # A Configuration block can have zero or more Node blocks
    Node "SAVDALHV01"
    {
        # Resource Block

        # File is a built-in resource you can use to manage files and directories
        # This example ensures files from the source directory are present in the destination directory
        File MyFileExample
        {
            Ensure = "Present"  # You can also set Ensure to "Absent"
            Type = "Directory" # Default is "File" but this tells it to look at entire folder
            Recurse = $true # Check all subfolders and content
            MatchSource = $true # Check for new files as part of the source
            SourcePath = "D:\Temp\Source" # This is a path that has source files
            DestinationPath = "D:\Temp\Target" # The path where we want to ensure the files are present
        }

    }
} 

Once this code executes and is applied, any new files added to the source will be copied to the target at the refresh interval. For example:

MyTestFileConfig # Creates the MOF file to be used by DSC

Start-DscConfiguration -Wait -Verbose -Path .\MyTestFileConfig # Applies the configuration

Get-DscConfiguration # Checks the current applied configuration
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish