NuGet – Find Project referencing Version Y of Pkg M – Part 1 of 2

Had an issue at work where Visual Studio Solution (.sln) was constantly showing that a NuGet package can be updated, even though all of the individual projects where up-to date, and greyed out.

I did some digging and had to learn PowerShell in order to find this out, as this can be used in the NuGet Package Manager Console.

First of all you will need to run the Get-Project -All to list all of the projects in the solution.

This returns a List<System.__ComObject> (C# Equivalent)

Finding out properties of an object

To Find out what variables are available in that object, I piped this output into the following commands:

  • | Select-Object -First 1 in order to only get the first object in the list
  • | Select * in order to select all properties of that object.
  • | Write-Host command in order to output all variables

This returned a list of all the available variables in the piped System.__ComObject

E.g. `@{Name=ABC; FullName=C:\SomeDir\SomeSln\ABC.csprod; …..}

Find installed packages using Get-Package command

NuGet has a command called Get-Package which lets you list all installed packages inside a Solution/Project.

It has 2 parameters which will help us:

  • -Filter M Which allows us to filter according to some string. E.g. Get-Package -Filter M to get all the packages with M in its name.
  • -First x which allows us to get the top x number of packages. E.g. Get-Package -First 1
  • -Project ABC Which allows us to filter for installed packages only in the specified project ABC

First I run Get-Package -Filter M to get a list of all the M packages installed.

This returned the following values

Id   Version
---  ------
M    1.0.0.0
M    2.0.0.0

As I am interested only in the older version. I knew I could use -First 1 in order to just get the first value.

Therefore Get-Package -Filter M -First 1 returned only the older package M(1.0.0.0)

Looping through each project

Powershell has a ForEach{} statement which you can use to loop over a collection, such as the List of System.__ComObjects returned from Get-Project -All

E.g. Get-Project | ForEach{ WriteHost }

This will loop over each returned object from Get-Project and do a Console.WriteLine(object). This basically just prints out a System.__ComObject\r\n per project.

There is a shorthand for writing ForEach{} statements in PowerShell which is %{}. Making the following 2 statements identical

Get-Project | ForEach{ Write Host }
Get-Project | %{ Write Host }    

I will use the shorthand %{} ForEach notation from now on.

Accessing the Piped Object

You can access the current piped variable using the $_ notation

Therefore you can access all the properties of this object inside the loop by $_.Property

E.g. Get-Package | %{ $_.Name } will return a list of all Project Names.(As Name is a property on the returned object from Get-Package

To Be Continued

In the next part I will talk about how using all of these, you can find out which project is referencing an older version of a NuGet package.

This should be available by 6th March 2016. Keep tuned in!

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

This site uses Akismet to reduce spam. Learn how your comment data is processed.