Performance

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.

 

Comments

26 comments have been added so far

  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. Any suggestions if this does not improve things at all? My initial connection took 166 seconds. After applying the above command (changed for Powercli 5.5), I get 165 seconds.

  6. If you are using PowerShell 3+, then you need to use the .Net 4 version of the NGEN tool. Change the path to:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319

    Also since the PowerCLI 5.5 uses .Net 2.0, you need to specify the /ExeConfig parameter and point it to the PowerShell_ISE.exe on your machine (probably any .Net 4 exe would do).

    So try something like:

    %windir%\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install “VimService55.XmlSerializers, Version=5.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f” /ExeConfig:%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe

    This is for the 64-bit version of PowerShell and the vSphere 5.5 assembly.

  7. Almost went insane on this one. On Atanas’ last post use this if you hit the unrecognized version error.

    %windir%\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install “VimService55.XmlSerializers, Version=5.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f” /ExeConfig:%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe

    It’s the formatting of the double quotes 🙂

    1. well, don’t use that, the page seems to format the quotes differently. if you hit the error manually re-type the quotes.

  8. im try but have this error. can help me ?

    PowerCLI C:\Windows\system32> C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install “VimService41.XmlSerializers, Versi
    on=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f”

    Microsoft (R) CLR Native Image Generator – Version 2.0.50727.5420
    Copyright (c) Microsoft Corporation. All rights reserved.
    Uninstalling assembly VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f because of an
    error during compilation: The system cannot find the file specified. (Exception from HRESULT: 0x80070002).
    The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
    PowerCLI C:\Windows\system32>

    1. First, do a directory listing to find out what version of PowerCLI you have installed…

      I went to the root and did:

      dir /a/s/w vimService*.*

      I found the following file, since I am running PowerCLI 5.5

      C:\Windows\assembly\NativeImages_v2.0.50727_64\VimService55.XmlSer#\2cd1c6297c237e915f27f2e23a04a895\VimService55.XmlSerializers.ni.dll

      I ran the same command and altered the file version number (I went into the properties of that file I found) and hoped that the PublicKeyToken would be the same (since they were all throughout this post), and voilla, seemed to have worked successfully. Here is the final command I ran:

      C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install “VimService55.XmlSerializers, Version=5.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f”

    1. For 6.0, you can try this:
      C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install “VimService60.XmlSerializers, Version=6.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f”
      C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install “VimService60.XmlSerializers, Version=6.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f”

      The PowerCLI installer should have already done this for you though.

      1. Still takes about 30 seconds to load PowerCLI on my 2008R2 server. I also unchecked the publisher’s certificate check in IE which definitely improved start up (was taking about a full minute before), but no where near as fast as my Win7 laptop which starts PowerCLI up in under 5 seconds. Very strange! I’m using Powershell v3, .NET v4 and PowerCLI v6r3.

  9. Hi Atanas,
    I’m attempting to do this with PowerCLI 6.5, and running into issues. Can you comment on if this technique still applies to 6.5, and if so how does one go about implementing it? I’ve tried doing it myself, and get the following error (with admin):

    C:\>C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install “VimService65.XmlSerializers, Version=6.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f”
    Microsoft (R) CLR Native Image Generator – Version 4.6.1038.0
    Copyright (c) Microsoft Corporation. All rights reserved.
    Uninstalling assembly VimService65.XmlSerializers, Version=6.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f because of an error during compilation: The system cannot find the file specified. (Exception from HRESULT: 0x80070002).
    The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

    I’ve confirmed that VimService65.dll exists, assuming this is what gets pointed to based on comments, but not sure how to tell ngen.exe where to find the file. I also can’t find “VimService” in the results of ‘ngen.exe /display /verbose’ either.

    1. This optimization is not possible and not needed any more with version 6.5+ of PowerCLI. We have optimized the code to not require precompilation with NGEN. Also PowerShell modules cannot currently use precompilation – NGEN requires that the assembly be stored in the GAC or the application folder (that is, the folder of powershell.exe). While modules are located in an arbitrary folder.

  10. Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *