POC: PowerShell Desired State Configuration for Linux

Since PowerShell went open source, I’ve been asked a lot about running PowerShell on Linux. Lately, I’ve been asked more and more about configuring Linux machines with DSC (Desired State Configuration).

So here goes a quick and dirty How-to download, install, and use DSC to configure your Linux machines

(In my case, I used Ubuntu Server 16.04 LTS).

Step 1: Install PowerShell

Install PowerShell via the Package Repository:

# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

# Register the Microsoft Ubuntu repository
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list

# Update the list of products
sudo apt-get update

# Install PowerShell
sudo apt-get install -y powershell

Or Direct download and install:

wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-beta.8/powershell_6.0.0-beta.8-1.ubuntu.16.04_amd64.deb
sudo dpkg -i ./powershell_6.0.0-beta.8-1.ubuntu.16.04_amd64.deb
sudo apt-get install -f

Other PowerShell Core packages can be found at:

Step 2: Install OMI and DSC for Linux

# Download and install the OMI (Open Management Infrastructure) package:
wget https://github.com/Microsoft/omi/releases/download/v1.1.0-0/omi-1.1.0.ssl_100.x64.deb
sudo dpkg -i ./omi-1.1.0.ssl_100.x64.deb
# (Other OMI packages can be found at: https://github.com/Microsoft/omi/releases)
# More information on OMI can be found at: https://github.com/Microsoft/omi

# Download and install the DSC for Linux package:
wget https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases/download/v1.1.1-294/dsc-1.1.1-294.ssl_100.x64.deb
sudo dpkg -i ./dsc-1.1.1-294.ssl_100.x64.deb
# (Other packages can be found at: https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases)

# Restart OMI:
sudo /opt/omi/bin/service_control restart

# To check OMI:
sudo /opt/omi/bin/omicli ei root/omi OMI_Identify

Step 3: Use DSC for Linux

# Run PowerShell and create a configuration:
sudo powershell

Then, In the PowerShell session

# Download and install the nx Module:
Install-Module nx

# Create a configuration:
Configuration myDscExample {
    Import-DSCResource -Module PSDesiredStateConfiguration, nx
    node localhost {
        nxFile myTestFile {
            Ensure          = "Present"
            Type            = "File"
            DestinationPath = "/tmp/helloFromDsc"
            Contents        = "Hello DSC for Linux!"

# Run the configuration to generate the MOF file:

# Return to the shell:

Back in bash:

# Test to see that the file is not there (yet):
ls /tmp/helloFromDsc

# Start the DSC configuration to apply the previously created MOF:
sudo /opt/microsoft/dsc/Scripts/StartDscConfiguration.py -configurationmof /home/vmadmin/myDscExample/localhost.mof

# Check that the file is there now:
ls /tmp/helloFromDsc

# and that it contains the expected content:
cat /tmp/helloFromDsc

Other “cmdlets” (python scripts) you’ll find useful:

# List the default DSCResources:
ls /opt/microsoft/dsc/modules/nx/DSCResources/

# List the ("DSC cmdlets") scripts:
ls /opt/microsoft/dsc/Scripts

# Get the LCM settings:
sudo /opt/microsoft/dsc/Scripts/GetDscLocalConfigurationManager.py

# Get the current applied configuration:
sudo /opt/microsoft/dsc/Scripts/GetDscConfiguration.py

If you’ll want to configure the Linux machine using WSMAN, you’ll need to open the port (from bash):

firewall-cmd --add-port=5986/tcp --permanent
firewall-cmd --reload

And then, even from a Windows machine, in PowerShell:

$node = 'u1604.contoso.com'
$cred = Get-Credential -UserName root
$opts = New-CimSessionOption -UseSsl -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$sess = New-CimSession -ComputerName $node -Credential $cred –Port 5986 –Authentication Basic –SessionOption $opts
Start-DscConfiguration -Path C:TempmyDscExample -CimSession $sess -Wait -Verbose

For further reading, see:

Built-In Desired State Configuration Resources for Linux: https://docs.microsoft.com/en-us/powershell/dsc/lnxbuiltinresources

Get started with Desired State Configuration (DSC) for Linux: https://docs.microsoft.com/en-us/powershell/dsc/lnxgettingstarted

PowerShell-DSC-for-Linux on GitHub: https://github.com/Microsoft/PowerShell-DSC-for-Linux