简体   繁体   English

NuGet软件包管理器控制台与普通PowerShell控制台之间观察到的差异

[英]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: 在Visual Studio(我使用的是2010 Ultimate)的NuGet的Package Manager控制台中,以下代码可以正常工作并成功激活“工具箱”窗口:

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

But the same code does not work in a stand-alone PowerShell console session. 但是,相同的代码在独立的PowerShell控制台会话中不起作用。 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'. 方法调用失败,因为[System .__ ComObject]不包含名为“ Item”的方法。
At C:\\users\\ams\\Desktop\\ATS\\AddTypeSample.ps1:114 char:18 在C:\\ users \\ ams \\ Desktop \\ ATS \\ AddTypeSample.ps1:114 char:18
+ $DTE.Windows.Item <<<< ("{B1E99781-AB81-11D0-B683-00AA00A3EE26}").Activate() + $ DTE.Windows.Item <<<<(“ {B1E99781-AB81-11D0-B683-00AA00A3EE26}”)。Activate()
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + CategoryInfo:InvalidOperation:(Item:String)[],RuntimeException
+ FullyQualifiedErrorId : MethodNotFound + 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. 我知道NuGet会准备好其控制台,例如, $dte变量可立即使用,而无需执行任何操作。 How can I set up my PS script so that the commands will work seamlessly as they do in NuGet's console? 如何设置我的PS脚本,以便这些命令可以像在NuGet控制台中一样无缝地工作?

I am using PowerShell version 2 on Windows 7 Ultimate. 我在Windows 7 Ultimate上使用PowerShell版本2。

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. 在Visual Studio的Package Manager控制台中时,您的PowerShell脚本在完全不同的主机中运行,该主机提供对Visual Studio环境的访问。 That is where the $dte variable comes from. 这就是$ dte变量的来源。 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. 在普通的PowerShell控制台窗口中运行脚本时,将使用不了解Visual Studio的默认PowerShell主机。

Have a look at Writing a Windows PowerShell Host Application on MSDN for more details about Windows PowerShell Hosts. 请参阅在MSDN上编写Windows PowerShell主机应用程序 ,以获取有关Windows PowerShell主机的更多详细信息。 You can check which host your script is running in using the Get-Host cmdlet. 您可以使用Get-Host cmdlet检查脚本在哪个主机中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NuGet包管理器控制台无法加载PowerShell - NuGet Package Manager Console unable to load PowerShell Nuget Package Manager控制台中有关PowerShell绑定的奇怪错误 - Strange errors in Nuget Package Manager Console about PowerShell bindings 从程序包管理器控制台添加自定义nuget源(PowerShell) - Add custom nuget feed from the Package Manager Console (PowerShell) Nuget Package Manager控制台无法执行EntityFramework PowerShell脚本 - Nuget Package Manager console unable to execute entityFramework powershell scripts 在Visual Studio Nuget包管理器控制台中中止“unclosed”powershell命令 - Aborting an “unclosed” powershell command in Visual Studio Nuget Package manager console 程序包管理器控制台上的PowerShell版本 - PowerShell Version on Package Manager Console Nuget Package Manager控制台等效于“管理”复选框 - Nuget Package Manager Console equivalent for Manage checkbox 从Nuget Package Manager控制台内的解决方案资源管理器运行powershell脚本 - Running powershell script from solution explorer inside Nuget Package Manager Console 没有PowerShell访问权限的Package Manager控制台命令 - Package Manager console command without PowerShell access 在 NuGet 包管理器控制台中列出特定包的所有可用版本 - List all available versions of a specific package in NuGet Package Manager Console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM