如何通过PowerShell脚本获取程序集文件属性中的AssemblyMetadataAttribute元数据特性?

摘要:在PowerShell脚本中获取程序集文件属性的指定元数据特性的方法——AssemblyMetadataAttribute
在PowerShell脚本中获取程序集文件属性的指定元数据特性的方法——AssemblyMetadataAttribute <# .SYNOPSIS 获取程序集文件属性的指定元数据特性 .DESCRIPTION 获取程序集文件属性的指定元数据特性 .PARAMETER filePath 程序集文件路径 .PARAMETER name 元数据名称 .EXAMPLE PS C:\> Get-AssemblyMetadata -filePath 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.4\System.Text.Json.dll' -name 'RepositoryUrl' https://github.com/dotnet/runtime .NOTES Additional information about the function. #> function Get-AssemblyMetadata { [OutputType([string])] param ( [Parameter(Mandatory = $true, HelpMessage = '程序集文件路径')] [Alias('p')] [string]$filePath, [Parameter(Mandatory = $true, HelpMessage = '元数据名称')] [Alias('n')] [string]$name ) if (-not (Test-Path $filePath)) { return "" } ## $asm = [System.Reflection.Assembly]::LoadFile($filePath) ## $data = $asm.CustomAttributes $buffer = [System.IO.File]::ReadAllBytes($filePath) $asm = [System.Reflection.Assembly]::Load($buffer) $data = [System.Reflection.CustomAttributeData]::GetCustomAttributes($asm) foreach ($attr in $data) { if ($attr.AttributeType.Name -eq 'AssemblyMetadataAttribute' -and $attr.ConstructorArguments.Count -eq 2) { $nameArg = $attr.ConstructorArguments[0]; $valueArg = $attr.ConstructorArguments[1]; if ($nameArg.Value -eq $name) { return $valueArg.Value } } } return "" } 使用示例 PS C:\> Get-AssemblyMetadata -filePath 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.4\System.Text.Json.dll' -name 'RepositoryUrl' https://github.com/dotnet/runtime