rev1be
June 22, 2020, 8:59pm
1
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?
New-Item -ItemType Directory -Force -Path ".\licenses";
@( Get-Project -All | ? { $_.ProjectName } | % {
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)
}
}
(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!
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.”
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)
}
}
Also if you have more than one you want to exclude, you may use this instead.
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)
}
}
rev1be
June 23, 2020, 10:59pm
3
[quote quote=237637]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.”
<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.
<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
[/quote]
Exactly what I wanted! Thank you very much! ++
You are very welcome! Have a fantastic day.