PowerCLI Get-VICommand function error repaired

PowerCLI logoOn my PC (Windows 8 Pro, Windows PowerShell 3 and VMware vSphere PowerCLI 5.1 Release 2) there is a very annoying problem with the Get-VICommand function. If I use this function without specifying the name of a cmdlet to search for, then I get an “Object reference not set to an instance of an object” error message:

PowerCLI C:\users\robert> Get-VICommand
get-command : Object reference not set to an instance of an object.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1:68 char:3
+   get-command -pssnapin VMware.* -Name $Name
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand

Listing 1. Get-VICommand error message with PowerShell 3.

As you can see in the error message, the error is thrown by the Get-Command cmdlet. This is because Get-VICommand is a function that calls the Get-Command cmdlet. The function is defined in the Initialize-PowerCLIEnvironment.ps1 file. You can see the complete path to this file in the above error message.

function global:Get-VICommand([string] $Name = "*") {
  get-command -pssnapin VMware.* -Name $Name

Listing 2. Original version of the Get-VICommand function.

The Get-VICommand function calls the Get-Command cmdlet with a wildcard for the -pssnapin parameter value. This is what causes the problem. For some reason this does not work on my PC. Probably because of some other conflicting software that I have installed. I have not found yet what exactly causes this problem.

To work around this problem I created a new version of the Get-VICommand function that enumerates all the VMware snapins and calls Get-Command for each of them.

function global:Get-VICommand([string] $Name = "*") {
  &{ foreach ($PSSnapin in (Get-PSSnapin -Name vmware.*))
       Get-Command -PSSnapin $PSSnapin -Name $Name -ErrorAction:SilentlyContinue
  } |
  Sort-Object -Property Name

Listing 3. New version of the Get-VICommand function.

If you have this problem also then put the new version of the Get-VICommand function in a file called Get-VICommand.ps1. You can then load the function to your PowerCLI session by dot sourcing the file. For example:

PowerCLI C:\> . .\Get-VICommand.ps1

Listing 4. Load the function in the Get-VICommand.ps1 file to your PowerCLI session by dot sourcing.

Now you can use the new Get-VICommand function in your PowerCLI session.

Have fun with PowerCLI!

Update 24-2-2013
While searching for the root cause of this problem, I discovered that you can also solve it by disabling PowerShell v3 module auto loading. You can do this with the following command:

$PSModuleAutoloadingPreference = 'none'

Listing 4. PowersShell v3 command to disable module auto loading.

Put this command in your PowerShell profile to disable PowerShell v3 module auto loading for all your PowerShell sessions. You can edit your profile with:

notepad $profile

Listing 5. PowerShell command to edit your PowerShell profile.

The disadvantage of disabling PowerShell module auto loading is that you have to load all the modules you use explicitly using the Import-Module cmdlet. The advantage is that the Get-VICommand function and Get-Command cmdlet are working again.


About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-Cloud, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016 and 2017 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. PernixData made him in 2015 a member of the PernixPro.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: