Powershell / PowerCLI - Slow on first run

Hello! This is my first post. :slight_smile:
I have a script, which only consists in connecting the server vcenter and run a simple command and then disconnect.
The script takes about 25~35 seconds to load the powershell variables, connect to vCenter, run the command Get-VMHost and finally disconnect.
When you run the script again the process goes much faster. About 5 seconds.
If I leave the PowerShell and enter again, the script remains slow on the first run.
What to see this slow on first run? Is there any way to overcome this problem so that whenever the get-VMHost command runs the process will be fast?

The script:

Add-PSSnapin VMware* Set-PowerCLIConfiguration -DefaultVIServerMode multiple -InvalidCertificateAction Ignore -Scope Session -ProxyPolicy NoProxy -Confirm:$false | Out-Null

Measure-Command {
Connect-VIServer 10.8.3.87 -Protocol https -Port 443 -User root -Password 123456
Write-Host “Time do connection with vCenter”
} | fl TotalSeconds

Measure-Command {
get-VMHost -Location $Clusters -State Connected| Format-Table -HideTableHeaders Name | Out-String
Write-Host "Time to run get-VMHOST "
} | fl TotalSeconds

Firts Result:
TotalSeconds : 11,1788088
TotalSeconds : 14,0271155

Second Result (without exit powershell):
TotalSeconds : 2,59300022
TotalSeconds : 0,05405447

Third Result (after exit Powershell and enter again)
TotalSeconds : 12,1127486
TotalSeconds : 13,6543578

You don’t mention the version of PowerShell you’re using?

PS C:\scripts> get-host

Name : ConsoleHost
Version : 4.0
InstanceId : 5d6b7e3c-03a8-4846-b456-455009e7184a
UI : System.Management.Automation.Internal.Host.InternalHostU
CurrentCulture : pt-BR
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace

PS C:\scripts> get-powercliversion

PowerCLI Version

VMware vSphere PowerCLI 5.1 Release 2 build 1012425

Snapin Versions

VMWare AutoDeploy PowerCLI Component 5.1 build 768137
VMWare ImageBuilder PowerCLI Component 5.1 build 768137
VMware vCloud Director PowerCLI Component 5.1 build 1012427
VMware License PowerCLI Component 5.1 build 669840
VMware VDS PowerCLI Component 5.1 build 1012428
VMware vSphere PowerCLI Component 5.1 build 1012428

Well, PowerCLI I can’t tell you anything about (VMware maintains a forum for those - there’s a sticky post in this forum with the link), but PowerShell itself does some compiling kinda stuff with scripts, which on first execution could induce a brief hit. It’s hard for me to know if it’s that (it usually isn’t noticeable) or something in the PowerCLI stuff.

There is a global variable that is used when you connect to a vCenter server (I think it is DefaultVIServersor something like that) and it stores the connection information for the server. The first time takes a while because it has to initiate the new connection while the second time goes quicker because the connection is already saved to the global variable. Ditto when you close PowerShell out and restart the console; it has to re-create the connection to the vCenter server.

If you were to use Disconnect-VIServer <servername> and then attempt to reconnect, I imagine the time would go back up to its 1st run time.