Working with VMware vCenter using Powershell and TabExpansionPlusPlus module

Working with VMware vCenter using PowerShell and TabExpansionPlusPlus module

If you read my previous blog posts, you will know I created some Tab Completion scripts to help me with managing ADUser, Groups and vCenter Permissions.

I’m working recently with View Horizon and need to manage few master images and other Virtual machines, I need to change memory, power on, off, open console etc. It takes a while if you doing this from Web Client but is much faster from within PowerShell window.

VMware PowerShell module VMware.VimAutomation.Core contains many commands which share same parameter name VM

Get-Command -Module vmware.vimautomation.core -ParameterName VM -CommandType Cmdlet
VMCommands.jpg

VMware module commands with VM as parameters

Because of that one parameter, I can create Tab completion script to use with all of them. After we connect to vCenter server PowerShell will store information about vCenter in $global variable

DefaultvCenterServer

PowerShell variable with vCenter server information

There are many information stores within this variable, but I will focus on default information which just server name to use it in my Tab completion script block

$AVMNname = {
    param ($CommandName,
        $ParameterName,
        $VMToComplete)
    
    $VMNames = Get-VM -Server $global:DefaultVIServer | Where-Object -FilterScript { $PSItem.name -match $VMToComplete }
    
    foreach ($VMName in $VMNames)
    {
        New-CompletionResult -CompletionText $VMName.Name -ToolTip $VMName.PowerState
    }
}

Next, I have added variable with all cmdlet from VMware module with parameter VM, and use Foreach to add all commands to Register-ArgumentCompleter command

$VMNameCmds = Get-Command -Module VMware.VimAutomation.Core -ParameterName VM -CommandType Cmdlet
foreach ($VMNameCmd in $VMNameCmds)
{
    Register-ArgumentCompleter -CommandName @($VMNameCmd) -ParameterName VM -ScriptBlock $AVMName
}

When all above command is loaded to PowerShell memory we can run all the commands and when specify VM parameters get a list of Virtual Machines.

VMCommands

Running vCenter command with tab completion

Now we can find Virtual machine we want work with faster and easier.

Thank you for reading.