VMware Cloud Community
DBADale
Contributor
Contributor
Jump to solution

Add-PSSnapin : An item with the same key has already been added

When running Add-PSSnapin I get the above error. This only seems to happen if I attempt to add the snapin a second time, however when this happens none of the Power CLI commands are available then until the server is rebooted.

Add-PSSnapin : An item with the same key has already been added.

At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

+     Add-PSSnapin $SnapinName;

+     ~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Add-PSSnapin], ArgumentExcept

   ion

    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co

   mmands.AddPSSnapinCommand

Attempts to remove the snap in at this point fail with:

Remove-PSSnapin : No Windows PowerShell snap-ins matching the pattern

'VmWare.VimAutomation.Core' were found. Check the pattern and then try the

command again.

At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

+     Remove-PSSnapin $SnapinName;

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (VmWare.VimAutomation.Core:Stri

   ng) [Remove-PSSnapin], PSArgumentException

    + FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.R

   emovePSSnapinCommand

I've already tried un-installing the dll and reinstalling it, that doesn't make any difference

    $dllpath = "C:\Program Files (x86)\Vmware\Infrastructure\vSphere PowerCLI\VMWare.VimAutomation.VICore.Cmdlets.dll"

    $SnapinName = "VmWare.VimAutomation.Core"

    #get the path for instalutil

    $path = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory();

    #create an alise for installtuil

    set-alias installutil (resolve-path (join-path $path "installutil.exe"));

    #uninstall the snapin

    installutil /u $dllpath;

    installutil $dllpath;

I've tried unloading the appdomain and then adding the snapin with the same result:

$appdomain = [AppDomain]::CurrentDomain

    $appdomain.Unload #| Out-Null

Add-PSSnapin "VMware.VimAutomation.Core"

$appdomain.Load

Simply skipping past the error is no good because subsequent commands can't be found:

Add-PSSnapin "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue

Get-VM $VirtualMachine

The term 'Get-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Any help resolving this would be greatly appreciated.

Thanks

1 Solution

Accepted Solutions
DBADale
Contributor
Contributor
Jump to solution

Ok, I've kind of worked out what was going wrong here.

The snapin was being loaded from within a powershell module. When the module is removed and added again, the snapin stops functioning but also cannot be added again.

If the snapin is loaded first outside of the module everything works correctly as long as you never try to remove or add the snapin again within the module.

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion

This sounds like a bug that was solved recently.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DBADale
Contributor
Contributor
Jump to solution

Hi,

I'm using PowerCLI 5.5 Relase 1 build 1295336.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you by any chance have a folder called "VMware.VimAutomation.Core" in one of the PS module folders ?

You mention you try to add the snapin a 2nd time, I trust you did a Remove-PSSnapin first ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DBADale
Contributor
Contributor
Jump to solution

Hi,

Yes, although remove-pssnapin also errors with:

Remove-PSSnapin : No Windows PowerShell snap-ins matching the pattern

'VmWare.VimAutomation.Core' were found. Check the pattern and then try the

command again.

At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

+     Remove-PSSnapin $SnapinName;

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (VmWare.VimAutomation.Core:Stri

   ng) [Remove-PSSnapin], PSArgumentException

    + FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.R

   emovePSSnapinCommand


Thanks

Reply
0 Kudos
DBADale
Contributor
Contributor
Jump to solution

Forgot to mention the folders, there are no folders with the same name as the snapin the module folders.

Reply
0 Kudos
Chrigoli
Enthusiast
Enthusiast
Jump to solution

Don't know if this helps, but I've added a check if the SnapIn is already loaded.

# Add in the VI Toolkit

if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )

{

Add-PSsnapin VMware.VimAutomation.Core

}

Reply
0 Kudos
DBADale
Contributor
Contributor
Jump to solution

Thanks, I've already been using something similar:

     if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue))

        {

            Add-PSSnapin VMware.VimAutomation.Core

        }

Reply
0 Kudos
DBADale
Contributor
Contributor
Jump to solution

Ok, I've kind of worked out what was going wrong here.

The snapin was being loaded from within a powershell module. When the module is removed and added again, the snapin stops functioning but also cannot be added again.

If the snapin is loaded first outside of the module everything works correctly as long as you never try to remove or add the snapin again within the module.

HD_haitu
Contributor
Contributor
Jump to solution

I wanted to add another alternative solution that worked for me.

So I had the error that the OP stated, where I had Add-PSSnapin VMWare* in my module, however, could not actually get the module working even though the key was in there.

The easy fix was just adding the line at the end of your module.

So it would look like this in YOUR.PSM1

function Get-Foo

{

    [CmdletBinding()]

    param(...)

    BEGIN {...}

    PROCESS {...}

    END{...}

}

function Get-Foo_Too

{

    [CmdletBinding()]

    param(...)

    BEGIN {...}

    PROCESS {...}

    END{...}

}

Add-PSSnapin VMWare*

Reply
0 Kudos