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

Capture updated content in a folder with the File provider in Windows PowerShell Desired State Configuration (DSC).

John Savill

April 12, 2014

2 Min Read
computer files

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:TempSource" # This is a path that has source files            DestinationPath = "D:TempTarget" # 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 DSCStart-DscConfiguration -Wait -Verbose -Path .MyTestFileConfig # Applies the configurationGet-DscConfiguration # Checks the current applied configuration

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like