6

I'm trying to run the following command in a PowerShell script.

nuget pack MyNuGetSpec.nuspec -Version 1.2.3-alpha

When I have this PS code, I get an error..

Code:

"NuGet packing $file to a .nupkg ..."
$exe = $path + "nuget.exe pack $file -Version $version"

$exe

&$exe

and the error message..

NuGet packing MyNuGetSpec.nuspec to a .nupkg ... C:\Projects\Foo\NuGet Package Specifications\nuget.exe pack MyNuGetSpec.nuspec -Version 1.2.3-alpha & : The term 'C:\Projects\Foo\NuGet Package Specifications\nuget.exe pack MyNuGetSpec.nuspec -Version 1.2.3-alpha' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Projects\Foo\NuGet Package Specifications\build.ps1:106 char:10 + &$exe + ~~~~ + CategoryInfo : ObjectNotFound: (C:\Projects\Foo... 0.1.0-alpha:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CommandNotFoundException

$path == Directory: C:\Projects\Foo\NuGet Package Specifications
$file == `MyNuGetSpec.nuspec`
$version == 0.1.0-alpha`

Lastly, I have the .exe side-by-side (in the same folder) as the .nuspec file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

8

You need to separate the executable name from the arguments:

$exe = $path + "nuget.exe"

&$exe pack $file -Version $version
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31
6

Minor alteration,

 $path="c:\..."
 $file= "MyNuGetSpec.nuspec"
 $version= "0.1.0-alpha"
 Invoke-Expression "$($path)\nuget.exe pack $($file) -Version $($version)"

This should execute nuget.exe with appropriate parameters

GeekzSG
  • 943
  • 1
  • 11
  • 28
  • How does this differ to @mickyBalladelli's answer? – Pure.Krome Dec 29 '14 at 13:40
  • 2
    When using `&`, the arguments are passed as-is separately. When using `Invoke-Expression` in this case, it will generate the full string first, then evaluate that string as powershell code. The `&` command is much safer since it will only expand the `$exe`. When using `Invoke-Expression` if `$file` has a semicolon and second command, you've got a problem. – Eris Jan 01 '15 at 21:39