PowerShell

342 次浏览

获取项目中全部 NuGet 包以及对应的 License

我们在日常 .NET Core 项目开发的过程中,经常需要获得整个解决方案中所有项目所引用的 NuGet 包的列表、对应的 License,甚至有时候出于法律的考量,我们需要检查 NuGet 包中所有递归引用的内部包的 License。

如果我们手动进行检查,工作量是巨大的,因此我编写了一个 PowerShell 脚本,来自动化完成这个工作。

基本原理

首先,基于 dotnet 命令自带的 list package 方法,及 –include-transitive 参数,来获取全部包引用的列表。
随后,通过 WebClient 下载对应的 License 文件。

使用方法

将 .ps1 文件放置到项目解决方案文件 (.sln) 所在位置并运行即可。

*

In the process of daily .NET Core project development, we often need to obtain a list of NuGet packages referenced by all projects in the solution, corresponding licenses, and sometimes for legal considerations, we need to check all licenses of recursion internal packages in each directly referenced NuGet packages.

If we check it manually, the workload is huge, so I wrote a PowerShell script to automate this task.

Fundamental

It’s based on the list package method that comes with the dotnet command, and the –include-transitive parameter, to obtain a list of all package references. And then, download the corresponding license file through WebClient.

Instructions

Place the .ps1 file in the location of the project solution file (.sln) and run it.

*

# Place the file to .sln folder, and run

dotnet list package --include-transitive > NuGetPackageList.txt

mkdir ".\licenses" > nul 2> nul
echo "Package ID,Package Version,Package Spec URL,License URL" > LicenseList.csv

Select-String "(>)\s+(\S+)\s+(\S+)\s+(\S+)*" NuGetPackageList.txt  |  Select @{Name="Id"; Expression = {$_.Matches.Groups[2]}}, @{Name="Version"; Expression = {$_.Matches.Groups[3]}}  |  Sort-Object -Property @{Expression="Id"; Descending=$false}, @{Expression="Version"; Descending=$false} -unique | % {

    $packageId = $_.Id;
    $packageIdLowerCase = $packageId.ToString().ToLower();
    $packageVersion = $_.Version;
    $packageSpecUrl = "https://api.nuget.org/v3-flatcontainer/$packageIdLowerCase/$packageVersion/$packageIdLowerCase.nuspec"

    Write-Host
    Write-Host("o> $packageId")
    Write-Host("  $packageVersion`n  $packageSpecUrl") -ForegroundColor Green 

    Try {  
        # Refer to https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-manifest-nuspec
        [xml]$packageSpec = (New-Object System.Net.WebClient).DownloadString($packageSpecUrl);
        $packageLicenseUrl = $packageSpec.package.metadata.licenseUrl;

        Write-Host("  $packageLicenseUrl") -ForegroundColor Green 
        echo "$packageId,$packageVersion,$packageSpecUrl,$packageLicenseUrl" >> LicenseList.csv
        
        Try {
            $filePath = (Join-Path (pwd) 'licenses\') + "$packageId.$packageVersion.txt";
            (New-Object System.Net.WebClient).DownloadFile($packageLicenseUrl, $filePath);
        }
        Catch [system.exception] {
            # Write-Host ($error[0].Exception);
            Write-Host ("!!> Could not download license file for $packageId $packageVersion") -ForegroundColor DarkMagenta
        }
    }
    Catch [system.exception] {
        # Write-Host ($error[0].Exception);
        Write-Host ("!!> Could not read license for $packageId $packageVersion") -ForegroundColor Red
        echo "$packageId,$packageVersion,$packageSpecUrl,(none)" >> LicenseList.csv
    }

}
1,394 次浏览

Dynamics 365: Three way to get organization unique name

We have three ways to get organization unique name in Microsoft Dynamics 365.

A. Just click on the top right corner of the user avatar, and right here (org64e4ed31).

B. Get it with PowerShell, At first, you need to download the Dynamics 365 SDK to local, and then Run the PowerShell as administrator, cd to the SDK folder, and run .\RegisterXRMTooling.ps1

Add-PSSnapin Microsoft.Xrm.Tooling.Connector
Add-PSSnapin Microsoft.Xrm.Tooling.PackageDeployment

$Cred = Get-Credential
Get-CrmOrganizations -Credential $Cred -DeploymentRegion NorthAmerica –OnlineType Office365

C. Get it from the settings page of the Dynamics 365

Click Settings -> Customization -> Developer Resources -> Unique Name

3,974 次浏览

PowerShell: 隐藏明文密码

本文描述如何加密 PowerShell 中的敏感信息,从而避免敏感信息(诸如密码)在代码中被显式的展现出来。这里我们使用 ConvertFrom-SecureString 命令来完成加密操作,需要注意的是,如果我们没有指定Key,那么,将采用基于 Windows 内置的 DPAPI 进行数据的加密。这种情况下,原文的加密和解密必须在同一台机器上,且基于同一个User去进行。

ConvertFrom-SecureString [-SecureString] <SecureString> [[-SecureKey] <SecureString> ] [ <CommonParameters>]

你可以从这里了解该命令的更多信息。

  • If no key is specified, the Windows Data Protection API (DPAPI) is used to encrypt the standard string representation.
    Your secret was automatically encrypted by the built-in Windows data protection API (DPAPI), using your identity and your machine as encryption key. So only you (or any process that runs on your behalf) can decipher the secret again, and only on the machine where it was encrypted.
    Continue reading
2,628 次浏览

PowerShell: 远程调用

通过本地程序调用 PowerShell 自定义脚本,我们可以有周期、计划性的执行一些扩展的操作,这在一定程度上提高了本地程序自身逻辑的扩展性。而在编写一段 PowerShell 脚本的过程中,也难免会遇到需要一段脚本在另一台计算机上远程执行的情形。例如,我们在执行一段 PowerShell 脚本时,需要向某个 AD Group 中添加一个 AD User,而这一操作需要在域控机器上才能执行,而此时,我们就会用到 PowerShell 远程调用执行代码。

在调用之前,我们需要保证 PowerShell 的远程调用设置是开启的。
1 启用远端计算机的 Windows Remote Management (WS-Management) service.
(1) 通过 services.msc 启动服务管理器
(2) 在其中找到 Windows Remote Management (WS-Management) 这一项
(3) 确定该项服务正处于运行状态,并且启动类型被设置为 Automatic

2 设定本地组策略.
(1) 通过 gpedit.msc 开启本地组策略编辑器
(2) 找到 Computer Configuration > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM
Service
(3) 在右侧的项目中,双击 “Allow remote server management through WinRM”
(4) 在新弹出的窗口中,选择Enabled,从而启用策略,然后再Options的设置区域,位IPv4和IPv6 filter添加规则”*”,从而使所有IP都不受到过滤器的限制。当然,这里也可以根据具体的环境进行配置,相关配置可参考下图。
Continue reading