PowerCLI

 View Only
  • 1.  PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 23, 2016 06:28 PM

    I had been running PowerCLI 5.8 R1; today I upgraded to 6.0 R3. And my PowerShell profile broke ... I used to use this, as my PowerShell profile:

    IF (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue")

    {

    # Running PowerCLI

         Connect-VIServer ....

    }

    ELSE

    {

    # NOT running PowerCLI

         Import-Module ActiveDirectory

    }

    This checked to see if VMware support was loaded, and - if so - connect me to my vCenter. If VMware support was not loaded, it would import various Microsoft-specific modules. Worked like a charm,

    However, since upgrading to PowerCLI 6.0 Release 3, this doesn't work. Which is odd, since that snapin still gets loaded, even in 6.0 R3, as shown by "Get-PSSnapin" after starting PowerCLI.

    I tried changing the test to Get-Module, and it still failed. I had it write out the list of loaded snapins/modules, and no VMware-related entries showed up.

    Basically, it looks like the profile is now being executed *before* the Initialize-PowerCLIEnvironment.ps1 (which actually loads all the VMware snapins and modules). And so the IF test will fail, as the snapins/modules are not yet loaded.

    Can anyone confirm? And - more to the point - how can I get it to do the old behavior (i.e., if VMware support is loaded, then automatically connect to vCenter; if not, load up other modules.



  • 2.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 23, 2016 06:34 PM

    MORE: I thought about perhaps checking for the command line parameters that were passed to PowerShell. For example, clicking on the PowerCLI icon actually executes this:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\" $true"

    I thought that if I could enumerate the parameters passed to Powershell.exe, then the 3rd parameter above is the C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\.

    And if so, then I am running PowerCLI, otherwise I am running stock PowerShell.

    But how do I access that parameter in my profile? Do I need to actually put in a PARAM statement in my profile? Or is that saved somewhere I can access it, like in an environmental variable or similar?



  • 3.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 23, 2016 07:24 PM

    I found out that yes, profiles execute before scripts, and so the modules aren't loaded. And it seems that auto-loading of modules (by calling a command in a module) doesn't seem to work in profiles, either.

    But this does work:

    IF ($Host.UI.RawUI.WindowTitle -like '*PowerCLI*')

      {

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

        Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -InvalidCertificateAction Ignore -Scope User -Confirm: $false

        Connect-VIServer ....

    }

    SO I check the window title, and execute the environment script myself; then the profile configurations work. It does mean that the environment script gets executed twice, but that isn't a showstopper.,



  • 4.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 23, 2016 07:52 PM

    I posted my current (PowerCLI 6.3) loading lines in  3.  Re: PowerCLI script to collect the vMotioned VM information

    Perhaps that helps ?



  • 5.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 24, 2016 12:38 PM

    Well, the problem is, that Get-Module shows *no* modules loaded by the profile. And apparently they don't properly auto-load, either (otherwise issuing a "Connect-VIServer" in my profile would auto-load the module first, then execute it. But auto-loading isn't working, and I have reports of the same failure to auto-load VMware-* modules on my Windows administration list, so it's not just me).

    So doing "$loaded = Get-Module -Name VMware* -ErrorAction SilentlyContinue ..." in the profile will return nothing, as no modules are loaded at the time the profile executes, and they're not auto-loading, either.

    So I just avoid the problem (for now) by executing the initialize-environment script in the profile myself, before having the profile issue any VMware related commands (such as Set-PowerCLIConfiguration or Connect-VIServer, etc.

    A bit brute-force, but it solves the problem.



  • 6.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 24, 2016 01:00 PM

    That is exactly the idea behind that 1st line, determine which modules are already loaded.

    And the next line will then load all the modules that are not yet loaded.



  • 7.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 24, 2016 01:19 PM

    Yes, the 2nd line loads anything not yet loaded (which is everything). So basically you're doing the same thing that the initialize environment script does - load the snapins and modules (minus whatever other initialization it does).

    That's not really that different from just calling the initialize environment script directly, as I do. :-)



  • 8.  RE: PowerCLI 6 - how to check that it is loaded, in Powershell profile

    Posted Mar 24, 2016 01:21 PM

    Also, remember that I want to load the VMware modules if I chose to start PowerCLI, and not load them everytime I just start Powershell itself. That's also why I needed to check the window title.