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:
- Install PowerShell Core (PWSH):
- Install PowerShell Core on all Morpheus appliance nodes.
- Open a terminal and type
pwsh
to access the PowerShell Core console.
- 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:
- Navigate to Morpheus UI:
- Log in to the Morpheus UI with appropriate credentials.
- 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.”
- 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.