In my last 5 or so posts, I have explored a little bit further deploying VMs using the Hyper-V Resources. While this is great, it limits what I can do from a VM deployment scenario. Especially when I have VMM at my disposal. So, I am going to go down the route of creating my own VMM Resources to build VMs, using DSC.
There is going to be A LOT of failure. Buckle up.
Here is a list of all the resources I have bookmarked and ready to go for use in this endeavor. I will also be heavily referencing the existing Hyper-V Resources, because they provide an excellent template for what I am going to try and do (and they already work!).
Reusing Existing Configuration Scripts in PowerShell DSC
Building Clouds Blog: Authoring DSC Resources when Cmdlets already exist
Building Clouds Blog: Testing DSC Resources
How Do I Create My Own DSC Resource?
PowerShell Blog: Walkthrough Writing a DSC Resource
Writing Help for Custom DSC Resources
Steve Murawski: Building DSC Custom Resources
With that out of the way, let’s take a look at the existing Hyper-V Resources, and compare them with what a successful SCVMM server build requires.
The xHyper-V Module includes Resources for:
Now, I am going to list the possible Resources I would need to create a VM using SCVMM, based off of the scripts you get at the end of the Create New Virtual Machine wizard. I walked through one wizard using a template, and one using an existing virtual disk.
- New Virtual SCSI Adapter
- New Virtual DVD Drive
- New Virtual Network Adapter
- Existing Hard Disk Info (If using an existing disk)
- New Disk Drive (If using an existing disk. This step doesn’t exist for a template)
- CPU Type
- New Hardware Profile
- Template Information (If using a template)
- Answer File (If using an answer file). Can probably skip this step by using the xVHDFileDirectory Resource to do the same thing. We will see.
- Operating System Information
- New SCVM Template (Temporary during creation…interesting)
- New VM Configuration
- Get/Set VM Host Information
- Get/Set CSV Information for storing the VM
- Finally, Create the VM
I am sure that list is going to be modified. Just for the sake of not confusing the hell out of myself, we are going to explore creating Custom Resources for SCVMM with the assumption that we will be using an existing template. If I survive that experience then I can add in the functionality for using an existing VHD relatively easily (I hope).
So, my possible Resource list looks like this:
- SCVMM_Hardware – Includes all the appropriate hardware configurations
- SCVMM_Template – Includes all the appropriate template information
- SCVMM_VM – Anything else I need to build the VM
Now, laid out like that, it doesn’t look too difficult. I am just going to start with the SCVMM_Hardware and see how it goes from there. I am going to start with the article on Authoring DSC Resources When Cmdlets Already Exist, because SCVMM already has all the cmdlets that I need as well as the article on using the Resource Designer Tool. First step, install the Resource Designer Tool! The article was written before PowerShellGet was available, but I bet it has what we need.
Install-Module xDSCResourceDesigner -Verbose
Now the other thing the Writing a DSC Resource article talks about is getting the list of Configuration Properties you need for your DSC Resource. So, for my SCVMM_Hardware Resource I am going to start out with the following Configuration Properties. The goal of this Resource will be to create the New SCHardwareProfile.
- Ensure (Present, Absent)
- Name (Name of Hardware Profile. No idea if this will work. This should take a value from a parameter probably)
- SCVirtualDVDDrive ($True or $False)
- CPU Count
- DynamicMemoryEnabled ($True or $False)
- HighlyAvailable ($True or $False)
- Generation (1 or 2)
There are about 40 parameters for creating a new Hardware Profile, but I think this enough for now. The only mandatory parameter is Name, so all the other stuff is just gravy.
Next step! First thing first, lets import the Designer Resource just to be safe.
PS C:\Scripts> Import-Module -Name xDSCResourceDesigner
Following along, lets check out the commands that are available.
PS C:\Scripts> (Get-Module xDSCResourceDesigner).ExportedCommands
Continuing along, lets check out New-DSCResourceProperty. Not so fast!
PS C:\Scripts> Help New-DSCResourceProperty
Get-Help : Get-Help could not find New-DSCResourceProperty in a help file in this session. To download updated help topics type: "Update-Help". To get help
online, search for the help topic in the TechNet library at http://go.microsoft.com/fwlink/?LinkID=107116.
At line:55 char:7
+ Get-Help @PSBoundParameters | more
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
I Update-Help but it still doesn’t exist. So that’s awesome. I do find this article on the xDSCResourceDesigner, so that’s cool I guess. However, I also know from Michael Greene’s article that I can also the Command Add-On in PowerShell to check out this command.
Combine all the articles together and this is what I come up with for my Resource Property definitions. Prepare yourselves. I should also mention that while doing this I realized I needed at least one Resource Property to have an Attribute of type Key. I choose Name to be my Key value. According to the Petri article “Following the rules of DSC Resources, one of the parameters must be defined as a unique key.” I also have about a bajillion questions after doing this, but we will see what happens. I should also mention that I added descriptions to each of the resources after looking at the MSFT_xVMHyperV.Schema.mof . I also realize I could have done this with just one property, Name, but what fun would that be? Go big or go home.
$Ensure = New-xDscResourceProperty -Name Ensure -Type String -Attribute Write -ValidateSet "Present","Absent"
$DependsOn = New-xDscResourceProperty -Name DependsOn -Type Boolean -Attribute Write
$Name = New-xDscResourceProperty -Name Name -Type String -Attribute Key -Description "Name of the hardware profile"
$DVDDrive = New-xDSCResourceProperty -Name DVDDrive -Type Boolean -Attribute Read -Description "Should DVD Drive be crated"
$VMNetwork = New-xDSCResourceProperty -Name VMNetwork -Type String -Attribute Read -Description "Name of VM Network to connect to"
$CPUCount = New-xDSCResourceProperty -Name CPUCount -Type Uint64 -Attribute Read -Description "Number of CPU's"
$MemoryMB = New-xDscResourceProperty -Name MemoryMB -Type Uint64 -Attribute Read -Description "Memory allocated to VM"
$HighlyAvailable = New-xDSCResourceProperty -Name HighlyAvailable -Type Boolean -Attribute Read -Description "Should VM be highly available"
$Generation = New-xDscResourceProperty -Name Generation -Type Uint64 -Attribute Read -ValidateSet "1","2" -Description "Generation of VM"
$VMMServer = New-xDscResourceProperty -Name VMMServer -Type String -Attribute Required -Description "Name of VMM Server"
That’s enough for today. I will pickup tomorrow with attempting to create a Resource off of this and see what breaks.