PowerShell DSC Journey – Day 15

So when I left off yesterday I appeared to have my Get and Test TargetResource functions working properly, now it’s time to work on the Set-TargetResource function, which is probably where the fun will happen.

When I go to set the Resource, I have a couple of different scenarios I need to work through.

  • Hardware Profile exists, ensure equals absent
  • Hardware Profile exists, ensure equals present
  • Hardware Profile does not exist, ensure equals present

And here is what I came up with those 3 scenarios after working my way through it (and looking at the xVMHyperV Resource).  I almost immediately ran into a problem where there doesn’t seem to be a clear way to tell the Hardware Profile that it needs a DVD drive (at least not using the Set-SCHardwareProfile cmdlets).  A little research led me to the Set-SCVirtualDVDDrive cmdlets which can be associated with “a virtual machine, virtual machine template, or hardware profile used in a Virtual Machine Manager (VMM) environment.” which is perfect.  When I set the New DVD Drive I must specify a LUN and a BUS.  I am going to need some extra logic eventually to deal with what happens if you have multiple disks or whatever else, but for right now this will have to do.  I don’t even want to admit how long this took me to work through, but it appears to work.

CPU Count is easy and straightforward, which was nice.

However, I ran into the same problem with VMNetwork that I did with the DVDDrive (which doesn’t make me happy). But since I already had that experience it was much quicker to come up with what to do about it.

Now that that is done (I feel like I just went through the ringer), all I really need to do for the next part is copy and paste the commands from above into the area where I am creating a Hardware Profile if none exists.

So I guess now we need to test this thing and see if it blows up. I am going to start with my original 3 tests and then add in some variations (providing those still work). I run the first one for an existing Hardware Profile and find out quickly I need to specify the Ensure parameter. Which means I should probably make that a mandatory property (I think?). I don’t see anyone leaving that out unintentionally but maybe I should add in some logic to check for that? I guess I will figure that out later.

Winning! Test #2! This does not go as well.

Alright, so it’s basically exiting after checking if the Hardware Profile exists. In this test, the profile does not exist, so it should jump down to the Else block and create it, however that is not happening. I am ashamed to admit this but I had an extra closing bracket in the top half of the script, so it was exiting before creating the profile. When I run it again, it works, sort of. The profile is created, but it throws errors because I didn’t specify a CPU count or a VMNetwork, because they aren’t mandatory. So, I need to do some updates here I think.

For the second test, it created the Hardware Profile, with no errors, however it added “VERBOSE: Hardware Profile DSCWEB Hardware Profile 2 is Absent” after displaying the Hardware Profile, which uh….doesn’t make any sense?

Looking at the verbose messages closer, I have a couple of issues that I need to get cleaned up. Which will be a good thing to do tomorrow.