So yesterday I finished work on Get-TargetResource. I use that term very loosely because I don’t believe in any way that it is actually going to work when I try to test it using the Resource Designer or with an actual Configuration. Either way it seems to “work” for now, so now we are going to move on to the Test-TargetResource function.
I am having a hard time wrapping my brain around this Test-TargetFunction, but lets get to work and see what we can break here. Looking at various existing DSC Resources, in my extremely simple example here I don’t think I need to really do anything beyond what I have already besides return $True or $False.
First thing that I do is that I copy the stuff from Get-TargetResource function to my Test-TargetFunction, so what I have looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#region function Set-TargetResource { [CmdletBinding()] param ( [ValidateSet("Present","Absent")] [System.String] $Ensure, [System.Boolean] $DependsOn, [parameter(Mandatory = $true)] [System.String] $Name, [parameter(Mandatory = $true)] [System.String] $VMMServer ) #Write-Verbose "Use this cmdlet to deliver information about command processing." #Write-Debug "Use this cmdlet to write debug information while troubleshooting." #Include this line if the resource requires a system reboot. #$global:DSCMachineStatus = 1 } #endregion #region function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [ValidateSet("Present","Absent")] [System.String] $Ensure, [System.Boolean] $DependsOn, [parameter(Mandatory = $true)] [System.String] $Name, [parameter(Mandatory = $true)] [System.String] $VMMServer ) Write-Verbose "VMMServer is $VMMServer" Write-Verbose "Hardware Profile Name is $Name" #Write-Verbose "Use this cmdlet to deliver information about command processing." #Write-Debug "Use this cmdlet to write debug information while troubleshooting." #Check if VirtualMachineManager module is present for SCVMM cmdlets If(!(Get-Module -ListAvailable -Name VirtualMachineManager)) { Throw "The VirtualMachineManager Module was not found. Please ensure this module is available by installing the VMM Console. For information On installing the VMM Console see: http://technet.microsoft.com/en-us/library/gg610627.aspx" } #Check to see if the specified VMM Hardware Profile Name exists $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer If($Name -cin $HWProfile.Name) { Write-Verbose "The Hardware Profile was found" } Else { Write-Verbose "The Hardware Profile was not found. Enter a valid Hardware Profile name" Throw "Unknown Hardware Profile" } <# $result = [System.Boolean] $result #> } #endregion |
I think the only thing that needs to be done right now is to change the check for the Hardware Profile to return $True or $False, so I modified it to look like this. I based this off of the xVMHyperV Resource.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$result = $false #Check to see if the specified VMM Hardware Profile Name exists try{ $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer If($Name -cin $HWProfile.Name) { Write-Verbose "The Hardware Profile was found" return $true } Else { return $false Write-Verbose "The Hardware Profile was not found. Enter a valid Hardware Profile name" Throw "Unknown Hardware Profile" } } catch [System.Management.Automation.ActionPreferenceStopException] { ($Ensure -eq 'Absent') } |
So, with that being done, let’s add some tests here and see what happens. Again, copying from the tests I used for Get-TargetResource.
1 2 3 |
#Test1 Test-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER -Verbose #Test2 Test-TargetResource -Name "DSCWEB Hardware Profile 2" -VMMServer MY-VMM-SERVER -Verbose #Test3 Test-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER2 -Verbose |
Test 1! I AM A DSC WIZARD!!!!!!!!!
1 2 3 4 5 6 7 8 9 |
PS C:\Scripts> Test-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER -Verbose VERBOSE: VMMServer is MY-VMM-SERVER VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'. VERBOSE: The Hardware Profile was found True |
Clearly, I am not a DSC Wizard. Far from it, but I am pretty excited my screen didn’t explode with red text :).
Test 2! I AM WINNING AT LIFE!!!!!
1 2 3 4 5 6 7 8 |
PS C:\Scripts> Test-TargetResource -Name "DSCWEB Hardware Profile 2" -VMMServer MY-VMM-SERVER -Verbose VERBOSE: VMMServer is MY-VMM-SERVER VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile 2 VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'. False |
Test 3! STILL WINNING!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
PS C:\Scripts> Test-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER2 -Verbose VERBOSE: VMMServer is MY-VMM-SERVER2 VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'. VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'. Get-SCHardwareProfile : VMM is unable to connect to the VMM management server MY-VMM-SERVER2 because the specified computer name could not be resolved. (Error ID: 1601) Ensure that the computer name is correct, and then try the operation again. If the problem persists, contact your network administrator. At line:42 char:22 + $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (:) [Get-SCHardwareProfile], CarmineException + FullyQualifiedErrorId : 1601,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetSCHWConfigCmdlet |
That’s exactly what should happen, since that server doesn’t exist. Tomorrow we are going to explore the Set-TargetResource function, even though right now I don’t believe I have anything that needs to be set. However, I will have stuff that will need to be set eventually so I suppose I should get started on it anyways.
Should also mention that there is still no sign of Depends On, so not real sure what is going on there yet.