Taming the VSX beast from PowerShell

Using VSX from PowerShell is not always a pleasant experience. Most stuff in VSX is still good old COM the System.__ComObject types are flying around. Everything can be casted to everything (for example EnvDTE to EnvDTE2) if you are in C#, but PowerShell can’t make spaghetti of it.

Enter Power Console. In Power Console there are some neat tricks available to help you out of VSX trouble. And most of the trouble solving is done in… PowerShell. You just need to know what to do.

To get out of trouble do the following:

  1. Head over to the Power Console site
  2. Right-click on the Download button, and save the PowerConsole.vsix file to your disk
  3. Rename PowerConsole.vsix to PowerConsole.zip and unzip
  4. Look in the Scripts folder for the file Profile.ps1 which is full of PowerShell/VSX magic

The PowerShell functions that perform the VSX magic are:

Extract from Profile.ps1
  1. <#
  2. .SYNOPSIS
  3.    Get an explict interface on an object so that you can invoke the interface members.
  4.    
  5. .DESCRIPTION
  6.    PowerShell object adapter does not provide explict interface members. For COM objects
  7.    it only makes IDispatch members available.
  8.    
  9.    This function helps access interface members on an object through reflection. A new
  10.    object is returned with the interface members as ScriptProperties and ScriptMethods.
  11.    
  12. .EXAMPLE
  13.    $dte2 = Get-Interface $dte ([EnvDTE80.DTE2])
  14. #>
  15. function Get-Interface
  16. {
  17.    Param(
  18.        $Object,
  19.        [type]$InterfaceType
  20.    )
  21.    
  22.    [Microsoft.VisualStudio.PowerConsole.Host.PowerShell.Implementation.PSTypeWrapper]::GetInterface($Object, $InterfaceType)
  23. }
  24.  
  25. <#
  26. .SYNOPSIS
  27.    Get a VS service.
  28.  
  29. .EXAMPLE
  30.    Get-VSService ([Microsoft.VisualStudio.Shell.Interop.SVsShell]) ([Microsoft.VisualStudio.Shell.Interop.IVsShell])
  31. #>
  32. function Get-VSService
  33. {
  34.    Param(
  35.        [type]$ServiceType,
  36.        [type]$InterfaceType
  37.    )
  38.  
  39.    $service = [Microsoft.VisualStudio.Shell.Package]::GetGlobalService($ServiceType)
  40.    if ($service -and $InterfaceType) {
  41.        $service = Get-Interface $service $InterfaceType
  42.    }
  43.  
  44.    $service
  45. }
  46.  
  47. <#
  48. .SYNOPSIS
  49.    Get VS IComponentModel service to access VS MEF hosting.
  50. #>
  51. function Get-VSComponentModel
  52. {
  53.    Get-VSService ([Microsoft.VisualStudio.ComponentModelHost.SComponentModel]) ([Microsoft.VisualStudio.ComponentModelHost.IComponentModel])
  54. }

 

The same Profile.ps1 file contains a nice example of how to use these functions:

A lot of other good samples can be found on the Power Console site at the home page.

Now there are two things you can do with respect to the Power Console specific function GetInterface() on line 22:

  1. Make sure that Power Console is installed and load the assembly Microsoft.VisualStudio.PowerConsole.Host.PowerShell.Implementation.dll
  2. Fire-up reflector and investigate the GetInterface() function to isolate the GetInterface code into your own library that only contains this functionality (I did this, it is a lot of work!)

For this post we use the first approach, the second approach is left for the reader as an exercise:-)

To try it out I want to present a maybe bit unusual case: I want to be able to access Visual Studio from a PowerShell script that is executed from the MSBuild script building a project.

In the .csproj of my project I added the following line:

<!-- Macaw Software Factory targets -->
<Import Project="..\..\..\..\tools\DotNet2\MsBuildTargets\Macaw.Mast.Targets" />

The included targets file loads the PowerShell MSBuild Task (CodePlex) that is used to fire a PowerShell script on AfterBuild. Below a relevant excerpt from this targets file:

Macaw.Mast.Targets
  1. <Project DefaultTargets="AfterBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  2.     <UsingTask  AssemblyFile="PowershellMSBuildTask.dll" TaskName="Powershell"/>
  3.  
  4.     <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildDependsOn)">
  5.         <!-- expand $(TargetDir) to _TargetDir, otherwise error on including in arguments list below -->
  6.         <CreateProperty Value="$(TargetDir)">
  7.             <Output TaskParameter="Value" PropertyName="_TargetDir" />
  8.         </CreateProperty>
  9.         <Message Text="OnBuildSuccess = $(@(IntermediateAssembly))"/>
  10.         <Powershell Arguments="
  11.                   MastBuildAction=build;
  12.                   MastSolutionName=$(SolutionName);
  13.                   MastSolutionDir=$(SolutionDir);
  14.                   MastProjectName=$(ProjectName);
  15.                   MastConfigurationName=$(ConfigurationName);
  16.                   MastProjectDir=$(ProjectDir);
  17.                   MastTargetDir=$(_TargetDir);
  18.                   MastTargetName=$(TargetName);
  19.                   MastPackageForDeployment=$(MastPackageForDeployment);
  20.                   MastSingleProjectBuildAndPackage=$(MastSingleProjectBuildAndPackage)
  21.                 "
  22.                 VerbosePreference="Continue"
  23.                 Script="&amp; (Join-Path -Path &quot;$(SolutionDir)&quot; -ChildPath &quot;..\..\..\tools\MastDeployDispatcher.ps1&quot;)" />
  24.   </Target>
  25. </Project>

The MastDeployDispatcher.ps1 script is a Macaw Solutions Factory specific script, but you get the idea. To test in which context the PowerShell script is running I added the following lines op PowerShell code to the executed PowerShell script:

$process = [System.Diagnostics.Process]::GetCurrentProcess()
Write-Host "Process name: $($a.ProcessName)"

Which returns:

Process name: devenv

So we know our PowerShell script is running in the context of the Visual Studio process. I wonder if this is still the case if you set the maximum number of parallel projects builds to a value higher than 1 (Tools->Options->Projects and Solutions->Build and Run). I did put the value on 10, tried it, and it still worked, but I don’t know if there were more builds running at the same time.

My first step was to try one of the examples on the Power Console home page: show “Hello world” using the IVsUIShell.ShowMessageBox() function.

I added the following code to the PowerShell script:

PowerShell from MSBuild
  1. [void][reflection.assembly]::LoadFrom("C:\Users\serge\Downloads\PowerConsole\Microsoft.VisualStudio.PowerConsole.Host.PowerShell.Implementation.dll")
  2. [void][reflection.assembly]::LoadWithPartialName("Microsoft.VisualStudio.Shell.Interop")
  3.  
  4. function Get-Interface
  5. {
  6.    Param(
  7.        $Object,
  8.        [type]$InterfaceType
  9.    )
  10.    
  11.    [Microsoft.VisualStudio.PowerConsole.Host.PowerShell.Implementation.PSTypeWrapper]::GetInterface($Object, $InterfaceType)
  12. }
  13.  
  14. function Get-VSService
  15. {
  16.    Param(
  17.        [type]$ServiceType,
  18.        [type]$InterfaceType
  19.    )
  20.  
  21.    $service = [Microsoft.VisualStudio.Shell.Package]::GetGlobalService($ServiceType)
  22.    if ($service -and $InterfaceType) {
  23.        $service = Get-Interface $service $InterfaceType
  24.    }
  25.  
  26.    $service
  27. }
  28.  
  29. $msg = "Hello world!"
  30. $shui = Get-VSService `
  31. ([Microsoft.VisualStudio.Shell.Interop.SVsUIShell]) `
  32. ([Microsoft.VisualStudio.Shell.Interop.IVsUIShell])
  33. [void]$shui.ShowMessageBox(0, [System.Guid]::Empty,"", $msg, "", 0, `
  34.     [Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON]::OLEMSGBUTTON_OK,
  35.     [Microsoft.VisualStudio.Shell.Interop.OLEMSGDEFBUTTON]::OLEMSGDEFBUTTON_FIRST, `
  36.     [Microsoft.VisualStudio.Shell.Interop.OLEMSGICON]::OLEMSGICON_INFO, 0)

When I build the project I get the following:

image

So we are in business! It is possible to access the Visual Studio object model from a PowerShell script that is fired from the MSBuild script used to build your project. What you can do with that is up to your imagination. Note that you should differentiate between a build done on your developer box, executed from Visual Studio, and a build executed by for example your build server, or executing from MSBuild directly.

Published Sunday, July 18, 2010 2:49 AM by svdoever

Comments

No Comments