Running PowerCLI Cmdlets in Morpheus Appliance

Introduction:
In this article, we will outline the steps to run PowerCLI cmdlets within a Morpheus appliance environment. This capability allows users to automate VMware vSphere tasks directly from the Morpheus platform.

Prerequisites:
Before proceeding with running PowerCLI cmdlets on Morpheus appliance, ensure the following prerequisites are met:

  1. Install PowerShell Core (PWSH):
  • Install PowerShell Core on all Morpheus appliance nodes.
  • Open a terminal and type pwsh to access the PowerShell Core console.
  1. Install VMware PowerCLI Module:
  • Install the VMware PowerCLI module using the following cmdlet:
Install-Module -Name VMware.PowerCLI.VCenter
Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCeip $false -confirm:$false
Set-PowerCLIConfiguration -Scope AllUsers -InvalidCertificateAction Ignore -confirm:$false

Creating a PowerShell Task:

  1. Navigate to Morpheus UI:
  • Log in to the Morpheus UI with appropriate credentials.
  1. Create a Shell Task:
  • Navigate to Tasks > Create Task.
  • Select “Shell” as the task type and ensure “Execute as Sudo” is checked.
  • Set the Execute target to “Local.”
  1. Sample PowerShell Script:
  • In the task content, include the following PowerShell script block:
pwsh -command "& {
    # PowerShell script block starts here
    \$VM = '<%=instance.containers.server.name%>'
    \$Instance = '<%=instance.name%>'
    \$vCenterUser = '<%=cypher.read('secret/VcenterServiceAccount')%>'
    \$vCenterPassword = '<%=cypher.read('secret/VcenterServiceAccountPassword')%>'

    # Convert plaintext password to a secure string
    \$secStringPassword = ConvertTo-SecureString \$vCenterPassword -AsPlainText -Force

    # Create PSCredential object with the provided credentials
    \$vCreds = New-Object System.Management.Automation.PSCredential (\$vCenterUser, \$secStringPassword)

    # Define vCenter server IP address or hostname
    \$vCenters = 'XXXXXXXXXXXXXXXXXXXXXXXX'

    # Connect to the vCenter server using the provided credentials
    Connect-VIServer \$vCenters -Credential \$vCreds
}"

Conclusion:
By following these steps, users can effectively leverage PowerCLI cmdlets within the Morpheus appliance environment to automate VMware vSphere tasks seamlessly.

2 Likes

@vsenthilkarasu appreciate the post!

You may want to check out this post here.

There are few things to note:

  • PowerShell Task Type supports a local execution, so the shell script with interpretation is no longer needed.
  • PowerShell Modules must be installed as morpheus-local, or -scope allusers
  • Setting a JSON block in Cypher allows a singular reference to credentials for 1 or more vCenter at the same time
1 Like