Share via


PowerShell 5.0 Desired State Configuration - File Resource Demo

PowerShell 5.0 Desired State Configuration - File Resource Demo

In this Wiki let's explore File Resource Demo

Explore Resource

Get-DSCResource

Syntax

 Get-DscResource -Name File -Syntax

Syntax command outputs the below

File [string] #ResourceName
{
    DestinationPath = [string]
    [ Attributes = [string[]] { Archive | Hidden | ReadOnly | System }  ]
    [ Checksum = [string] { CreatedDate | ModifiedDate | SHA-1 | SHA-256 | SHA-512 }  ]
    [ Contents = [string] ]
    [ Credential = [PSCredential] ]
    [ DependsOn = [string[]] ]
    [ Ensure = [string] { Absent | Present }  ]
    [ Force = [bool] ]
    [ MatchSource = [bool] ]
    [ Recurse = [bool] ]
    [ SourcePath = [string] ]
    [ Type = [string] { Directory | File }  ]
}

Structure of File Resource

  1. File is Resource we consumed here for Demo.
  2. Name your resource as required. Ensure to adapt organization naming convention or any standards.
  3. Set properties as applicable.

Create File

The below Code is to create a file in C:\Temp as DSC>txt with Content 'File Created Using - Desired State Configuration'

Configuration MyFile
{
    Node $env:ComputerName
    {
        File MyFile
        {
            DestinationPath = 'C:\Temp\DSC.txt'
            Contents = 'File Created Using - Desired State Configuration'
            Ensure = 'Present'
        }
    }
}
MyFile
Start-DscConfiguration .\MyFile -Verbose -wait -force

MOF Structure

/*
@TargetNode='DSC'
@GeneratedBy=ChenV
@GenerationDate=01/09/2015 21:26:27
@GenerationHost=DSC
*/

instance of MSFT_FileDirectoryConfiguration as $MSFT_FileDirectoryConfiguration1ref
{
ResourceID = "[File]MyFile";
 Ensure = "Present";
 Contents = "Desired State Configuration";
 DestinationPath = "C:\\Temp\\DSC.txt";
 ModuleName = "PSDesiredStateConfiguration";
 SourceInfo = "::5::9::File";
 ModuleVersion = "1.0";

 ConfigurationName = "MyFile";

};
instance of OMI_ConfigurationDocument
{
 Version="2.0.0";
 MinimumCompatibleVersion = "1.0.0";
 CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};
 Author="ChenV";
 GenerationDate="01/09/2015 21:26:27";
 GenerationHost="DSC";
 Name="MyFile";
};