Day 17: Managing Linux Files using PowerShell DSC

Following in the same vein as last week’s article: Day 12: Managing Linux Services using PowerShell DSC, this week we’ll be discussing how to manage Linux Files in both Ubuntu and CentOS using PowerShell DSC.

Introduction to the MSFT_nxFileResource Class

By default, the MOF file, MSFT_nxFileResource.schema.mof, is installed in the following path when you install the nx-PSModule:

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\nx\DSCResources\MSFT_nxFileResource

The contents of the MOF file are below:

Below is a quick breakdown of the most important aspects of the file:

— We need to provide the path where the File or Directory will reside

— We need to provide the Source Path of the File (this is only required if the Contents Value isn’t used)

— We need to provide the Contents of the File (this has some limitations which we’ll cover.)

— We need to provide the Group the file will belong too.

— We need to provide the Mode (Permissions) of the file in octal or symbolic notation

— We need to provide the owner of the file.

 

Limitations of the Contents Field

As you may have noticed earlier, the Contents field is interpreted as a String as stated in the MOF File. In reality, certain special characters are interpreted by this field if you include them and do not escape them using the grave-accent (`).

While this might not seem like a major issue, the majority of configuration files used by Linux based applications will more than likely use one or more of the following special characters:

An example of where this can become a real problem, is in regards to the standard sshd script in the /etc/init.d/ directory in CentOS.

Below are the first 55 lines of the Script:

 

In order to get these 55 lines to show up correctly when pushing out the configuration file to the CentOS Host, the following lines have to have one or more characters escaped:

Just in those 2 Lines of Code, I had to escape 14 characters in order for the first 55 lines of the sshd script to be successfully pushed out to the CentOS Host. In order to give you the magnitude of the scope of work that may be required, the entire script is 234 lines.

The Final Resource Definition in PowerShell is shown below. Something else that should be noted here, I had to copy and paste the code directly before escaping characters so that I would not mess up the formatting of the script. If you try and clean up the way the code looks in the PowerShell Configuration, you will mess up the formatting when it is on the Linux host and potentially break the script.

Using the SourcePath Field

An alternative to using the Contents Field is to use the SourcePath Field. The upside to utilizing this Field is that you don’t have to worry about escaping characters in your bash scripts or configuration files before adding them to your DSC Configuration. The downside is that you have to provide a path that is local to the Linux Host that you are targeting. As many of you are astute readers, you can immediately understand how this is in many ways self-defeating.

In order to utilize the SourcePath Field, you must provide the full path to the Directory or File that you want to copy from the Linux Host that you are working with. As of this writing, I have not found a way to utilize the SourcePath to reference a File or Directory that exists on a Network Share, Website or Windows Network Path. An example is given in the Sample Script for managing Linux Files using SourcePath (Ubuntu) section.

 

Sample Script for Managing Linux Files using SourcePath (Ubuntu)

Below is a sample Script that copies the ssh script in /etc/init.d to the /tmp directory and names the file ssh_script. Note that this will clobber the file if it already exists in the /tmp directory.

To use this script, first create the following directory on your DSC Scripting Host: C:\LinuxConfigs. Next, replace the value <COMPUTERNAME> in the $LinuxServer variable next to the -ComputerName: switch, with the name of your Ubuntu Host.

 

 

Sample Script for Managing Linux Files using Contents (CentOS)

Below is a sample Script that adds the first 55 lines of the sshd script in CentOS to a new file in the /tmp directory called sshd_script_sample. Note that this will clobber the file if it already exists in the /tmp directory.

To use this script, first create the following directory on your DSC Scripting Host: C:\LinuxConfigs. Next, replace the value <COMPUTERNAME> in the $LinuxServer variable next to the -ComputerName: switch, with the name of your CentOS Host.

 

Conclusion

I hope this article clarifies how to configure and manage Linux Files in Ubuntu and CentOS using PowerShell DSC.

Next week we will be going over how to manage Linux Users and Groups using PowerShell DSC.

 

Previous Installments

One thought on “Day 17: Managing Linux Files using PowerShell DSC

  1. Kristoher Bash

    This is a great write-up!   Rather than dealing with escaping all of those characters, why not declare the Contents as a multi-line string literal?  Such as:

    Contents = @’
    #!/bin/bash


    @’

    Alternatively, you could put the contents in a text file and do use Get-Content to read it in to a variable:
    Get-Content “D:\Contents\MyFileContents.txt” |out-string

     

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.