Automation

 View Only
  • 1.  Add-PSSnapin : An item with the same key has already been added

    Posted Oct 25, 2013 03:08 PM

    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



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

    Posted Oct 25, 2013 04:59 PM

    Which PowerCLI version are you using ?

    Do a

    Get-PowerCLIVersion

    This sounds like a bug that was solved recently.



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

    Posted Oct 25, 2013 11:08 PM

    Hi,

    I'm using PowerCLI 5.5 Relase 1 build 1295336.

    Thanks



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

    Posted Oct 25, 2013 11:17 PM

    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 ?



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

    Posted Oct 28, 2013 06:52 AM

    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



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

    Posted Oct 28, 2013 06:53 AM

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



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

    Posted Oct 29, 2013 02:34 PM

    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

    }



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

    Posted Oct 30, 2013 12:48 PM

    Thanks, I've already been using something similar:

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

            {

                Add-PSSnapin VMware.VimAutomation.Core

            }



  • 9.  RE: Add-PSSnapin : An item with the same key has already been added
    Best Answer

    Posted Nov 01, 2013 02:39 PM

    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.



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

    Posted Jul 26, 2016 09:30 PM

    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*