Gac/UnGac (GacUtil) from Powershell

Here’s a sample of how I GAC and UnGAC DLLs from Powershell. I think I got this on StackOverflow, but don’t have the link handy now.
(GAC, of course, refers to the .NET Global Assembly Cache. All BizTalk artificats and C# programs must be in the GAC to be called from a BizTalk orchestration).

<pre>

function GacDll ($dllpath)
{
    Write-Host " "  # blank line 
    if (!(Test-Path $dllpath)) 
       {

          Write-Host "`n dllpath not found: $dllpath  `n`n" 
          exit
       }
    [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
    $publish = New-Object System.EnterpriseServices.Internal.Publish            
    $publish.GacInstall($dllpath)
    Write-Host "Published to GAC: $dllpath" 

}

function unGacDLL ($dllpath) 
{
    if (!(Test-Path $dllpath)) 
       {

          Write-Host "`n dllpath not found: $dllpath  `n`n" 
          exit
       }
    [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
    $publish = New-Object System.EnterpriseServices.Internal.Publish            
    $publish.GacRemove($dllpath) 
    Write-Host "Removed from GAC: $dllpath" 
  
}

cls

$dllpath1 = "c:\VSTS\ABC.Oracle\ABC.OracleDB.Helpers\ABC.OracleDB.Helpers\bin\Debug\ABC.OracleDatabase.Helpers.dll"
UnGacDll $dllPath1 

$dllpath2 = "c:\VSTS\ABC.BizTalkNonDB.Helpers\ABC.BizTalkNonDB.Helpers\bin\Debug\ABC.BizTalkNonDB.Helpers.dll"
GacDll $dllPath2 
 
$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
Write-Host "`nDate/Time Completed: $date "
</pre>

Warning – may not be reliable

I have used this routine successfully at one company, but now at another with Visual Studio 2015, it frequently fails silently. In other words, it runs and acts like it works, but if you go look in the GAC (c:\windows\microsoft.net\assembly\GAC_MSIL\), the .dll date remain unchanged. See Stackoverflow question/answer for more advice.

Here are a few issues that seem to cause it to fail silently:
1) You opened PowerShell in non-Admin mode
2) You have the .DLL open in a separate Visual Studio project that references it.

Uncategorized  

Leave a Reply