Home > Blogs > VMware PowerCLI Blog > Monthly Archives: June 2011

Monthly Archives: June 2011

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.

 

Managing Virtual Machine questions with PowerCLI

Greetings to all vSphere administrators out there! This post will illustrate another vSphere feature that is interesting from an automation perspective, namely – virtual machine questions.

Virtual Machine questions are questions that must be answered before the operation of a given virtual machine can continue. These questions can arise in different situations. These situations are usually rare but when they occur they block the virtual machine and require attention before the VM can be started again. Let’s see how we can elegantly handle those VM questions with PowerCLI.

One case where a VM question can arise is when you physically copy a VM form one Datastore location to another and then add the newly copied VM to the inventory. In this case the ESX server wants to know if you have moved the VM or copied it. In the first case the UUID of the VM is preserved whereas in the second case a mew UUID is generated. The specifics of this scenario are described in this KB. The important thing here is that when you try to start the VM with PowerCLI you will get an error stating that a VM question must be answered before the VM can start.

PS D:\ > Get-VM QuestionVM | Start-VM

Start-VM : 6/27/2011 1:11:55 PM    Start-VM        This VM has questions that must be answered before the operation can continue.

At line:1 char:29

+ Get-VM *question* | Start-VM <<<<

    + CategoryInfo          : InvalidOperation: (:) [Start-VM], VmBlockedByQuestionException

    + FullyQualifiedErrorId : Client20_VmServiceImpl_WrapInVMQuestionWatchingTask_HasQuestions,VMware.VimAutomation.ViCore.Cmdlets.Commands.StartVM

PS D:\ >

 Now you know a question is preventing this VM from starting. What you want to do is get the question and answer it. This is done with the Get-VMQuestion and Set-VMQuestion cmdlets.

PS D:\ > Get-VMQuestion

 

Text                                               Options              Default

—-                                               ——-              ——-

msg.uuid.altered:This virtual machine might have b {Cancel, I moved it, I copied i

een moved or copied.                                I copied it}        t

In order to configure certain management and networking features, VMware ESX needs to know if this virtual machine was moved or copied.

If you don't know, answer "I copied it".

PS D:\ >

You can answer the question this way:

PS D:\ > Get-VMQuestion | Set-VMQuestion –Option "I copied it"

Confirmation

Answer 'I copied it' to VM question 'msg.uuid.altered:This virtual machine might have been moved or copied.

In order to configure certain management and networking features, VMware ESX needs to know if this virtual machine was moved or copied.

If you don't know, answer "I copied it".

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

PS D:\ >

 Alternatively, you can specify that you want to give the default answer of the question (every VMQuestion has a default answer):

PS D:\ > Get-VMQuestion | Set-VMQuestion –DefaultOption

You can also cancel the Power-On operation by answering “cancel” to the question:

 PS D:\ > Get-VMQuestion | Set-VMQuestion –Option cancel

 

I hope this will be useful.

Regards,

-Angel Evrov, MTS at VMware

Back to Basics: Part 1 – Installing PowerCLI

The “Back to Basics” (B2B) blog post series will be showing people new to PowerShell and PowerCLI how to get started, they will be an ongoing feature on the PowerCLI blog and will cover each aspect of managing your virtual infrastructure with PowerShell and PowerCLI.

To get us started we have gone back to the beginning – Installing PowerCLI.

PowerCLI is typically installed on a vSphere administrators Microsoft Windows based desktop system or anywhere where you may currently run the vSphere Client, PowerCLI will access the vSphere API in a similar way to the vSphere Client so the same ports and access rights are needed to your vCenter or vSphere host.

In order to support installing PowerCLI to a desktop system the following prerequisites software packages must be present:

· Windows .Net Framework 2.0 (3.5 latest SP Recommended)

· Windows PowerShell (V2 Recommended)

Windows PowerShell V2 is integrated with Windows 7 and Windows 2008 R2, previous operating systems such as Windows XP, Windows Vista, Windows 2008 (Non R2) and Windows 2003 are compatible with Windows PowerShell, this must be first downloaded and installed from the following website: http://support.microsoft.com/kb/968929

After checking all pre-requests’ are installed you must set the 'Execution Policy' of PowerShell to enable it to be able to run scripts. By default PowerShell is installed in secure mode, which will disable the running of scripts within PowerShell.

Continue reading