«

»

Dec 03 2012

How to use VMware View PowerCLI with PowerShell 3.0

Whilst VMware View Connection Server does offer support for Windows Server 2008 R2 and R2 SP1, only PowerShell 2.0 is supported with the View PowerCLI cmdlets. The PowerShell 2.0 may offer all the functions an administrator may need, but you might want to utilize the newer  PowerShell 3.0 that is available for download from Microsoft Download Center.

PowerShell 3.0 offers many new features like Workflows, .NET Framework 4.0 support, a bunch of new core cmdlets and much more. I recently needed to use the new Invoke-RestRequest cmdlet from within VMware View Connection Server to talk to another system’s API.

After installing PowerShell 3.0 in my system I started  getting the error bellow when trying to execute cmdlets added by the VMware View PS-SnapIn (VMware.View.Broker). At this point I realized that VMware.View.Broker SnapIn is not forward compatible with PowerShell 3.0.

powercli_error_PS3

If you decide to upgrade to PowerShell 3.0 you will need to use the Start-Job cmdlet to run a Windows PowerShell background job. A Windows PowerShell background job runs a command “in the background” without interacting with the current session. When you start a background job, a job object is returned immediately, even if the job takes an extended time to complete. You can continue to work in the session without interruption while the job runs.

Here is a sample code:

[css lang=”plain” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ padlinenumbers=”false” gutter=”true” smarttabs=”true” tabsize=”10″ toolbar=”true”]
Start-Job -ScriptBlock {get-DesktopVM –Name “test-01”} -InitializationScript{add-pssnapin -name vmware.view.broker} -PSVersion 2.0
[/css]

 

One of the advantages of using Start-Job is that it is possible to specify the PowerShell environment version to execute the code between {}; –PSVersion 2.0. The Start-Job cmdlet also allows you to specify the VMware.View.Broker Snap-In to be loaded during the environment initialization. This is required to instantiate the VMware View PowerCLI cmdlets.

To see the job results simply execute Get-Job;

startjob

Optionally you may also use the Receive-Job cmdlet with the –Wait parameter to wait until the job is complete and return results to the $viewvmlist variable.

[css lang=”plain” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ padlinenumbers=”false” gutter=”true” smarttabs=”true” tabsize=”10″ toolbar=”true”]
$viewvmlist = Start-Job -ScriptBlock {get-DesktopVM} -InitializationScript {add-pssnapin -name vmware.view.broker} -PSVersion 2.0 | Receive-Job -Wait –AutoRemoveJob
[/css]

To execute regular PowerShell 3.0 cmdlets the Start-Job cmdlet is not required.

If you have a different method to execute the VMware.View.Broker Snap-In cmdlets in PowerShell 3.0 please share with us!

This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.

4 comments

Skip to comment form

  1. LucD

    Just stumbled on the same problem.
    My solution was to start PowerShell in v2, like this
    powershell.exe -version 2
    From there you can load the View snapin and execute the cmdlets.

  2. Andre Leibovici

    @LucD
    That works, but you are not able to utilize PowerShell 3.0 cmdlets. Unless you apply the same method, but with verison 3.0

    Start-Job -ScriptBlock {get-DesktopVM –Name “test-01”} -InitializationScript{add-pssnapin -name vmware.view.broker} -PSVersion 3.0

    Andre

  3. Anon

    Do you know if this problem has been fixed in current releases of PowerCLI?

  4. Andre Leibovici

    @Anon, I believe so; a long way back.

Leave a Reply