NuGet License Script – How can I ‘blacklist’ projects?

Welcome Forums General PowerShell Q&A NuGet License Script – How can I ‘blacklist’ projects?

Viewing 3 reply threads
  • Author
    Posts
    • #237520
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      I have a script that downloads every NuGet-License for every package used within a solutionfile (for every project in it). My problem is, that I got some ‘test-projects’ in nearly every solution which I wanna ignore.

      Is there a way to create a ‘blacklist’ with all project names I don’t wanna get licenses from?

      (this script needs to be run in the package-manager-console, with an .sln file opened)

      Get-Project -All – gets all the projects (so the project names) of an solution file.

      I’m quite new to this, but I tryna solve this problem since 4 days now but nothing really works. Wouldn’t it be possible to save the project names from ‘Get-Project -All’ in a list and do a foreach-loop to ignore the blacklisted projects?

      I hope someone can help me with this – I’m ready to learn. Thank you and stay healthy!

    • #237637
      Participant
      Topics: 6
      Replies: 552
      Points: 2,090
      Helping Hand
      Rank: Community Hero

      If I understand correctly, then the project names that are like “Test-Projects” should be excluded. See the minor adjustment below. You may need to adjust the $excluded string if it’s not really “Test-Projects.”

      Also if you have more than one you want to exclude, you may use this instead.

    • #237781
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      If I understand correctly, then the project names that are like “Test-Projects” should be excluded. See the minor adjustment below. You may need to adjust the $excluded string if it’s not really “Test-Projects.”

      PowerShell
      53 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      New-Item ItemType Directory Force Path “.\licenses”;
      $excluded = “Test-Projects”
      @( Get-Project All | ? { $_.ProjectName -and $_.ProjectName -notlike “*$excluded*”} | % {
      Get-Package ProjectName $_.ProjectName | ? { $_.LicenseUrl }
      } ) | Sort-Object Id Unique | % {
      $pkg = $_;
      Try
      {
      if ($pkg.Id -notlike ‘*Microsoft*’ -and $pkg.LicenseUrl.StartsWith(‘http’))
      {
      Write-Host (“Download license for package “ + $pkg.Id + ” from “ + $pkg.LicenseUrl);
      #Write-Host (ConvertTo-Json ($pkg));
      $licenseUrl = $pkg.LicenseUrl
      if ($licenseUrl.contains(‘github.com’)) {
      $licenseUrl = $licenseUrl.replace(“/blob/”, “/raw/”)
      }
      $extension = “.txt”
      if ($licenseUrl.EndsWith(“.md”))
      {
      $extension = “.md”
      }
      # Download File
      (New-Object System.Net.WebClient).DownloadFile($licenseUrl, (Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension);
      $licenseText = get-content “$((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension)”
      # if the content is HTML content rename extension to .html
      if( ($licenseText | Select-String “<html”) -ne $null )
      {
      get-item $((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension) | Rename-Item -NewName { $_.Name -replace $extension,’.html‘ }
      $extension = “.html”
      }
      # Delete the license file downloaded.
      # Remove-Item $((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension) -ErrorAction SilentlyContinue -Force
      }
      }
      Catch [system.exception]
      {
      Write-Host (“Could not download license for “ + $pkg.Id)
      }
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Also if you have more than one you want to exclude, you may use this instead.

      PowerShell
      53 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      New-Item ItemType Directory Force Path “.\licenses”;
      $excluded = “Test-Projects”,“Another Project”,“Definitely dont want this one”
      @( Get-Project All | ? { $_.ProjectName -and $_.ProjectName -notin $excluded} | % {
      Get-Package ProjectName $_.ProjectName | ? { $_.LicenseUrl }
      } ) | Sort-Object Id Unique | % {
      $pkg = $_;
      Try
      {
      if ($pkg.Id -notlike ‘*Microsoft*’ -and $pkg.LicenseUrl.StartsWith(‘http’))
      {
      Write-Host (“Download license for package “ + $pkg.Id + ” from “ + $pkg.LicenseUrl);
      #Write-Host (ConvertTo-Json ($pkg));
      $licenseUrl = $pkg.LicenseUrl
      if ($licenseUrl.contains(‘github.com’)) {
      $licenseUrl = $licenseUrl.replace(“/blob/”, “/raw/”)
      }
      $extension = “.txt”
      if ($licenseUrl.EndsWith(“.md”))
      {
      $extension = “.md”
      }
      # Download File
      (New-Object System.Net.WebClient).DownloadFile($licenseUrl, (Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension);
      $licenseText = get-content “$((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension)”
      # if the content is HTML content rename extension to .html
      if( ($licenseText | Select-String “<html”) -ne $null )
      {
      get-item $((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension) | Rename-Item -NewName { $_.Name -replace $extension,’.html‘ }
      $extension = “.html”
      }
      # Delete the license file downloaded.
      # Remove-Item $((Join-Path (pwd) ‘licenses\’) + $pkg.Id + $extension) -ErrorAction SilentlyContinue -Force
      }
      }
      Catch [system.exception]
      {
      Write-Host (“Could not download license for “ + $pkg.Id)
      }
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Exactly what I wanted! Thank you very much! ++

    • #237832
      Participant
      Topics: 6
      Replies: 552
      Points: 2,090
      Helping Hand
      Rank: Community Hero

      You are very welcome! Have a fantastic day.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.