PowerShell DSC – Copy Folder, Recursively

email me

Show more about DSC...

 

Using a DSC script, copy contents of a source to a destination folder, start configuration, and view configuration.

The basic processing works like this:

make a declarative script > generate mof > apply mof > verify configuration is running


Code

# create configuration

Set-Location "C:\DSC\"

Configuration CopyFolderExample
{
Node "localhost"
{
File DirectoryCopy
{
Ensure = "Present" # Present, Absent.
Type = "Directory" # File, Directory
Recurse = $true # $true, $false
SourcePath = "c:\DSC\source"
DestinationPath = "C:\DSC\destination"
}

}
}

# create mof
CopyFolderExample

# load configuration from mof
Start-DscConfiguration -Path CopyFolderExample -ComputerName localhost -Wait -Verbose -Force

# check configuration
Get-DscConfiguration

 


Notes

Install-Module -Name PSDesiredStateConfiguration
winrm quickconfig

Desired State Configuration Overview for Engineers

PSDesiredStateConfiguration

Get-DscConfiguration

DSC mofs C:\Windows\System32\Configuration

 

Contents of the mof file

/*
@TargetNode='localhost'
@GeneratedBy=Homelab
@GenerationDate=04/16/2019 07:40:11
@GenerationHost=HOMELAB
*/

instance of MSFT_FileDirectoryConfiguration as $MSFT_FileDirectoryConfiguration1ref
{
ResourceID = "[File]DirectoryCopy";
Type = "Directory";
Ensure = "Present";
DestinationPath = "C:\\DSC\\destination";
ModuleName = "PSDesiredStateConfiguration";
SourceInfo = "C:\\DSC\\FolderExample.ps1::9::9::File";
Recurse = True;
SourcePath = "c:\\DSC\\source";

ModuleVersion = "1.0";

ConfigurationName = "CopyFolderExample";

};
instance of OMI_ConfigurationDocument

{
Version="2.0.0";

MinimumCompatibleVersion = "1.0.0";

CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};

Author="Homelab";

GenerationDate="04/16/2019 07:40:11";

GenerationHost="HOMELAB";

Name="CopyFolderExample";

};