PowerShell DSC locally: basic example

If you are new to the principle of Desired State Configuration (DSC), its Big Idea is to take a set of declarative instructions (basically configuration as code), and if the requested thing is not in the required state, “make it so”. If it is in the required state, do nothing. This article uses the simple example of folder creation to demonstrate that. Everything happens locally, which is not a typical use-case, but is useful for focussing on learning and basic syntax, rather than also getting your head around remoting.

Sometimes rather than struggle with remote authentication, I am happy just to push a bunch of scripts to a new VM, and do everything locally. You still need WinRM running.

For the example, I am creating a folder. (Of course it is very easy to create a folder in “normal” PowerShell or from the command line or any number of ways. But this is precisely about using the very simplest example to illustrate a point.) The push server and the receiving client are the same thing. This example can be executed on a Windows 7 client (providing it has the right level of PowerShell for DSC*) or Windows 10 client, or Windows 2012 R2 server and up. *The minimum version of PowerShell is 4.0. You can check this by running $psversiontable in Powershell.

On the first execution, the folder does not exist, so you get this…

The system cannot find the file specified.
The related file/directory is: c:\temp\TheDemo01.

On the second execution, the folder does exist, so you get this…

The destination object was found and no action is required.

Both of these were expected, as you might hope with something called Desired State Configuration.

gist.

Pics:

 

 

DSC_2_5

Advertisements

DSC: What is the purpose of the Script Resource?

… given that it does not seem to work as advertised. Only the TestScript block seems to have any impact, GetScript and SetScript do nothing, as far as I can see.

https://msdn.microsoft.com/en-us/powershell/dsc/scriptresource

 

PowerShell: basic Desired State Configuration (DSC)

This shows a basic config…

, which I have adapted for my Windows 10 box. Note ref Kerberos etc objections, you will also need to look at this.

Some history from the PowerShell session lest I forget:

Get-DscResource service -Syntax
Get-Item -Path WSMan:\localhost\Client\TrustedHosts
$cred = Get-Credential
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value ‘DEN-I7’
$sess = New-PSSession -ComputerName DEN-I7
$sess = New-PSSession -ComputerName DEN-I7 -Credential $cred
C:\temp\BasicInstall.ps1

, where last line is this:

dsc01

Configuration BasicDscConfig
{
Node “DEN-I7” {
WindowsFeature NetFramework35Core {
Name = “NET-Framework-Core”
Ensure = “Present”
}

WindowsFeature NetFramework45Core {
Name = “NET-Framework-45-Core”
Ensure = “Present”
}

WindowsFeature ASP {
Ensure = “Present”
Name = “Web-Asp-Net45”
}
}
}

BasicDscConfig -InstanceName “DEN-I7”
Start-DscConfiguration -Path .\BasicDscConfig -Wait -Verbose -Force

#http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx

dsc02

Following a reboot after unsetting .Net 3.5 and 4.6 (sic), I tried again, and this time go this:

dsc03

So because this is a client-type machine, it won’t accept these particular Features from DSC.

Let’s see what happens if I more modestly just try to create a folder (I’ll delete the MOF files first)

Configuration BasicDscConfig
{
Node “DEN-I7” {
<# WindowsFeature NetFramework35Core { Name = “NET-Framework-Core” Ensure = “Present” } WindowsFeature NetFramework45Core { Name = “NET-Framework-45-Core” Ensure = “Present” } WindowsFeature ASP { Ensure = “Present” Name = “Web-Asp-Net45” } #>
File MyRandomDir {
DestinationPath = “c:\temp3\andMore”
Type = “Directory”
Recurse = $false
}
}
}

BasicDscConfig -InstanceName “DEN-I7”
Start-DscConfiguration -Path .\BasicDscConfig -Wait -Verbose -Force

#http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx
#https://technet.microsoft.com/en-us/library/dn282129.aspx

1

dsc04

2

dsc05

3

dsc06

4