Reputation: 18147
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?
Upvotes: 7
Views: 8598
Reputation: 825
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.
Upvotes: 0
Reputation: 126732
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
Upvotes: 11