简体   繁体   中英

Difference observed b/w NuGet Package Manager Console and plain PowerShell console

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM