4

I'm trying to find a way to call an Install-Package myPackage right from C# code.

I've tried to do so using powershell in c# but I'm not very familiar with it.

Can anyone give me an example of how to do so?

Rob
  • 45,296
  • 24
  • 122
  • 150

2 Answers2

1

You could try something like this to execute powershell command

Namespace: System.Management.Automation

Assembly: System.Management.Automation (in system.management.automation.dll)

    private static void RunPowershell(string command)
    {

        var powerShell = PowerShell.Create();
        powerShell.AddCommand(command);
        powerShell.Invoke();

    }
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Thanks, but I've already figured how to do this. The thing is that NuGet provide a powershell extension that I need to call. In Visual Studio, I can use the Package Manager to execute commands but I can't figure how to call NuGet commands from code. –  Feb 20 '11 at 06:04
0

You need to add the NuGet Module in your PowerShell session. After installing NuGet, you can run Get-Module and see what is available

PM> get-module | fl *


ExportedCommands    : {Get-Package, Update-Package, Register-TabExpansion, Get-Project...}
Name                : NuGet
Path                : C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\nuget.psm1
Description         : NuGet PowerShell module used for the Package Manager Console
Guid                : 76e6f9c4-7045-44c0-a557-17fab0835c12
ModuleBase          : C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160
PrivateData         : 
Version             : 1.1.229.160
ModuleType          : Script
AccessMode          : ReadWrite
ExportedFunctions   : {[NugetTabExpansion, NugetTabExpansion], [Register-TabExpansion, Register-TabExpansion]}
ExportedCmdlets     : {[Add-BindingRedirect, Add-BindingRedirect], [Get-Package, Get-Package], [Get-Project, Get-Project], [Install-Package, Install-Package]...}
NestedModules       : {NuGet.Cmdlets}
RequiredModules     : {}
ExportedVariables   : {}
ExportedAliases     : {}
SessionState        : System.Management.Automation.SessionState
OnRemove            : 
ExportedFormatFiles : {C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\NuGet.Format.ps1xml}
ExportedTypeFiles   : {C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\NuGet.Types.ps1xml}

Notice the path to the PSM1 file is here

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft Corporation\NuGet Package Manager\1.1.229.160\Scripts\nuget.psm1

if you import this PSM1 file, you should be able to execute your NuGet commands.

Andy Schneider
  • 8,516
  • 6
  • 36
  • 52
  • I need to do something really similar. Any chance you can expand on how to import the PSM1 file to be able to execute the NuGet commands. My scenario is that I want to make a winforms app that executes a scaffolding package passing a load of params taken from the users selections on the UI to control what files are generated. – chrisp_68 Feb 28 '11 at 09:00
  • I had a reply on the NuGet forum about NuGet 1.2 having an API allowing to do that kind of actions directly in code instead of having to call PowerShell stuff. I'll wait 'til there and leave out the powershell method. http://nuget.codeplex.com/discussions/246688 –  Feb 28 '11 at 19:35
  • Take a look here as well for an example of it working: http://haacked.com/archive/2011/01/15/building-a-self-updating-site-using-nuget.aspx – ferventcoder May 25 '11 at 18:05