Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

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.
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

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

Avatar of janhoedt
janhoedt

ASKER

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.
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.
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?
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.
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".
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?
ASKER CERTIFIED SOLUTION
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial