PowerShell DSC – File Example

Show more about DSC...

 

Using a DSC script, create a file in 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

Set-Location "C:\temp\"

# define configuration

configuration FileExample
{
Import-DscResource -ModuleName PSDesiredStateConfiguration

Node localhost
{

File FileExample
{
Ensure           = 'Present' # Present, Absent
Contents         = 'Config settings in file'
DestinationPath  = 'C:\temp\FileExample.txt'
Type             = 'File'

}
}
}

# create mof
FileExample

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

# check configuration
Get-DscConfiguration

 

Notes

Install-Module -Name PSDesiredStateConfiguration

winrm quickconfig

see PowerShell DSC – Registry Example

DSC mofs C:\Windows\System32\Configuration

 

Contents of the mof file

/*
@TargetNode='localhost'
@GeneratedBy=Homelab
@GenerationDate=04/15/2019 21:46:55
@GenerationHost=HOMELAB
*/

instance of MSFT_RegistryResource as $MSFT_RegistryResource1ref
{
ResourceID = "[Registry]RegistryExample";
ValueName = "ValueHere";
Key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\ExampleKey";
Ensure = "Present";
SourceInfo = "C:\\temp\\reg_example.ps1::8::2::Registry";
ValueType = "String";
ModuleName = "PSDesiredStateConfiguration";
ValueData = {
"DataHere"
};

ModuleVersion = "1.0";

ConfigurationName = "RegistryExample";

};
instance of OMI_ConfigurationDocument

{
Version="2.0.0";

MinimumCompatibleVersion = "1.0.0";

CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};

Author="Homelab";

GenerationDate="04/15/2019 21:46:55";

GenerationHost="HOMELAB";

Name="RegistryExample";

};