Automation

 View Only
  • 1.  Reducing number of powercli imports

    Posted 12 days ago

    I'm trying ideas to reduce the import of powercli modules in the hopes this will reduce the time logging in to a vcenter.

    I installed the powercli core module, plus vds, plus storage.

    I have this list of module folders:

    VMware.Vim
    VMware.VimAutomation.Cis.Core
    VMware.VimAutomation.Common
    VMware.VimAutomation.Core
    VMware.VimAutomation.Sdk
    VMware.VimAutomation.Storage
    VMware.VimAutomation.Vds

    If I do nothing special before logging in, and record any -verbose output, I get a count of 374 Import line mentions. About >90% are imports of cmdlets. To reduce the imports, some internet places said to try to import the core module before logging in, but that actually increases the count of import cmdlet lines to 717.

    My scripts are usually using just a handful of powercli commands and certainly not 300+ commands. Is there a way to reduce the number of imports to the bare minimum?



  • 2.  RE: Reducing number of powercli imports

    Posted 12 days ago

    Are you sure you only have those PowerCLI folders present?
    How did you install PowerCLI? And which version?
    Do you use Import-Module lines in your scripts?



    ------------------------------


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


    ------------------------------



  • 3.  RE: Reducing number of powercli imports

    Posted 12 days ago

    I did have a couple of more modules, which I deleted to make it a smaller test.

    The install was by using a yum repo: https://packages.microsoft.com/rhel/9.0/prod/
    The version installed is: 7.5.0

    I have this test case below that demonstrates the number of cmdlets after an import of core. The import part seems fast now (0.5 seconds) but the connect is about 3-4 seconds. Is this typical? Is it possible just load individual cmdlets (like connect-viserver, get-vm)?

    Sample code:

    #!/bin/pwsh
    
    $server = "****.domain"
    $credpath = "cred.xml"
    write-output("Time of import cli")
    $time = measure-command { $cred = import-clixml -path $credpath}
    write-output("$time")
    write-output("Time of import-module")
    $time = measure-command { import-module VMware.VimAutomation.Core }
    write-output("$time")
    
    write-output("Count cmdlet for Vim")
    get-command -Module VMware.Vim -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Cis.Core")
    get-command -Module VMware.VimAutomation.Cis.Core -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Common")
    get-command -Module VMware.VimAutomation.Common -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Core")
    get-command -Module VMware.VimAutomation.Core -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Sdk")
    get-command -Module VMware.VimAutomation.Sdk -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Storage")
    get-command -Module VMware.VimAutomation.Storage -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Vds")
    get-command -Module VMware.VimAutomation.Vds -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    write-output("Count cmdlet for Utility")
    get-command -Module Microsoft.PowerShell.Utility -CommandType Cmdlet | measure-object | select-object -expandproperty Count
    
    write-output("
    List of Modules loaded")
    (get-Module).Name
    write-output("
    List available modules")
    (get-module -listavailable).Name
    
    write-output("
    Time of connect")
    $time = measure-command {$conn = (connect-viserver -server $server -credential $cred) }
    write-output("$time")
    $conn.name

    Sample Output from a run. 

    [root@***** ~]# time ./test.ps1
    
    Time of import cli
    00:00:00.0081103
    Time of import-module
    00:00:00.5633627
    Count cmdlet for Vim
    0
    Count cmdlet for Cis.Core
    3
    Count cmdlet for Common
    6
    Count cmdlet for Core
    339
    Count cmdlet for Sdk
    1
    Count cmdlet for Storage
    146
    Count cmdlet for Vds
    32
    Count cmdlet for Utility
    114
    
    List of Modules loaded
    Microsoft.PowerShell.Management
    Microsoft.PowerShell.Utility
    VMware.Vim
    VMware.VimAutomation.Cis.Core
    VMware.VimAutomation.Common
    VMware.VimAutomation.Core
    VMware.VimAutomation.Sdk
    VMware.VimAutomation.Storage
    VMware.VimAutomation.Vds
    
    List available modules
    VMware.Vim
    VMware.VimAutomation.Cis.Core
    VMware.VimAutomation.Common
    VMware.VimAutomation.Core
    VMware.VimAutomation.Sdk
    VMware.VimAutomation.Storage
    VMware.VimAutomation.Vds
    Microsoft.PowerShell.Archive
    Microsoft.PowerShell.Host
    Microsoft.PowerShell.Management
    Microsoft.PowerShell.PSResourceGet
    Microsoft.PowerShell.Security
    Microsoft.PowerShell.Utility
    PackageManagement
    PowerShellGet
    PSReadLine
    ThreadJob
    
    Time of connect
    00:00:03.1398126
    ****.domain
    
    real	0m4.693s
    user	0m3.860s
    sys	0m0.637s



  • 4.  RE: Reducing number of powercli imports

    Posted 12 days ago

    The version of powercli according to the get-module is 8.3.0 (for vim), and 13.3.0 (for the remaining modules)




  • 5.  RE: Reducing number of powercli imports

    Posted 12 days ago

    I was asking for the PowerCLI version, not the PS version.

    Note that PS has the auto-load feature. From the Get-Module help page
    "Modules are imported automatically on first use and you can use the Import-Module cmdlet to import them."
    And on the Import-Module help page
    "Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module."

    So when the Connect-VIServer is the 1st cmdlet, you might have the overhead of loading the module (that contains the Connect-VIServer cmdlet).

    You might want to verify if the Connect-VIServer takes as long when you first explicitly import the module

    Import-Module -Name VMware.VimAutomation.Core
    
    Measure-Command {$conn = (Connect-VIServer -Server $server -Credential $cred) }

    You might also compare the execution times from consecutive invocations of the Connect-VIServer cmdlet.

    Also note that the Connect-VIServer cmdlet has some overhead due to protocol negotiations, and certificate verification, ...



    ------------------------------


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


    ------------------------------



  • 6.  RE: Reducing number of powercli imports

    Posted 12 days ago

    I was able to reduce the actual get-command counts for the vmware modules if I used this style: import-module vmware.vimautomation.core -cmdlets connect-viserver,get-vm,set-powercliconfiguration. But it did nothing to reduce the first vcenter connection. I also tried without the 'cmdlets' portion and the timing was still the same.
    When I tested re-connecting in the same session (connect-viserver, disconnect-viserver), it does reduce the time from ~4 seconds to 1 second on the later connections. So there is some ~3 sec overhead on the first connection besides the actual connecting.
    Does this timing match up with what you experience? What is typical?




  • 7.  RE: Reducing number of powercli imports

    Posted 12 days ago

    I can't really provide absolute numbers, since every environment is different.
    But the ratio between the 1st and 2nd connection seems to be of the same magnitude that I'm seeing.



    ------------------------------


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


    ------------------------------



  • 8.  RE: Reducing number of powercli imports

    Posted 12 days ago

    Thanks LucD