37

I was trying to use my favorite source control from the Package Manager console in Visual Studio 2010. I had issues described in another topic. For now the best answer is to move all command-line stuff from Visual Studio to bare PowerShell.

But this case the Visual Studio related commandlets are not working. For example, most crucial one — I cannot run Update-Database command from Entity Framework.

Is it possible to register somehow Visual Studio specific commandlets for the current project in PowerShell? Or make PowerShell automatically take current project context from the packages subfolder?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shytikov
  • 9,155
  • 8
  • 56
  • 103

3 Answers3

13

The NuGet PowerShell commands rely on being run from within Visual Studio so will not work outside in the normal PowerShell running from the command line.

You can however use migrate.exe which ships with the EntityFramework NuGet package and use that from the command line to update your database.

As a prototype I put together a way to use NuGet PowerShell commands from the normal PowerShell command line using SharpDevelop. Unfortunately at the moment the EntityFramework NuGet package does not work with SharpDevelop.

Another interesting project is StudioShell which provides a new DTE: drive inside Visual Studio but can also be used outside from the command line. I do not believe it supports NuGet PowerShell commands being run from the normal PowerShell command line.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
10

I can find the NuGet.psd1 file at:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1.

However, when you try to load it:

PS> Import-Module $pathToNuGetPsd1 -Force -NoClobber -Scope Global
Import-Module : The name of the current Windows PowerShell host is: 'ConsoleHost'.
The module 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\
Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1' requires the following Windows
PowerShell host: 'Package Manager Host'.

I think we're out of luck. It has to be run from the Package Manager Host and requires things from Visual Studio as stated by Matt.

To solve my problem I used Chocolatey to install NuGet.CommandLine and then used NuGet.bat to do what I needed. It is a little more work and may not work in all cases depending on what you're trying to do.

Chocolatey: https://github.com/chocolatey/chocolatey/wiki/Installation

NuGet.CommandLine:

PS> cinst NuGet.CommandLine
petrsnd
  • 6,046
  • 5
  • 25
  • 34
  • 2
    You can change the console host name by this code `$host.gettype().GetField('nameResult', 'Instance, NonPublic').setValue($host, 'Package Manager Host')`, but then there's some more errors. – Mark Toman Jun 19 '14 at 22:58
4

I'm not terribly familiar with the Visual Studio cmdlets, but you can import a module into your PowerShell session by using Import-Module -Name <ModuleName>. You can list the available (aka. "installed") PowerShell modules by using `Get-Module -ListAvailable'.

My guess would be that the Visual Studio cmdlets are contained with its own PowerShell module, but it's quite possible that it's not "installed" to one of the standard locations in $env:PSModulePath. If this is the case, then you might need to locate the module directory and import either the .psd1 or .psm1 file directly, and pass that into: Import-Module -Name <FullPathToModuleFile>.

As an example of the above, take notice of where the Windows Azure PowerShell module is located: http://trevorsullivan.net/2012/06/07/introducing-microsofts-official-windows-azure-powershell-module/

It's under the Program Files directory, and is not immediately available to PowerShell, unless you import the module from its fully qualified path (the .psd1 module manifest file).

Hope this helps.