【问题标题】:Get the AssemblyFileVersion from GetReferencedAssemblies从 GetReferencedAssemblies 获取 AssemblyFileVersion
【发布时间】:2012-03-26 04:44:01
【问题描述】:

我正在尝试使用 Powershell 查找已部署的 Sharepoint 解决方案的 AssemblyFileVersion。

到目前为止,我设法找到了关于解决方案本身的信息,但现在我正试图找到关于它的引用的相同信息。

有没有办法获取这些数据。

这是我目前的代码

$assembly = [System.Reflection.Assembly]::LoadWithPartialName("<AssemblyName>")
$fvi = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($assembly.Location)
Write-Host "File Version Number " $fvi.ProductVersion

$references = $assembly.GetReferencedAssemblies();
foreach ($ref in $references)
{
    Write-Host $ref.Version
}

$ref.Version 返回不同的 AssemblyVersion。

我尝试了相同的方法 ([System.Reflection.Assembly]::LoadWithPartialName),但它不起作用。我猜测这是一个共享点解决方案这一事实会对此产生影响。

【问题讨论】:

    标签: .net sharepoint powershell


    【解决方案1】:

    我正在寻找解决方案,并找到了可能对您有所帮助的 ReflectionOnlyLoad 方法。

    $processed = @{}
    function writeAssemblyFileVersions {
      param($parentAssemblyPath)
      if ($processed[$parentAssemblyPath]) {
        return
      }
      $processed.$parentAssemblyPath = 1
    
      $ver = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($parentAssemblyPath).ProductVersion
      $assembly = [reflection.assembly]::LoadFile($parentAssemblyPath)
    
      Write-Output (New-Object PsObject -Property @{Version = $ver; Assembly = $assembly})
      foreach($a in $assembly.GetReferencedAssemblies()) {
        $aForLocation = [Reflection.Assembly]::ReflectionOnlyLoad($a.FullName)
        writeAssemblyFileVersions $aForLocation.Location
      }
    }
    
    ###### sample
    $loc = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms").Location
    writeAssemblyFileVersions $loc |   
      Select Version, {$_.Assembly.ManifestModule.Name}
    

    它递归地检查所有依赖项。 $processed 缓存在那里,所以它最终结束了:)

    【讨论】:

    • 感谢您的帮助。我试过了,但它不起作用。问题是 Sharepoint 解决方案上引用的程序集没有发布到 GAC。因此,当 ReflectionOnlyLoad 击中它们时,它们不存在,会给出“无法加载文件或程序集”错误。然后我可以向 GAC 发布,但我试图找到一种不改变现有项目的方法
    • 好的,程序集是否在已知文件夹中?我问是因为如果你知道位置,你可以加载它们并只过滤那些被引用的。
    • 他们可能是,但它可能在疯狂的 Sharepoint Wive 中
    【解决方案2】:

    System.Reflection.AssemblyFileVersionAttribute 是一个自定义属性。使用此 API:

    ps> $assembly = [System.Reflection.Assembly]::LoadWithPartialName("<AssemblyName>")
    ps> $attr = $assembly.getcustomattributes(
           [reflection.assemblyfileversionattribute])[0]
    ps> $attr.version
    1.0.4.1
    

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多