PowerShell DSC Journey – Day 9

When I left off I had modified my Get-Target resource function to include steps for checking to make sure SCVMM cmdlets existed, and that the specified VMM Server was a valid VMM Server. Next, I need to make sure that the given Name of the Hardware Profile exists.

The issue I run into here is that the Get-SCHardwareProfile doesn’t have a Name parameter (because that would make too much sense) only an ID value.

So, knowing the ID I can do this.

To make this work, I am going to need to get the ID of the profile using the name, without really using the name. I thought this was going to be complicated, but it turns out to be easier than I thought.

So, to test this let’s do this:

And here is some output from testing this.

Alright, so let’s add some tests into the Get-TargetResource Function before we populate the $returnValue section.

Let’s test the first one and see what happens.

What the what? I clear all my variables (because opening a new session is too easy) and try again. Same thing. Fine, I will open a new tab in ISE. That also doesn’t work. Well, what the hell, I was just using this and it was working fine! After reading through this, I see what I am doing wrong. I need to replace Get-SCHardwareProfile -VMMServer (Get-SCVMMServer -ComputerName $VMMServer) with $HWProfile = Get-SCHardwareProfile -VMMServer $SCVMMServer . And, that doesn’t work so well either.

Alright. So let’s back up here for a second. Get-SCVMMServer -ComputerName MY-VMM-SERVER works fine. If I set $VMMServer = “MY-VMM-SERVER” and then run Get-SCVMMServer -ComputerName $VMMServer it works fine. If I then run $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer it works fine. So, uh, why isn’t this working for me in my Get-TargetResource function? The function is doing the exact same thing, and failing. I have no idea why.

I add the following two lines to the type of the script (underneath the parameter declaration) just to make sure nothing screwy is happening (and it isn’t).

I do some more testing, and something screwy is going on. It looks like once a connection is made to the VMMServer it isn’t able to make the connection a second time in the same script? Does that even make sense? I change the $SCVMMServer parameter to return the entire SCVMMServer object, and then see if the $VMMServer matches $SCVMMServer.Name like this.

Once I do that, it works like magic!

Alright, let’s test the next scenario.

Well, that’s great and all, except that it didn’t throw an error and stop, which it needs to. So, I add the following line to my function (I place it after the Verbose line, because I figured out that once it does the Throw, there isn’t anything else that happens after that).

Now, this is better.

And finally for my last test.

This is great and all, except that it shouldn’t try to find the Hardware Profile if it can’t contact the VMMServer. I check the $ErrorActionPreference and it is set to continue, I want to set this (in the function) to stop. There is no reason for it to keep going if it encounters an error. I add $ErrorActionPreference = “Stop” to the beginning of the function and try my test again.

I will spare you the text, but it failed to connect to the VMMServer, and then stopped. Up next, filling out the return values section and testing that out.