How to use PowerShell Modules with vRA 8.2
Options to use PowerShell Modules
I like to review VMware vRA (vRealize Automation) 8.2 as a Microsoft Windows Server Admin. Most of the reviews you see are creating Linux VMs and customizing the OS using tools for Linux. I create Windows/Linux VMs and customize the OS using a new feature added to vRA 8.x, Action Based Extensibility (ABX) with PowerShell.
In my vRA 7.6 environment, I use a PowerShell Host to run processes and on the PowerShell Host is where I install all the PS Modules that I use. When I started looking at how to use PowerShell with vRA 8.2 the one item that I needed to understand was how to use PowerShell Modules. VMware does have a Blog Post on how to include PowerShell modules with ABX Actions/vRO Work Flows - Click Here to see the Blog Post. After doing some testing and reviewing my options I decided to continue to use a PowerShell Host with vRA 8.2. What this allows me to do is use the same exact PowerShell Host for 7.6 and 8.2. The existing PS scripts that I have also work with both 7.6 and 8.2. No changes to any scripts and the PS Host. I like how VMware included PS as an options for all scripts within vRA 8.2. Hopefully in the future VMware will allow you to install your own PS Modules directly in the vRA Appliance. The one item that some people may not like about this setup is it does require an additional Windows Server that a standard vRA setup does not require. But to be able to use an existing Windows PS Host that can be shared between vRA 7.6 and 8.2 until I get my environment to 100% vRA 8.2 also has it positives. With Automation there is always many ways to complete the same task.
I wrote a Blog Post previously that describes how to pass Cloud Template inputs/properties to ABX Actions. This Post is really using that same technique but instead of only running an ABX Action, this ABX Action Script is passing the inputs/customproperties onto a PowerShell Host. Here is link to previous post: VREALIZE AUTOMATION 8.2 - POWERSHELL ABX. By passing the custom properties as parameters to a PS Host I can also use PS Modules to connect to SQL servers and many other options. Basically any PS Module that is available can be used.
Details on how to use a ABX Script with a PowerShell Host:
- Create a Cloud Template. (See included yaml code and screen shot).
- Create inputs for the parameters you need to send to a PS script that will run on the PS Host. The inputs are the values that are used for the resource/properties. (See included yaml code)
- Create Properties in the resources section of the yaml for the inputs you just created. These properties will be the parameters that get passed to the PS Script. The properties get passed to the ABX script using $payload. (See included PS code)
-
Step 2:
- Create a PowerShell ABX Action. (See included PS code and screen shot)
- To get the custom properties from the Cloud Template you need to use $payload.
- $payload.customProperties."Properties Name" is how you get the values from the $payload.
- You can assign the custom properties to a variable within the ABX PS code.
- I use Invoke-VMScript to run the PS Script on the PS Host because my environment is also a Zero Trust Environment. Invoke-VMScript allows me to run PS scripts on remote servers without opening ports to the remote servers.
-
Step 3:
- Create a Subscription. See Screenshot.
- The subscription is want runs the ABX scripts.
- A subscription can be Fired before or after a compute resource gets provisioned. I create a subscription for both in my environment.
- Within the subscription is where you define which ABX script is run. If you would want to string several ABX scripts together, the subscription could also run a ABX Flow.
Sample Code and Screen Shots:
Cloud Template:
The properties under resources in the yaml code will be used like parameters in a PS Script. Since I have a background in PowerShell that is how I am using the properties. When you deploy the Cloud Template, the values entered that I defined will not be used until the ABX script runs. The ABX Script will run after the subscription is triggered. The values will be passed from the Cloud Template to the ABX Script using $payload. Just like passing parameters to a PS script.
Click to expand code:
|
|
ABX Action Script:
In my PowerShell code I am using Write-Host to help show how the Cloud Template passes values to the ABX Action using the $payload. I use the code to troubleshoot and understand how it is working. I am able to take values from the payload customProperties and assign to variables. The customProperties are defined in the Cloud Template yaml code as resources/properties. I can then make decisions in the code based on these values. For production the write-host lines could be removed.
Click to expand code:
|
|
Subscription:
- vRA 7.6 and 8.2 can share the same PS Host and Scripts.
- A lot of the PowerShell code and logic I use in vRA 7.6 will be able to be reused/shared.
- Using a PS Host does require an additional VM.
- Within ABX scripts I have always changed the default "Custom limits and retry options". I set the "Memory Limit" to 1024 and "Timeouts" to 900.
- Anytime I speak to others about Automation the one item I stress is there is no right way or wrong way to accomplish your tasks. Build on the skills that your Team already has. The environment I work in is 90% Windows Servers so we use PowerShell a lot. That is why I needed to be able to use all the PS Modules that I have installed on the PS Host.
- If you like wearing Crocs and want to get a pair like I wear, follow this link to Amazon: My Favorite Crocs
- If you found this Blog article useful and it helped you, Buy me a coffee to start my day.