Day 27: Managing Linux Scripts using PowerShell DSC

Following up to last week’s article, Day 22: Managing Linux Users and Groups using PowerShell DSC, this week we’ll be demonstrating how to manage Linux Scripts in Ubuntu and CentOS using PowerShell DSC. Included in this week’s post, is a method of getting around the limitations of setting a User’s password when using the Users and Groups DSC Resources for Linux.

Introduction to the MSFT_nxScriptResource Class

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

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\nx\DSCResources\MSFT_nxScriptResource
The contents of the MOF file are below:

Below is a quick breakdown of what’s important:

— We need to provide the contents of the Script in the SetScript field.
— The TestScript field must be invoked.
— The GetScript field must be invoked.

You can provide a different value besides root for the User and Group value to run the Script; however, this is probably not something you’ll use very often as you are managing the Linux Host.

Additional information on using the TestScript field can be found on the current documentation on GitHub.

Expected Behavior when using the Scripting Resource

Previously when we were Managing Linux Files using PowerShell DSC, you might remember that there were significant problems when trying to include the contents of a File in the DSC Configuration. Interestingly enough, when managing Scripts you can format how they look within the PowerShell DSC Configuration without the same problems.

For Example, the following definition below will work:

 

Where things get interesting is in regards to the Linux Host MOF file that is created. When you view the ScriptResource in this file, you’ll notice that the SetScript definition will be incredibly long single line and mostly unreadable. The SetScript value of the example above will look something like this:

SetScript = “#!/bin/bash\n #\n #\n #\n PROCESS_COUNT=$(ps -aef | wc -l)\n echo \”Number of Process Currently Running: $PROCESS_COUNT\” > /tmp/RunningProcessCount_date +%m.%d.%Y-%H:%M:%S.txt”;

 

This does not affect the capability of the script to run, but it’s something you may want to be aware of if you end up having to manually edit or manage your Linux Host MOF files.

Lastly, when you the Script is run on the Linux Host, the Script will be named temp_script.sh and will be run from the /tmp directory within another directory named tmp<six_random_letters_and_numbers>. So the entire path to the script can look something like this:

/tmp/tmpY6Y3ew/temp_script.sh

If the script runs successfully, the temp_script.sh file will be empty.

 

Additional Requirements for this week’s Examples

In order for the test Scripts in this week’s blog post to work, the expect application must be installed on your Linux Hosts.

To install on Ubuntu, run the following command as root

apt-get install expect

 

To install expect on CentOS, run the following command as root

yum install expect

 

Sample Script for managing Linux Scripts in Ubuntu 12.04

Below is a sample Script that will reset the password of the scom-svc-ubuntu user that was created in Day 22: Managing Linux Users and Groups using PowerShell DSC. It should go without saying that storing credentials in clear text for Production purposes is a really bad idea; but just in case, the password here is in clear text for demonstration purposes only!

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 Scripts in CentOS 6.2

Below is a sample Script that will reset the password of the scom-svc-centos user that was created in Day 22: Managing Linux Users and Groups using PowerShell DSC. It should go without saying that storing credentials in clear text for Production purposes is a really bad idea; but just in case, the password here is in clear text for demonstration purposes only!

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

This week’s post has demonstrated how to manage Linux Scripts using PowerShell and given an example of how to get around the limitations of the Users and Groups DSC Resources for Linux.

 

Previous Installments

100 Days of DevOps with PowerShell

 

 

Leave a Reply

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