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\Infrastructur e\PowerCLI \Scripts\I nitialize- PowerCLIEn vironment. ps1') { . "C:\Program Files (x86)\VMware\Infrastructur e\PowerCLI \Scripts\I nitialize- PowerCLIEn vironment. ps1" }
else { Add-PSSnapin -Name 'VMware.VimAutomation.Core ' }
}
Thanks for your input.
J.
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\Infrastructur
else { Add-PSSnapin -Name 'VMware.VimAutomation.Core
}
Thanks for your input.
J.
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.
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
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.
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
}
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.
ASKER
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?
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.
Import-Module VMWare.VimAutomation.Core
Then use your commands.
ASKER
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".
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".
ASKER
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?
Add-PSSnapin -Name 'VMware.VimAutomation.Core
but will that work on every version of Powershell?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to check to see if the module is loaded you can use something like below:
Open in new window