0

In NuGet's Package Manager Console within Visual Studio (I am using 2010 Ultimate), the following code works and successfully activates the Toolbox window:

$dte.windows.Item("{B1E99781-AB81-11D0-B683-00AA00A3EE26}").activate()

But the same code does not work in a stand-alone PowerShell console session. Here's the script I am using:

# Note: Start PowerShell console with the -STA switch.

# Get reference to VS2010 automation object (COM).
$DTE = New-Object -ComObject VisualStudio.DTE.10.0

$DTE.Solution.Open("C:\wfa1\wfa1.sln")

$DTE.MainWindow | %{$_.gettype().InvokeMember("Visible","SetProperty",$null,$_,$true)}

$DTE.Windows.Item("{B1E99781-AB81-11D0-B683-00AA00A3EE26}").Activate()

$DTE.Quit() 

The error I am getting:

Method invocation failed because [System.__ComObject] doesn't contain a method named 'Item'.
At C:\users\ams\Desktop\ATS\AddTypeSample.ps1:114 char:18
+ $DTE.Windows.Item <<<< ("{B1E99781-AB81-11D0-B683-00AA00A3EE26}").Activate()
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

I know NuGet preps its console so that, for example, the $dte variable is immediately available for use w/o having to do anything. How can I set up my PS script so that the commands will work seamlessly as they do in NuGet's console?

I am using PowerShell version 2 on Windows 7 Ultimate.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • (Note: Entirely unfamiliar with NuGet.) What is `$dte.Windows` in both scenarios? – Etan Reisner Apr 15 '15 at 17:57
  • @EtanReisner The output of `$dte.Windows` in NuGet's console is a series of repeating properties starting with `AutoHides` and ending with `OutstandingEventCount`; in the PowerShell console, the output is `System.__ComObject` repeated nine times. – Sabuncu Apr 15 '15 at 18:02
  • What about `$dte.windows[0].gettype()`? (I'm assuming it is an array from the repeating.) – Etan Reisner Apr 15 '15 at 18:13
  • @EtanReisner In the PowerShell console: Unable to index into an object of type System.__ComObject. In NuGet: Unable to index into an object of type Microsoft.VisualStudio.Platform.WindowManagement.DTE.Windows. – Sabuncu Apr 15 '15 at 18:24
  • Not exactly how I expected to find out the type but that seems to have given me what I want. I don't know what to do with that information but the problem seems to be that powershell isn't using/setting the correct type on the windows object. You could try a manual cast but I think that's unlikely to work. Someone familiar with NuGet might be able to say more here but I think I'm too far out of my depth to help any more. – Etan Reisner Apr 15 '15 at 18:27
  • @EtanReisner OK, thanks nevertheless. – Sabuncu Apr 15 '15 at 18:28

1 Answers1

1

I don't think what you are trying to do is possible. When inside the Package Manager Console in Visual Studio your PowerShell script is running inside an entirely different host which provides access to the Visual Studio environment. That is where the $dte variable comes from. When you are running your script in a plain PowerShell Console window you are using the default PowerShell host which has no knowledge of Visual Studio.

Have a look at Writing a Windows PowerShell Host Application on MSDN for more details about Windows PowerShell Hosts. You can check which host your script is running in using the Get-Host cmdlet.

  • I see, you are saying the magic provided by the Package Manager Console cannot easily be duplicated in a plain console. – Sabuncu Apr 17 '15 at 15:44