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 } }
1 Answer 1
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– BozoJoe2017年10月18日 15:40:51 +00:00Commented Oct 18, 2017 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
(orLabel
) andExpression
. They can be abbreviated all the way down to a single character:@{n='Name';e={$_.Value}}
. You can also specify the column's width withWidth
withFormat-Table
. See here and here. \$\endgroup\$Bacon Bits– Bacon Bits2017年10月18日 19:20:47 +00:00Commented Oct 18, 2017 at 19:20 -
\$\begingroup\$ @BozoJoe,
Exp
can contain any PowerShell expression. For instance if you wantedpackagename
to be in uppercase, then you could doExp={$_.packagename.ToUpper()}
. I used in the code above because this won't work:select versions.Count
. \$\endgroup\$Dangph– Dangph2017年10月19日 02:26:31 +00:00Commented Oct 19, 2017 at 2:26
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 toversionMAX
? Also, keep in mind that Version is a string and will use string sorting, so version10.2
is less than version9.5
\$\endgroup\$