PowerShell Desired State Configuration (DSC) Journey – Day 16

When I started this original project my intention was to work through all of the individual DSC Resources one at a time, hoping that I could get through them all before they released some more.  Well, that didn’t happen.  On Friday the PowerShell Team released DSC Resource Kit Wave #3, which is freaking awesome.  So, instead of working through the rest on my list, I am just going to start working on a project I probably should have been working on anyways.  I want to build a Configuration to replace using Virtual Machine Manager Templates, with the goal being to be able to deploy multiple, identically configured servers at one time.  I want to do this for a demo I am going to be doing (hopefully before I leave for the PowerShell Summit).

The information on the Hyper-V module can be found here, and I will also be referencing this post on building Composite DSC Resources.  After a lot of time spent looking at the documentation, I am just going to follow along with the example on the TechNet page in creating the various parameters and pieces of the Hyper-V portion of my Configuration.

First, let’s start with the xVMSwitch Resource.

PS C:\Scripts> Get-DscResource -Name xVMSwitch | Select-Object -ExpandProperty Properties

Name                                            PropertyType                                                                        IsMandatory Values                                         
----                                            ------------                                                                        ----------- ------                                         
Name                                            [string]                                                                                   True {}                                             
Type                                            [string]                                                                                   True {External, Internal, Private}                  
AllowManagementOS                               [bool]                                                                                    False {}                                             
DependsOn                                       [string[]]                                                                                False {}                                             
Ensure                                          [string]                                                                                  False {Absent, Present}                              
NetAdapterName                                  [string]                                                                                  False {}

Here is what I started off my Configuration with.

Configuration BuildTestVM
{

Import-DscResource -Module xHyper-V

Param{

    [Parameter(Mandatory)]
    [ValidateNotNullorEmpty()]
    [String]$SwitchName,

    [Parameter(Mandatory)]
    [ValidateNotNullorEmpty()]
    [String]$SwitchType

}

    xVMSwitch DSCSwitch
    {
        Name = $SwitchName
        Ensure = "Present"
        Type = $SwitchType
        AllowManagementOS = "True"
    }

}

ProTip:  Don’t import the Module before declaring your parameters like I did above.  It will fail miserably.  I should also mention that all of these examples will be done on my PC using Hyper-V Manager because I don’t have access to a functional lab that will allow me to do what I want to do here.  I Invoke the Configuration and get a .MOF file for my PC.

BuildTestVM -SwitchName DSCTest -SwitchType Internal

I then push out that Configuration file to my PC using this command.

Start-DscConfiguration -Wait -Verbose -Path .\BuildTestVM

Here is the Verbose output and a screenshot showing the completed switch.  It amazes me how fast this completed.  If I create a new VMSwitch through the GUI and select the “Use Guest OS” option my computer literally locks up for 5 minutes.

PS C:\scripts> Start-DscConfiguration -Wait -Verbose -Path .\BuildTestVM
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' =
root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer localhost with user sid S-1-5-21-738551990-92959840-526660263-26310.
VERBOSE: [localhost]: LCM:  [ Start  Set      ]
VERBOSE: [localhost]: LCM:  [ Start  Resource ]  [[xVMSwitch]DSCSwitch]
VERBOSE: [localhost]: LCM:  [ Start  Test     ]  [[xVMSwitch]DSCSwitch]
VERBOSE: [localhost]:                            [[xVMSwitch]DSCSwitch] Checking if Switch DSCTest is Present ...
VERBOSE: [localhost]:                            [[xVMSwitch]DSCSwitch] Switch DSCTest is not Present
VERBOSE: [localhost]: LCM:  [ End    Test     ]  [[xVMSwitch]DSCSwitch]  in 10.1500 seconds.
VERBOSE: [localhost]: LCM:  [ Start  Set      ]  [[xVMSwitch]DSCSwitch]
VERBOSE: [localhost]:                            [[xVMSwitch]DSCSwitch] Switch DSCTest is not Present.
VERBOSE: [localhost]:                            [[xVMSwitch]DSCSwitch] Creating Switch ...
VERBOSE: [localhost]:                            [[xVMSwitch]DSCSwitch] Switch DSCTest is now Present.
VERBOSE: [localhost]: LCM:  [ End    Set      ]  [[xVMSwitch]DSCSwitch]  in 5.7440 seconds.
VERBOSE: [localhost]: LCM:  [ End    Resource ]  [[xVMSwitch]DSCSwitch]
VERBOSE: [localhost]: LCM:  [ End    Set      ]
VERBOSE: [localhost]: LCM:  [ End    Set      ]    in  17.3600 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 17.558 seconds

dsc27

Tomorrow I will work on creating the .VHDX file(s) that are required.