Search This Blog

Friday, 28 July 2017

Create File using DSC PowerShell Script - Part III

PowerShell DSC is one of my favorite topics to teach, because the technology is simply amazing. Usually I do not have enough time with a customer to teach all of the built-in resources. I would guess that the Script resource is one of the least understood.This sample has tested on PSVersion 4.0.

Step 1. Script Resource Log File Example

Here is an elegantly simple use of the Script resource. We have send Node (Server) Name and file contents as parameter to DSC Script. The code is its own documentation. Study it carefully.

configuration WinLogAutoGen
{ 
    param(
    
        [parameter(Mandatory=$true)]
        [string]    $Server,

        [parameter(Mandatory=$true)]
        [string]    $Contents
    )

    #import the module of DSC for using DSCResources.
    Import-DscResource -ModuleName PSDesiredStateConfiguration;

    node $Server
    {
        # use the file resource to create file.
        File windowsupdatelog{
            DestinationPath  = 'C:\windows\DynamicParameterDemo.log';
            Ensure = 'Present';
            Contents = $Contents ; 
        }
    }
 
}

#Create mof file
WinLogAutoGen -OutputPath D:\dsc-mof


#Apply mof File
Start-DscConfiguration -Wait -Verbose -Force -Path D:\dsc-mof

Notice the Write-Verbose statements. These are important for logging and output when the resource runs.

Step 2. Output



Step 3. Verify Output

Step for Verification.
  1. Connect to Server.
  2. Verify the content of DynamicParameterDemo.log file. PowerShell Command as below

Get-Content C:\Windows\DynamicParameterDemo.log

Note: We can verify the mof file content as well, PowerShell Command as below

Get-Content D:\dsc-mof\localhost.mof
IMP : Target nodes(Server) can apply only one MOF file.

Reference

Your Turn

Now it is your turn. Take this sample and implement your own script resources. Leave a comment below telling what you accomplished.

Best wishes on your adventure learning DSC!

No comments:

Post a Comment

Elasticsearch - Nodes, clusters, and shards

Elastic Stack Video - Load your gun in short time.   Beginner's Crash Course to Ela...

Recent Post