I’m writing a script to deploy Azure VMware Solution (AVS) and ran into a situation many of us likely have: Some parameters depend on other parameters.

I started with Parameter Sets where I did have several parameters participating in multiple Parameter Sets, but that didn’t work how I thought it would (or should).

Here’s what didn’t work:

My intention was to have additional mandatory parameters based on additional switches.  For instance, if you add “-createvNet”, the script needs four additional parameters.  Also, if you used “-EnableVMInternet” without “-createvNET”, the script will also need to recognize that wasn’t supplied and make the parameters with it mandatory.  Spoiler: that didn’t work.

The problem I had was using “-createvNET” doesn’t actually force the four additional parameters to mandatory, since the ParameterSet is “cli” by default.  I tried to set the “-createVNET” switch to it’s own parameter set, but that’s where the either/or came in. When I tried to use the mandatory parameters from “cli” AND “createvNET”, PowerShell threw the error:

This won’t work for me because the Parameter Set is “cli”, my additional parameters needed to create the vNet aren’t mandatory, so I decided to try my hand at Dynamic Parameters. These are more complicated because they’re basically built during runtime based on whatever criteria you supply.
In my case, I wanted to start with just the four parameters that become required when supplying the “-createVNET” switch.

Here’s what that looks like (this is my first stab at Dynamic Parameters, fyi):

This actually worked perfectly as desired in two scenarios:

  • If I only provided “-createVNET”, the script would prompt for the four missing parameters.
  • I could supply “-createVNET” along with the additional four mandatory parameters and it’d work directly in the single CLI.

That’s a lot of code for four parameters, though, and in three months (or three days), I’ll likely forget how this all works. I still have other parameters and dependencies that need to be accounted for. As functional as this is, I might scrap it.

Let me know what you think in the comments.