KB: Automate the adding / removing / updating of a VMware connection

Automate VMware connection 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.

By using the script below you can add and remove  connections by making use of a PowerShell script. All examples are based on modifying 1 connection. But you can easily change the script to handle multiple connections if needed.

Example script how to add a vCenter / Esx Host connection

 

## ======================================================================
## Example script how to add a vCenter / Esx Host connection
## in SCOM using Powershell
## ======================================================================
## Michel Kamp
## ======================================================================
## Run this script on a MS server
## ======================================================================
## Notice:
## for the $ConnectionPassword you will need the password hash. To get this hash
## first manually using the SCOM VMware configuration dashboard add one connection
## and copy value the password property to use in the $ConnectionPassword
## ======================================================================

## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == create/add a new vCenter / Host Connection
## ===========================================================
$ConnectionName = "vCenter01.contoso.com"
$ConnectionUserName = "root"
$ConnectionPassword = "xxxxxxxxxx" ## should be the password hash
$LicenseKey="xxxxxxxxx" ## apply the license key hash

## create discovery handler
$Class = Get-SCOMClass -Name OpsLogix.IMP.VMWare.vCenter
$discovery = New-Object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData
$ConnectionClassObject = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($mg,$class)
# set the KEY props
$ConnectionClassObject[$Class,"vCenterHostName"].Value = $ConnectionName
$ConnectionClassObject[$Class,"ConnectionURL"].Value = "https://$ConnectionName/sdk"
$ConnectionClassObject[$Class,"LicenseKey"].Value = $LicenseKey
$ConnectionClassObject[$Class,"Username"].Value = $ConnectionUserName
$ConnectionClassObject[$Class,"Password"].Value = $ConnectionPassword

# add the connection first
$discovery.Add($ConnectionClassObject)
$discovery.Overwrite($MG)

# now create the pool relation ships
# first we create the connection to the resource pool
# get target
$Connection = Get-SCOMMonitoringObject -Id $ClassObject.Id

# get source target
$PoolRel= Get-SCOMRelationship -id cb72a458-d56e-3be8-950b-955b16f2f6a2
$class = Get-SCOMClass -Name "OpsLogix.IMP.VMWare.OpsMgrResourcePool"
$Pool = Get-SCOMMonitoringObject -Class $Class

$Poolrelation=New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementRelationshipObject($PoolRel.ManagementGroup,$PoolRel)
$Poolrelation.SetSource($Pool);
$Poolrelation.SetTarget($Connection);

# second we create the connection to the action point
$ActionPointRel= Get-SCOMRelationship -id cdb09107-2411-d9e2-d718-e574983d304d
$Actionrelation=New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementRelationshipObject($ActionPointRel.ManagementGroup,$ActionPointRel)
$Actionrelation.SetSource($Pool);
$Actionrelation.SetTarget($Connection);

# add and write back to scom
# this will create the connection visible in scom
$discovery.Add($Actionrelation)
$discovery.Add($Poolrelation)
$discovery.Overwrite($MG)

## ==================

Example script how to change a vCenter / Esx Host connection

 

## ======================================================================
## Example script how to change a vCenter / Esx connection
## in SCOM using Powershell
## ======================================================================
## Michel Kamp
## ======================================================================
## Run this script on a MS server
## ======================================================================
## Notice:
## for the $ConnectionPassword you will need the password hash. To get this hash
## first manually using the SCOM VMware configuration dashboard add one connection
## and copy value the password property to use in the $ConnectionPassword
## ====================================================================

## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == change the user or password on a connection
## ===========================================================
$ConnectionDisplayName = "vCenter01.contoso.com" ## you can use wildcards/regex
$ConnectionUserName = "root"
$ConnectionPassword = "xxxxxxxxxx" ## should be the password hash
## change the connection
$Class = Get-SCOMClass -Name OpsLogix.IMP.VMWare.vCenter
$ConnectionClassObject = Get-SCOMMonitoringObject -Class $Class | where { $_.DisplayName -like $ConnectionDisplayName}
# change the property
$ConnectionClassObject[$Class,"Username"].Value = $ConnectionUserName
$ConnectionClassObject[$Class,"Password"].Value = $ConnectionPassword
# write it to scom
$ConnectionClassObject.Overwrite()

## ==================

Example script how to remove a vCenter / Esx connection

 

## ======================================================================
## Example script how to remove a vCenter / Esx  connection
## in SCOM using Powershell
## ======================================================================
## Michel Kamp
## ======================================================================
## Run this script on a MS server
## ======================================================================
## login to the MS group
New-SCOMManagementGroupConnection -ComputerName "localhost"
$mg = Get-SCOMManagementGroup

## ===========================================================
## == remove a vmware connection
## ===========================================================
$ConnectionDisplayName = "vCenter01.contoso.com" ## connection to be removed

## get the class instance to remove

$Class = Get-SCOMClass -Name OpsLogix.IMP.VMWare.vCenter
$ClassInstance = Get-SCOMMonitoringObject -Class $Class | where { $_.DisplayName -like $ConnectionDisplayName}
# add the class instance to delete
$discovery = New-Object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalDiscoveryData
$discovery.RemoveInternal($ClassInstance,$ClassInstance.GetClasses()[0])
## execute the delete
$discovery.Commit($MG)
## ==================