Update-PackageReference.ps1

function Update-PackageReference
{
    <#
         .SYNOPSIS
         更新匹配的的包到最新版本,用于快速更新对公司内部库的引用.
          
         .DESCRIPTION
         'Microsoft.EntityFrameworkCore' | Update-PackageReference -source 'http://47.242.252.207:5000/v3/index.json'
     
    #>

    [cmdletBinding( DefaultParameterSetName = 'Instance',
                    SupportsShouldProcess = $true,
                    ConfirmImpact = 'High' )]    
    param(
        [parameter( Position = 0,
                    Mandatory = $true,
                    ValueFromPipeline = $true,
                    ValueFromPipelineByPropertyName= $true)]
        [string]
        $packageMatch,
        [parameter( Position = 1,
                    Mandatory = $true,
                    ValueFromPipeline = $true,
                    ValueFromPipelineByPropertyName= $true)]
        [string]
        $source
    )

    process
    {
        Get-ChildItem . -Include *.csproj -Recurse | foreach {
            $fileText = Get-Content $_ -Raw -Encoding UTF8
            $filePath = $_.FullName
            Get-Content $_ -Encoding UTF8 | foreach {
                if($_ -match '^\s*<PackageReference\s+Include="(?<packagename>[\w\.]+)"\s+Version="(?<packageversion>[\d\.]+)"\s+/>\s*$')
                {
                    $packagename = $matches.packagename
                    $packageversion = $matches.packageversion
                    
                    if($packagename -match $packageMatch )
                    {
                        Invoke-Expression "& dotnet remove $filePath package $packagename" | Out-Null
                        Invoke-Expression "& dotnet add $filePath package $packagename --source $source" | Out-Null
                        Write-Host -ForegroundColor Green "Update PackageReference: $($packagename) -> $([System.IO.Path]::GetFileName($filePath))"
                    }
                }
            }
        }

    }    
}