Powershell CLI howto load correctly in Powershell

Hi,

How do I quickly check if Powercli module is loaded, else load it (the remote one is Powercli 6.5, local one 5.5)?
Tried this to have as well loaded the snappin when available or the module when there but doesn't seem logical to me.
Would just like to check if  ! (get-Powerclimodule) the load it, if not there then give an error.

What I got so far.

if (! $(Get-PowerCLIVersion))
{
if (Test-Path 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1') { . "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" }
                  else { Add-PSSnapin -Name 'VMware.VimAutomation.Core' }
}

Thanks for your input.
J.
janhoedtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
In PowerShell 3.0 and above when you use a cmdlet in a module the module autoloads.

If you want to check to see if the module is loaded you can use something like below:

$var = Get-Module | Where Name -eq "PowerCLIModulename"
if($var -eq $null) {
     "No module loaded"
}
else {
     "Module Loaded"
}

Open in new window

0
janhoedtAuthor Commented:
Duh. That s exactly what I need to know = what the name of the module is, "some module" does not help.
Need to know what the general way/module name is to see if powercli is loaded.
0
DBAduck - Ben MillerPrincipal ConsultantCommented:
No need to be rude. In 6.0 there is an MSI that installs the modules and the path may be relevant. In the new world, the PowerCLI module is distributed via the PowerShell Gallery and can be installed with Install-Module as illustrated in the link below.

If you installed via the MSI for this module, the $PSModulePath will have a VMWare path in it, so you could check that, or you could just run Get-PowerCLIVersion and have it autoload if you are in PowerShell 3.0.  Or you can use

$exists = Get-Command "Get-PowerCliVersion"
if($exists) {
     $true
}
else {
    $false
}

Open in new window


Take a look here.  The module is named VMWare.PowerCLI.
https://www.powershellgallery.com/packages/VMware.PowerCLI/6.5.1.5377412

Here is the blog about the module, I hope this will help you.  But in the current module there is no command called
https://blogs.vmware.com/PowerCLI/2017/04/powercli-install-process-powershell-gallery.html

Or you can look here for a solution to just try { } catch { } it.
http://www.kellestine.com/load-powercli-modules/


I hope that is helpful. I don't have the version 5.5 installed. If this is not helpful, just let me know and I can install that version and look at another solution for you.
1
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

janhoedtAuthor Commented:
Sorry for that, was rude indeed.
Thanks for your input. I tried the get-powercli version in my script, but that makes the script fail (done it throws an error).
Actually I need just some basic get-vm and snapshot manipulations but loading the full module takes not of time. Isn't there a way to load parts of module only?
0
DBAduck - Ben MillerPrincipal ConsultantCommented:
those commands are in the VMWare.VimAutomation.Core module and it loads much faster than the entire VMWare.PowerCLI module.

Import-Module VMWare.VimAutomation.Core

Then use your commands.
0
janhoedtAuthor Commented:
Well, that's the reason I initially create the post.
import-module VMWare.VimAutomation.Core is giving an error on the destination host.

So what I would like to have is a solid proof "check if there is a powercli loaded, if not, load one that's there".
0
janhoedtAuthor Commented:
I guess the crorrect command is Get-PSSnapin -Name 'VMware.VimAutomation.Core'  if not there then
 Add-PSSnapin -Name 'VMware.VimAutomation.Core'
but will that work on every version of Powershell?
0
DBAduck - Ben MillerPrincipal ConsultantCommented:
The problem is that there is a version that is Snapins and it is installed with an installer (MSI) and there is the new version that is in the PowerShell Gallery that you can get with Install-Module if you are on PowerShell 5.0 or 5.1 (Look at $PSVersionTable for the PowerShell version)

If you are using the MSI one that has Snapins then you would use Get-PSSnapin and if you are using the module then you would use Get-Module.

So if you need solid proof, then the first thing you do is to check to see if the Module is present --  Test this out and see if that will help.

if( (Get-Module -ListAvailable VMWare.VimAutomation.Core) ) {
        $module = Get-Module | Where { $_.Name -eq "VMWare.VimAutomation.Core" }
        if($module) {
                "Module is loaded"
        }
        else {
                Import-Module VMWare.VimAutomation.Core
        }
}
else {
        $snap = Get-PsSnapin -Registered -Name VMWare.VimAutomation.Core -EA SilentlyContinue
        if($snap) {
              $snap2 = Get-PsSnapin -Name VMWare.VimAutomation.Core -EA SilentlyContinue
              if($snap2) {
                      "Snapin Loaded"
              }
              else {
                      Add-PsSnapin VMWare.VimAutomation.Core
              }
        }
        else {
                   "Snapin not installed"
        }
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.