2012-03-08 22 views
0

我試圖找到使用Powershell,部署的Sharepoint解決方案的AssemblyFileVersion。從GetReferencedAssemblies獲取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),但它不起作用。我認爲這是一個共享點解決方案,因爲它對此產生影響。

回答

2

我正在尋找一個解決方案,並找到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緩存在那裏,以便它最終結束:)

+0

感謝您的幫助。我嘗試過但它不起作用。問題是Sharepoint解決方案中的引用程序集不會發布到GAC。所以當ReflectionOnlyLoad命中他們時,他們不在那裏,是給出了「無法加載文件或程序集」錯誤。我可以發佈到GAC,但我試圖找到一種方法來不改變現有的項目 – 2012-03-09 14:13:53

+0

好吧,在已知文件夾中的程序集?我問,因爲如果你知道位置,你可以加載它們並只過濾那些被引用的。 – stej 2012-03-09 15:42:39

+0

他們可能是,但它可能是在詼諧的Sharepoint Wive – 2012-03-09 16:14:03

0

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