PowerShell Desired State Configuration (DSC) Journey – Day 23

After yesterday’s shenanigans I am ready to move on to the next step.  Here is my current Configuration.

$ConfigData = @{
    AllNodes = @(
                @{
                    NodeName = "localhost"
                    PSDscAllowPlainTextPassword = $True
                    }
                )
}

Configuration DSCTemplate
{

    Param
    (

        [pscredential]$Credential
    )

    Import-DscResource -Module xNetworking
    Import-DscResource -Module xComputerManagement

    Node $AllNodes.NodeName
    {

        xIPAddress IPAddress
        {
            InterfaceAlias = "Ethernet"
            IPAddress = "100.198.100.152"
            AddressFamily = "IPv4"
            DefaultGateway = "100.198.100.1"
            SubnetMask = 24
        }

        xComputer ComputerConfiguration
        {
            Name = "DSCTemplate"
            DomainName = "domain.com"
            Credential = $Credential
            DependsOn = "[xIPAddress]IPAddress"
        }

    }
}

DSCTemplate -ConfigurationData $ConfigData -Credential (Get-Credential)

Now, following along with this article I am currently testing Option 1 – Inject a DSC Configuration (.ps1) into a VHD.  A couple of things from that example that I need to add to my Configuration.  It starts with Powershell -c .  I have no idea what this does and can’t find any information on it but I will include it anyways.  If I run that itself in PowerShell I just get the help syntax.  And I need to add the line to start the DSC Configuration.  Here is the updated Configuration.

Powershell -c{
$ConfigData = @{
    AllNodes = @(
                @{
                    NodeName = "localhost"
                    PSDscAllowPlainTextPassword = $True
                    }
                )
}

Configuration DSCTemplate
{

    Param
    (

        [pscredential]$Credential
    )

    Import-DscResource -Module xNetworking
    Import-DscResource -Module xComputerManagement

    Node $AllNodes.NodeName
    {

        xIPAddress IPAddress
        {
            InterfaceAlias = "Ethernet"
            IPAddress = "100.198.100.152"
            AddressFamily = "IPv4"
            DefaultGateway = "100.198.100.1"
            SubnetMask = 24
        }

        xComputer ComputerConfiguration
        {
            Name = "DSCTemplate"
            DomainName = "domain.com"
            Credential = $Credential
            DependsOn = "[xIPAddress]IPAddress"
        }

    }
}

DSCTemplate -ConfigurationData $ConfigData -Credential (Get-Credential)

Start-DSCConfiguration -Path .\DSCTemplate -ComputerName localhost -Wait -Force -Verbose

}

I have saved the above Configuration as DSCTemplate.ps1 .  This is the file I will be injecting into the VHD Template in Virtual Machine Manager (I hope).  First step is to download the unattend.xml from that article.  Second step is to create a RunDSC.cmd file.  Using the example in the article this is the contents of my RunDSC.cmd.

schtasks.exe /Create /SC ONSTART /TN "\Microsoft\Windows\Desired State Configuration\DSCRestartBootTask" /RU System /F /TR 
"PowerShell.exe -ExecutionPolicy RemoteSigned -File c:\dsc\DSCTemplate.ps1"

On my Template VM I create a C:\DSC directory and copy the RunDSC.cmd and DSCTemplate.ps1 files.  The unattend.xml gets copied under the root directory (C:\).  Now, time to create a template from this VM, deploy it and see what happens.  That’s where we will pick up tomorrow.