VMware Cloud Community
enclsoed
Contributor
Contributor

Calling Initialize-PowerCLIEnvironment.ps1 causes variable leak

Hi!

I've noticed that when I'm loading PowerCLI modules (as suggested in this blog) some of the variables in my script get rewritten. Here's a simplified example (I'm using PowerCLI 6.3R1):

$Configuration = 'Something valuable'

if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {

   . “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”

$Configuration | fl *

Returns:

DefaultVIServerMode         : Multiple

ProxyPolicy                 : UseSystemProxy

ParticipateInCEIP           : True

CEIPDataTransferProxyPolicy : UseSystemProxy

DisplayDeprecationWarnings  : True

InvalidCertificateAction    : Unset

WebOperationTimeoutSeconds  : 300

VMConsoleWindowBrowser      :

Scope                       : Session

I was wondering if there is another way to do this or if there are any plans to introduce a module that could be loaded using Import-module instead of dot-sourcing to avoid variable leakage? That module could offer a slightly different experience that could suit better for embedding into other scripts (i.e. avoid things like, rewriting PSHost title, changing PS prompt, changing location, etc.)

Thanks!

Tags (1)
0 Kudos
1 Reply
LucD
Leadership
Leadership

That $configuration variable is unfortunately also used in the init script

As was announced at VMworld last week, the next version will be modules only.

Then auto-oading should be possible.

For now, you could use my Enable-PowerCLI  function to load the modules and PSSnapin.

See Universal PowerCLI Loader


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos