I'm hoping to automate the 'Manage Nuget Packages for Solution' Consolidate User Interface using Powershell in the 'Package Manager Console'.

The following script should take ONE of the packages in the consolidation list and 'fix it'.

#do just one
get-package | Group-Object Id | foreach { [pscustomobject]@{ packagename=$_.Name; VG=$_.Group ;versions=($_.Group |select -expandproperty versions |select -ExpandProperty Version); versionMAX=($_.Group |select -expandproperty versions |select -ExpandProperty Version |sort -property versions -descending |select -last 1)  } } | foreach { [pscustomobject]@{ packagename=$_.packagename; VG=$_.VG; versions=$_.versions; versionCount= $_.versions.Count; versionMAX=$_.versionMAX }} | where versionCount -gt 1 | foreach { [pscustomobject]@{ packagename=$_.packagename; VG=$_.VG; versions=$_.versions; versionMAX=$_.versionMAX; dversions=($_.versions | select -Unique) }} | foreach { [pscustomobject]@{ packagename=$_.packagename; VG=$_.VG; versionMAX=$_.versionMAX; PN=$_.VG.ProjectName; versions=$_.versions; dversionCount=$_.dversions.Count }} | where dversionCount -gt 1 | select -first 1 | foreach { foreach($v in $_.VG) { update-package -Id $v.Id -Version $_.versionMAX -projectname $v.ProjectName } }
  • \$\begingroup\$ versionMAX = ($_.Group |select -expandproperty versions |select -ExpandProperty Version |sort -property versions -descending |select -last 1) Is that not a bug? Sort the versions from Z to A, then get the last item and assign it to versionMAX? Also, keep in mind that Version is a string and will use string sorting, so version 10.2 is less than version 9.5 \$\endgroup\$
    – Bacon Bits
    Oct 18 '17 at 19:44
  • \$\begingroup\$ @BaconBits I thought it was a bug too! but using select -first 1 was giving me the lowest version. Maybe its the -descending in front of it ?? \$\endgroup\$
    – BozoJoe
    Oct 18 '17 at 23:56

You can use Select-Object instead of casting to pscustomobjects. For instance this:

foreach { [pscustomobject]@{ packagename=$_.packagename; VG=$_.VG; versions=$_.versions; versionCount= $_.versions.Count; versionMAX=$_.versionMAX }}

becomes this, which is somewhat shorter and less noisy:

select packagename VG versions @{Name="versionCount"; Exp={$_.versions.Count}} versionMAX

Notice the calculated property for versionCount.

Regarding the single line of code, I think you could have multiple statements separated by semicolons. You would work on the code as multiple lines in your text editor, and then join them together before pasting into VS.

Write-Output hello; Write-Output world
  • \$\begingroup\$ does the Exp field have special meaning? I was having problems without the cast to pscustomobject. \$\endgroup\$
    – BozoJoe
    Oct 18 '17 at 15:40
  • \$\begingroup\$ @BozoJoe It's a calculated property. It's a special hashtable to create a property with Select-Object and some other cmdlets. It has two elements: Name(or Label) and Expression. They can be abbreviated all the way down to a single character: @{n='Name';e={$_.Value}}. You can also specify the column's width with Width with Format-Table. See here and here. \$\endgroup\$
    – Bacon Bits
    Oct 18 '17 at 19:20
  • \$\begingroup\$ @BozoJoe, Exp can contain any PowerShell expression. For instance if you wanted packagename to be in uppercase, then you could do Exp={$_.packagename.ToUpper()}. I used in the code above because this won't work: select versions.Count. \$\endgroup\$
    – Dangph
    Oct 19 '17 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.