1 2 Previous Next 15 Replies Latest reply on Feb 23, 2018 7:28 AM by LucD

    Properly load PowerCLI SnapIn or Module

    vMarkusK1985 Hot Shot
    vExpert

       

      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?

        • 1. Re: Properly load PowerCLI SnapIn or Module
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

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

          • 2. Re: Properly load PowerCLI SnapIn or Module
            vMarkusK1985 Hot Shot
            vExpert

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

            • 3. Re: Properly load PowerCLI SnapIn or Module
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              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.

              1 person found this helpful
              • 5. Re: Properly load PowerCLI SnapIn or Module
                vXav Hot Shot
                vExpert

                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 ...

                • 6. Re: Properly load PowerCLI SnapIn or Module
                  vMarkusK1985 Hot Shot
                  vExpert

                  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.

                  • 7. Re: Properly load PowerCLI SnapIn or Module
                    vXav Hot Shot
                    vExpert

                    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

                    • 8. Re: Properly load PowerCLI SnapIn or Module
                      Brennison22 Lurker

                      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
                      }


                      • 9. Re: Properly load PowerCLI SnapIn or Module
                        Kent1295 Lurker

                        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?

                        • 10. Re: Properly load PowerCLI SnapIn or Module
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

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

                           

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

                           

                          • 11. Re: Properly load PowerCLI SnapIn or Module
                            Kent1295 Lurker

                            That did it !!!!

                            Thanks.

                            • 12. Re: Properly load PowerCLI SnapIn or Module
                              cvtz Lurker

                              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

                                                            }

                              • 13. Re: Properly load PowerCLI SnapIn or Module
                                LucD Guru
                                User ModeratorsCommunity WarriorsvExpert

                                Why PowerCLI 6.3?

                                • 14. Re: Properly load PowerCLI SnapIn or Module
                                  cvtz Lurker

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

                                  1 2 Previous Next