0

I'm not that great with Powershell and I'm facing a simple problem. I'm building an automatic Nuget package publish script and for that I need to push the nuget package with a file name like this "Name.version.nupkg". The version number changes so I would need to find a file that matches the patter with changing version number.

Currently the script is a simple script:

cd $PSScriptRoot
dotnet nuget push ..\bin\Debug\MacroFramework.*.nupgk --api-key --source https://api.nuget.org/v3/index.json

$PSScriptRoot is "MacroFrameworkLibrary/Local"

And the output is:

error: File does not exist (..\bin\Debug\MacroFramework.*.nupgk).

Image of the directory content here:

enter image description here

Thanks for the help in advance

EDIT: The final working script is as follows:

cd $PSScriptRoot
$files = Get-ChildItem ..\bin\Debug\MacroFramework.*.nupkg | Sort-Object LastWriteTime -Descending | Select-Object -First 1

foreach ($pgk in $files)
{
  dotnet nuget push $pgk.FullName --api-key --source https://api.nuget.org/v3/index.json
}
0

1 Answer 1

2

dotnet nuget push doesn't support wildcards in paths.

You can get around this by using the wildcard in Get-ChildItem, sorting by date descending, then piping the first match to dotnet nuget push.

Here's an example. gci is an alias of Get-ChildItem, | means to send its output as input to the next command, and % { } is a for-each loop. Inside the loop, $_ refers to the current item.

cd $PSScriptRoot
gci ..\bin\Debug\MacroFramework.*.nupkg | sort LastWriteTime -Descending | select -First 1 | % { dotnet nuget push $_.FullName --api-key --source https://api.nuget.org/v3/index.json }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I changed from the & {} to a foreach loop. The working script is in the answer.
@porras Glad it helped. I missed that you only wanted the most recent nupkg. I added sorting by date descending and selecting the first result to my answer. That eliminates the need for the break and guarantees that the first item in the for-each is the most recent: $files = Get-ChildItem ..\bin\Debug\MacroFramework.*.nupkg | Sort-Object LastWriteTime -Descending | Select-Object -First 1
thanks, I updated the script for the original question as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.