PowerShell DSC Journey – Day 18

Yesterday I finished up modifying the Set-TargetResource function and doing all the tests and it seems to be working exactly the way that I want. The next step today is to turn this into a module, import it, write a DSC Configuration and see if it actually works.

I already have my SCVMM_Hardware.psm1 file, so I just need to add a module manifest file.

PS C:\scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -RootModule 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psm1' -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Mod
ules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1".

Looking at the other DSC Resources, none of them specify a root module, so I guess we will see if this breaks it or not. They also have multiple resources associated with each module, so that could be part of the reason as well.

This article on TechNet says to “Finally, use the New-ModuleManifest cmdlet to define a .psd1 file for your custom resource module. When you invoke this cmdlet, reference the script module (.psm1) file”, but the module manifest they show has the root module as ”, which doesn’t match up with what they are saying. I am just going to try it this way and see what happens. Because that’s what I do. Actually, I am going to change something up here to prepare for my other SCVMM Resource.

  • Rename the root folder to cSCVMM
  • Create a DSCResources folder underneath
  • Create a cSCVMM_Hardware folder, move my .psm1 and schema.mof files into this folder
  • Create the module manifest and place it in the root cSCVMM folder

Round 2.

PS C:\scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -RootModule 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psm1' -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Modules\DSCResource
s\cSCVMM\cSCVMM.psd1".

So far so good! (No errors anyways). I am going to restart ISE and see if it loads the module or what happens. This is where I had so much trouble in my previous post. And I ran into the same issue. If I say Get-Module -Name cSCVMM nothing happens. If I do Get-Module -ListAvailable it shows up in the list. So first thing, I am going to get rid of the root module path and see what happens.

PS C:\Scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Modules\DSCResource
s\cSCVMM\cSCVMM.psd1".

Same thing. Move it out of the DSCResources folder, same thing.

Looking at the other resources, they have a CLR version and a description, so let’s just try that (although I am sure that has nothing to do with it).

New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -ClrVersion 4.0 -Description "Module with DSC Resources for System Center Virtual Machine Manager" -Verbose

Long story short, turns out (once again) that I just don’t know what I am doing. Once you import it by name, you can then get the information on it. Duh.

PS C:\Scripts> Import-Module cSCVMM -Verbose
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1'.

PS C:\Scripts> Get-Module -Name cSCVMM

ModuleType Version    Name                                ExportedCommands                                                                                                                                          
---------- -------    ----                                ----------------                                                                                                                                          
Manifest   1.0        cSCVMM                                                                                                                                                                                        

So, now that I wasted a bunch of time on that, let’s see if I can remember how to build a Configuration :).

First issue I run into is, when I do Import-DSCResource -Module cSCVMM it acts like it has no idea what the hell that is. So, uh, what do I do about that?

Configuration TestSCVMMHardwareProfile
{
    Import-DscResource -Module xNetworking
    Import-DscResource -Module cSCVMM

    node localhost
    {

    }
}

So, yeah. My .psm1 file is exporting the commands, so that shouldn’t be a problem. I guess first thing first, let’s run Get-DSCResource and see what happens.

PS C:\Scripts> Get-DSCResource
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2336 char:13
+             $moduleName = $moduleFolder.FullName.Substring($folder.Le ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

I get that error, but then all of the DSCResources are listed (except for mine).

Further investigation reveals this.

PS C:\Scripts> Get-DscResource -Name cSCVMM
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2336 char:13
+             $moduleName = $moduleFolder.FullName.Substring($folder.Le ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException
 
CheckResourceFound : The term 'cSCVMM' is not recognized as the name of a Resource.
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2428 char:13
+             CheckResourceFound $name $resources
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,CheckResourceFound

Alright, so that’s fun. I am going to backup here and try something referencing the steps in this article.

I create a “new” DSCResource using my properties. And guess what? Even then it doesn’t show up as a DSC Resource. So, that’s good right? I don’t think so. That article makes it seem like it should be pretty simple, so I don’t know what’s going on.

Next step, I am going to test this on WMF 4.0 (I am using WMF 5.0 Preview). And the same thing happens there. So that’s good I guess. I really have no idea what is going on here. Completely. Puzzled.

Time to stop for the day. Hopefully I think of something before tomorrow as of what to try next.

Edit: I was able to resolve this issue with the help of Don Jones on the PowerShell.org forums.

When I first created the Resource what I did was this:

New-xDscResource -Name cSCVMM_Hardware -Property $DVDDrive, $VMNetwork, $CPUCount, $Ensure, $Name, $VMMServer -FriendlyName "SCVMM_Hardware" -ClassVersion 1.0 -Path 'C:\Program Files\WindowsPowerShell\Modules\'

What I should have done was this:
New-xDscResource -Name cSCVMM_Hardware -Property $DVDDrive, $VMNetwork, $CPUCount, $Ensure, $Name, $VMMServer -FriendlyName “SCVMM_Hardware” -ClassVersion 1.0 -Path ‘C:\Program Files\WindowsPowerShell\Modules\cSCVMM’

Since I didn’t specify the Module name it just created the DSC Resources folder and that messed up everything. Lesson learned!