7

I am getting following compilation issue in Powershell.

Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core because it is already added. Verify the name of the snap-in and try again.

The error clearly mentions that I need to verify the name of the snap-in. It was added successfully when i execute first time itself.

How to verify snap-in exist ,if not then add?

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • possible duplicate of [How to check if PowerShell snap-in is already loaded before calling Add-PSSnapin](http://stackoverflow.com/questions/1477994/how-to-check-if-powershell-snap-in-is-already-loaded-before-calling-add-pssnapin) – JohnC Apr 14 '14 at 11:00

2 Answers2

11

You can load it if it's not loaded already:

if(-not (Get-PSSnapin VMware.VimAutomation.Core))
{
   Add-PSSnapin VMware.VimAutomation.Core
}

You could also load it anyway and ignore the error:

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
0

I was getting the following errors and thought it was because the snapin was already loaded but it doesn't seem to be the case.

ERROR: The specified mount name 'vmstores' is already in use.
ERROR: The specified mount name 'vis' is already in use.

The solution Provided above is certainly much more simplistic than what I started writing below.

I do suppose the one contributing factor would be I look to see if the snapin is registered first.

$snaps1 = Get-PSSnapin -Registered
$snaps2 = Get-PSSnapin *VMWare -ErrorAction SilentlyContinue  

$vmsnap = 0

foreach ($snap1 in $snaps1) {
    if ($snap1.name -eq "VMware.VimAutomation.Core") {
        Write-Host "VM Snapin Registered..."
        $vmsnap = 1
        }
    }

if ($vmsnap -eq 0) {
    Write-Host "VMWare Snapin NOT Registered. Ensure the CLI is installed and available on machine."
}

if ($vmsnap -eq 1) {
    foreach ($snap2 in $snaps2) {
        if($snap2.name -eq "VMware.VIMAutomation.Core") {
            Write-Host "VMware Snapin Already Loaded..."
            $vmsnap = 2
            }
        }
    } 

if ($vmsnap -ne 2) {
    Write-Host "Loading VMware Snapin..."
Add-PSSnapin VMware.VimAutomation.Core 
}

granted I am still very very very new to PS syntax.

ATek
  • 815
  • 2
  • 8
  • 20