KB: Automate the adding / removing / updating of a VMware connection (Using the powershell module)

Automate VMware connections using PowerShell

In some situations you may need to manage many vCenter or Esx Hosts connections. Ordinarily you would use the "Opslogix VMware Configuration Dashboard" for adding and removing connections, however this can be very time consuming using the GUI.

Since V20.11.x.x we have shipped a Powershell module to manage connection. By using the Opslogix VMware powershell module you can add and remove connections and you are able to assign host licenses. All examples are based on adding or modifying 1 connection. But you can easily change the script to handle multiple connections if needed.

You will need already to have a valid VMware license imported using the licensing dashboard.

This module is part of the software bundle. You will need to copy and import this module on the SCOM Management server where the Collector is installed.

Example script how to add a vCenter / Esx Host connection

## Notice: needs to be executed on a SCOM server were the Opslogix VMware collector is installed.

# load the module
# change the path to the vmware installation package
Import-Module "C:\install\Opslogix VMware\OpsLogix VMWare PowerShellModule\OpsLogix.IMP.VMWare.PowerShellModule.dll"

# check if the module is loaded
get-module 'OpsLogix.IMP.VMWare.PowerShellModule'

# supply the connection details
$LicenseKey = "paste encrypted license key here"
$vCenterFQDN = "vcenter01.contoso.com"
$ServiceAccUsername = "administrator@vsphere.local"
$ServiceAccPlainPassword = "*******"
$ResourcepoolName = "OpsLogix Resource Pool for VMware Management Pack"

# now we add the connection
# Note: if the connection already exists it will be updated
Add-SCOMVMwareConnection -Verbose -OpsMgrSDK "localhost" -vCenterFQDN $vCenterFQDN -ServiceAccUsername $ServiceAccUsername -ServiceAccPlainPassword $ServiceAccPlainPassword -ResourcepoolName $ResourcepoolName -LicenseKey $LicenseKey

# lets wait to give SCOM the time to process
sleep 120

# now we assign the host licenses
# you can use a Regex to filter the connections and hosts (see ConnectionRegEx and HostRegEx).
Set-SCOMVMwareHostLicences  -ConnectionRegEx "()" -HostRegEx "()"
# or
# when you don't use the filter by default every host is assigned as long you have licenses left.

# if you want to unassing you can use the parameter Assign = $false
Set-SCOMVMwareHostLicences  -HostRegEx "(Host1|Host2)" -Assign $false

# if you want to remove a vmware connection
Remove-SCOMVMwareConnection -Verbose -vCenterFQDN "vcenter01.contoso.com"