Home > Blogs > VMware PowerCLI Blog


How to speed-up the execution of the first PowerCLI cmdlet

Some users may experience delays on the first run of a PowerCLI cmdlet for each new PowerShell session. This delay is more noticeable when running on a 64-bit OS. You can see this with a simple script that measures the run time:

Connect-VIServer …

Measure-Command { Get-VMHost } | fl TotalSeconds

Measure-Command { Get-VMHost } | fl TotalSeconds

The first run of a Get-VMHost will be slower than the second one. This will only happen once in a PowerShell process.

Here are some results:

On a 64-bit OS:

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 23.4781061

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 1.0336202

On a 32-bit OS:

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 8.578462

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 1.1123916

This issue is due to the fact that the .Net framework compiles the underlying code on first use.

To improve this, by precompiling, you can run the following commands at your command prompt (you need to run the prompt as administrator):

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService40.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService25.XmlSerializers, Version=2.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

If you are running on 64-bit OS, you need to run the following as well:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService40.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService25.XmlSerializers, Version=2.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

Note that you need to do this only once for a PowerCLI installation. It is persisted even across OS restarts. The precompiled assemblies are stored in GAC, that is, all users and applications that use them will be affected by this change.

Here is a summary of the results after the execution of the above commands:

On a 64-bit OS:

Before:

TotalSeconds : 23.4781061

TotalSeconds : 1.0336202

After:

TotalSeconds : 3.437742

TotalSeconds : 1.0284757

On a 32-bit OS:

Before:

TotalSeconds : 8.578462

TotalSeconds : 1.1123916

After:

TotalSeconds : 3.592183

TotalSeconds : 1.2297652

You can check if there is any speed-up for you, by using the PowerCLI script at the beginning to measure your timings.

If there is no significant change in the timings for you, you should undo these settings by replacing the install options of the ngen commands with the uninstall option.

The tool that is used above is ngen.exe. It is a tool that is installed with .Net Framework. You can read more about it here.

 

6 thoughts on “How to speed-up the execution of the first PowerCLI cmdlet

  1. Thanks for the great info Atanas.
    Provided I’m an administrator on my machine, I suppose I can place these ngen statements in one of my PowerShell profiles ?

  2. No need to. You need to do it only once. It is persisted and may only need a rerun if a new version of PowerCLI is installed. The precompiled assemblies are stored in the GAC, that is, all users and applications that use them will be affected by the change,
    Thanks for the question, I’ll update the post to note that.

  3. wow.. This is a MUST DO!!! I would go as far as to say a script should be included in the next release which does this on 1st run.
    Thanks so much for sharing..
    Ryan

  4. Thanks for the post, i am having problems getting this to make any difference to my times still though. When i try to run the command i am getting the following:
    C:\Windows\system32>C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe ins
    tall “VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyTo
    ken=10980b081e887e9f”
    Microsoft (R) CLR Native Image Generator – Version 2.0.50727.4927
    Copyright (c) Microsoft Corporation. All rights reserved.
    Installing assembly VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutra
    l, PublicKeyToken=10980b081e887e9f
    Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANT
    LOADLIBRARY))

  5. Hi Adam,
    Make sure you run the cmd prompt as administrator (right-click > Run as administrator)
    If that does not help, check that VimService41.XmlSerializers is present in your GAC (C:\windows\assembly)