1

I'm having some trouble understanding the DSC script resource, in particular the test and get phases. I am trying to change the default Hyper-V VHD and VM paths, but can't seem to work out how to validate those via DSC scripts.

I usually use the below command to accomplish this goal:

Set-VMHost -ComputerName NAME -VirtualHardDiskPath D:\HV\VHD -VirtualMachinePath D:\HV\VM

Thanks!

Garrett Dumas
  • 287
  • 2
  • 9

1 Answers1

3

The DSC Script Resource needs three blocks:

SetScript
- This is run to apply the desired configuration
TestScript
- This is run to see whether the desired configuration should be reapplied
GetScript
- This script should return a hashtable with information about the current configuration

In your situation that would translate to something like:

Script SetHypVPaths {
    SetScript = { 
        Set-VMHost -VirtualHardDiskPath D:\HV\VHD -VirtualMachinePath D:\HV\VM 
    }
    TestScript = {
        $VMHost = Get-VMHost
        return ($VMHost.VirtualHardDiskPath -eq "D:\HV\VHD" -and $VMHost.VirtualMachinePath -eq "D:\HV\VM")
    }
    GetScript = {
        $VMHost = Get-VMHost
        return @{
            VirtualHardDiskPath = $VMHost.VirtualHardDiskPath
            VirtualMachinePath = $VMHost.VirtualMachinePath
        }
    }
}
Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95