vMarkusK1985
Expert
Expert

Properly load PowerCLI SnapIn or Module

 

Hello,

 

With the new PowerCLI Version I need to write a new loading section for my Scripts. The loading part should work with all PowerCLI Versions.


#region: Load VMware Snapin or Module (if not already loaded)

if (!(Get-Module -Name VMware.VimAutomation.Core) -and (Get-Module -ListAvailable -Name VMware.VimAutomation.Core)) {

    Write-Output "loading the VMware COre Module..."

    if (!(Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {

        # Error out if loading fails

        Write-Error "`nERROR: Cannot load the VMware Module. Is the PowerCLI installed?"

     }

    $Loaded = $True

    }

    elseif (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -and !(Get-Module -Name VMware.VimAutomation.Core) -and ($Loaded -ne $True)) {

        Write-Output "loading the VMware Core Snapin..."

     if (!(Add-PSSnapin -PassThru VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {

     # Error out if loading fails

     Write-Error "`nERROR: Cannot load the VMware Snapin or Module. Is the PowerCLI installed?"

     }

    }

#endregion

Any recommendations for enhancement?

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
15 Replies
LucD
Leadership
Leadership

Is that logic for the core module in the first part not working with 6.5R1?


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

0 Kudos
vMarkusK1985
Expert
Expert

It is workig. But I am not sure that this is the most efficient way...

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos
LucD
Leadership
Leadership

I don't immediatly see how to improve the module part, especially since you want it to work for multiple PowerCLI versions.

You could skip the PSSnapin part altogether if you are dealing with PowerCLI 6.5R1 or higher.

A Get-PowerCLIVersion could do that.


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

vMarkusK1985
Expert
Expert

OK, thanks LucD‌.

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos
vXav
Expert
Expert

I have a function in my Powershell profile to customize the prompt and one to connect to vcenter.

I used to add the snapin in the Connect-vCenter function but I have to change that:

  • Apparently the snapin is not supported anymore in PowerCLI 6.5 => Use of "Import-Module VMware.VimAutomation.Core" instead of "add-pssnapin"
  • For some reason Import-Module VMware.VimAutomation.Core resets the prompt => Moved Import-module out of the function to load everytime and placed it before the Prompt function



The module is imported even if I don't need it but at least I keep my custom prompt and the overhead at the start of PowerShell is less than 1 sec on my pc so ...

0 Kudos
vMarkusK1985
Expert
Expert

Hello vXav‌,

this Section should be added to all of my Scripts shared to colleagues. This Scripts are executed in different enviroments with different PowerCLI versions.

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos
vXav
Expert
Expert

Yes my reply wasn't a solution to your problem, more just sharing the issue I faced after upgrading to PowerCLI 6.5. In case someone had it too Smiley Happy

0 Kudos
Brennison22
Contributor
Contributor

This VMware load modules script works for me.

if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
    if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
        $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
      
    } else {
        $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
    }
    . (join-path -path (Get-ItemProperty  $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
}
if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
    Write-Host "VMware modules not loaded/unable to load"
    Exit 99
}


0 Kudos
Kent1295
Contributor
Contributor

I just upgraded to PowerCLI 6.5 and this script can not file   "VMware.VimAutomation.Core"

which generates the error: "Cannot load the VMware Module. Is the PowerCLI installed?"

PowerCLI is installed and I am able to run it from the command line. I installed from the VMware site.

Any Ideas?

0 Kudos
LucD
Leadership
Leadership

With 6.5R1 it became a lot easier, you can try

Get-Module -Name VMware* -ListAvailable | Import-Module


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

Kent1295
Contributor
Contributor

That did it !!!!

Thanks.

0 Kudos
cvtz
Contributor
Contributor

I just upgrade my PowerCLI to 6.3 from PowerCLI 5.8 I tried the following code to load the modules but it doesn't seem to work. Any input would be nice. Thanks!

if (!([Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole] "Administrator")) {

          [void][System.Windows.Forms.MessageBox]::Show("This application must be ran using an account with Admin rights.")

          exit

     } else { $erroractionpreference = "SilentlyContinue"

    # PowerCLI 6.5 uses posh modules exclusively.  Check if modules are available and if not load snapin for ver 5.8 or earlier...if installed at all.

     $PCliModule = Get-Module -ListAvailable vmware.vimautomation.core

     if ($PCliModule -ne $null) {

               Import-Module vmware.vimautomation.core -ea SilentlyContinue -wa SilentlyContinue

               # add result to var so can log it once up and running.

               $script:PCLIver = Get-PowerCLIVersion

     } else {

               $Snapin = "vmware.vimautomation.core"

               if (Add-Snapin -Snapin $Snapin) {

                              set-PowerCLIConfiguration -InvalidCertificateAction "Ignore" -Confirm:$false

                              $script:PCLIver = Get-PowerCLIVersion

                              } else {

                                        [void][System.Windows.Forms.MessageBox]::Show("This application requires $Snapin to be installed.?")

                                        exit

                              }

0 Kudos
LucD
Leadership
Leadership

Why PowerCLI 6.3?


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

0 Kudos
cvtz
Contributor
Contributor

No reason why LucD. Just any version newer than 5.8 seems to have this issue.

0 Kudos
LucD
Leadership
Leadership

If your vSphere version is compatible, I would strongly suggest to go for 6.5.4, the latest version.

That is all modules, and if you are also using PowerShell 3.* or higher, the module autoload feature takes all the loading hassle away.

For the installation see Welcome PowerCLI to the PowerShell Gallery – Install Process Updates for installation instructions.


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

0 Kudos