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?
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
It is workig. But I am not sure that this is the most efficient way...
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
OK, thanks LucD.
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:
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 ...
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.
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
}
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?
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
That did it !!!!
Thanks.
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
}
Why PowerCLI 6.3?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No reason why LucD. Just any version newer than 5.8 seems to have this issue.
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