Hi, I want to auto-load all PowerCLI 6.0 modules into PowerShell ISE session (or standard PowerShell console). PowerCLI 6.0 was installed under my standard domain account with admin rights on my laptop. However I always run PowerShell console and ISE with run as and use my domain admin account. Having done so path to VMware modules was not automatically added to $env:PSModulePath variable. I created profile.ps1 file with the following content:
$env:PSModulePath = $env:PSModulePath + ";C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules"
Connect-VIServer vcenter -User myuser -Password mypassword
However Connect-VIServer cmdlet has not been recognized i.e. auto-load of VMware modules did not work. When I added line Import-Module VMware.VimAutomation.Core, before Connect-VIServer . . . the latter will work but I do not know why auto-load did not work since path to modules was added to $env:PSModulePath - Import-Module should be redundant step.
There is a known problem with the PowerCLI modules: LINK
What I do to prevent this is the following:
# Correcting the Environment Variable to the PowerCLI Module, due to a bug in the PowerCLI installer
#Save the current value in the $p variable.
$p = [Environment]::GetEnvironmentVariable('PSModulePath')
#Add the new path to the $p variable. Begin with a semi-colon separator.
$p += ';D:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules\'
#Add the paths in $p to the PSModulePath value.
[Environment]::SetEnvironmentVariable('PSModulePath',$p)
Import-Module -Name VMware.VimAutomation.Core
# Connect to the VMware vCenter Server
Connect-VIServer - Server vcenter -User myuser -Password mypassword
I have already done that. So Import-Module VMware.VimAutomation.Core is necessary. Pitty, this should be trivial since auto-loading of modules has been there since PowerShell v3. However VMware moved from snap-ins to modules for the first time in PowerCLI 6.0 so this might have something to do with this new concept.
Bojan, maybe here’s a clue for you: LINK
I will try later if autoloading works for me. The code what I posted is for a SMA runbook; so it’s PowerShell Workflow.
Yes, I have already read this but unfortunately in my case Connect-VIServer cmdlet would fail unless module is imported manually.
This is the code I use on my ISE profile (Microsoft.PowerShellISE_profile.ps1) to add the PowerCLI on it:
#VMware - Logon
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
“Connect on VMware PowerCLI”, {
Set-Location “C:\VMware”
$VMcred = Get-Credential (“$env:USERNAME$”)
Add-PsSnapin VMware.VimAutomation.Core -ea “SilentlyContinue”
Connect-VIServer -Server srv-01, srv-02, srv-03, srv-04 -Credential $VMcred
},
“Control+Alt+3”
)
#VMware - Logout
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
“Disconnect from VMware PowerCLI”, {
Disconnect-VIServer *
},
“Control+Alt+4”
)