2010-09-03 50 views
0

我想用System.EnterpriseServices卸載c#com +組件,替換dll並重新安裝新版本。System.EnterpriseServices和Powershell鎖定dll

問題是,當我到達行副本項時,腳本總是失敗,因爲System.EnterpriseSerivces正在鎖定目標文件。如果我將腳本分成兩部分,一部分調用UnistallAssembly,另一部分執行副本並調用InstallAssembly,一切正常。

強制system.enterpriseservices發佈dll的任何想法?

$comRoot = "C:\Comroot\" 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") 
[System.String]$applicationName = "My App Name"; 
[System.String]$typeLibraryName = $null; 

$objAdmin = new-object -com COMAdmin.COMAdminCatalog 
$objAdmin.ShutdownApplication("$applicationName"); 
$objAdmin = $null 

$helper = New-Object System.EnterpriseServices.RegistrationHelper 
$helper.UninstallAssembly("$comRoot\$StepName.dll", $applicationName) 
$helper = $null 
$applicationName = $null 
[gc]::collect() 
[gc]::WaitForPendingFinalizers() 

"SOURCE : $BranchPath\Steps\$StepName\bin\Debug\" 
"DESTINATION : $comRoot" 
copy-item "$BranchPath\Steps\$StepName\bin\Debug\*$StepName*" -destination "$comRoot" - force 

$helper = New-Object System.EnterpriseServices.RegistrationHelper 
$helper.InstallAssembly("$comRoot\$StepName.dll", [ref] $applicationName, [ref]  $typeLibraryName, [System.EnterpriseServices.InstallationFlags]::ConfigureComponentsOnly); 
"Install Complete <$typeLibraryName>" 
Read-Host "Press any key to exit" 

回答

0

我不知道是否有可能在您嘗試複製時卸載未完成?也就是說,調用UninstallAssembly同步還是異步?解決「正在使用的文件」問題的一種方法是嘗試使用類似SysInternals MoveFile.exe的工具重命名文件,然後安裝新版本。

+0

對不起延遲迴到你..它被鎖定,直到腳本執行..如果我把一個斷點,並等待2-3分鐘問題仍然存在..一旦腳本退出鎖定消失。 – Mel 2010-09-16 09:44:21

+0

我來看看MoveFile.exe – Mel 2010-09-16 09:44:41

+0

發現這也是:http://stackoverflow.com/questions/1751006/system-enterpriseservices-registrationhelper-doesnt-release-file它似乎有完全相同的問題 – Mel 2010-09-16 09:46:18