Are you using PowerCLI to automate or perform daily administrative tasks in your virtual infrastructure? VMware vSphere PowerCLI is snapin module for Microsoft PowerShell command-shell and scripting language which you often see installed on latest Microsoft Operating Systems. PowerCLI comes with powerful feature to create and manage aliases for built-in cmdlets for creating and managing virtual resources. In this tutorial, I will show you how to create, export, import and manage your aliases. Aliases are often helpful when you are performing repetitive tasks/commands within Powershell or PowerCLI or if you just despise typing a dash in every single cmdlet! You will need Powershell and PowerCLI installed on your workstation for commands below to work properly.
Connect to your vSphere ESXi or vCenter server using the “connect-viserver” cmdlet.
Once connected, issue the follow cmdlet to get list of all of the configured aliases:
[vSphere PowerCLI] C:\>get-alias
You can also use “gal” (alias for get-alias) cmdlet to get list of aliases.
[vSphere PowerCLI] C:\>gal
In this example, we will create two aliases for existing cmdlets: get-vm and get-host
[vSphere PowerCLI] C:\> New-Item Alias:gvm -Value Get-VM
[vSphere PowerCLI] C:\> gvm
[vSphere PowerCLI] C:\> New-Item Alias:gvh -value Get-VMHost
[vSphere PowerCLI] C:\> gvh
Export and Import Aliases
When you configure aliases in PowerShell, they are only available for that particular session and are deleted once the session ends. You may export aliases using the export cmdlet or edit your PowerShell profile script file to add your additions. To see location of your profile, issue $profile at the command prompt:
[vSphere PowerCLI] C:\> $profile
Use the Export-Alias cmdlet to export aliases into the PowerShell script file above:
[vSphere PowerCLI] C:\> export-alias
cmdlet Export-Alias at command pipeline position 1
Supply values for the following parameters:
[vSphere PowerCLI] C:\>
You can also edit the “Microsoft.PowerShell_profile.ps1″ file directly, add your aliases and import using the import-alias cmdlet
[vSphere PowerCLI] C:\> import-alias
The process is similar to export-alias cmdlet, you may encounter errors for aliases which were already imported when you started the PowerShell/PowerCLI session.
You can learn more about PowerCLI, at Vmware’s PowerCLI documentation page.
I hope this helps you manage your VMware vsphere environment better and saves you time. Please keep in mind that above cmdlets may or may not work in your environment, so always test in non-production environment first as I am not responsible for your mistakes.
Discuss this and other virtualization management ideas, techniques in our virtualization forum.