PowerCLI Get-VICommand function error repaired
February 24, 2013 Leave a comment
On 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.