You are using vCloud Director, and want to add Virtual Machines from deployed vApps to specific Security Groups within vShield App. In my case, there were three Security Groups created to make a 3-tier environment. Web, App and Database.

Once again AlanRenouf came through by creating a vShield module for PowerCLI. Follow the directions in his video to install it. It’s actually quite easy.

The script I am going to list below requires valid connections to three sources in order to do the work:

The vCenter that manages the compute nodes in your vCloud

The vCloud Director cell.

The vShield Manager for the vCloud stack.

(You also need to be licensed for vShield App.)

Prior to connecting to vShield Manager, you will need to instantiate the module Alan created. That _should_ have been done when watching his video, but if not, do:

import-module vshield

within PowerCLI.

At this point you can connect to your three services:

connect-viserver <for vCenter>

connect-ciserver <for vCloud Director>

connect-vshieldserver <for vShield Manager>

Ok, so now hopefully our connections are set up. Let’s describe the script a little more. As I said before, the use case was to create a 3-tier environment via vShield App: Web, App and DB. Our VM’s in the vApp are conveniently named “WWW,” “APP” or “DB.” We are sort of cheating, and keying off that nomenclature to identify the VM’s.

We have three hardcoded security groups in the script: Web, App and DB. Their variables are $SGWeb, $SGApp and $SGDb. I know I am clever.

We are going to provide the name of a vAPP in vCloud Director from the command line. This script will then walk the contents of the vApp, which are our three servers. For those who are heavily involved in vCloud Director, you know that each VM in vCenter is identified by <VMNAME> (vCloud UUID). In order for us to add a VM to vShield App, which is tied to vCenter, we must actually push that naming nomenclature. I’m frankly not the best at coding, so I had to cheat and use the trim() function twice in order to pull the UUID out of the urn:vcloud:vm:uuid string.

At that point, we use PowerShell’s like function to do string comparison, and then run Mr. Renouf’s set-vshieldsecuritygroup in order to place the VM in to appropriate vShield App Security Group. That command is covered in his movie. I hope you find it useful!