GAC installation in many ways!

Posted: November 29, 2011 in .Net Framework, .Net Tools, PowerShell
Tags: , , , ,

Every BizTalk Developer knows that one of the tedious tasks during BizTalk Development is Deployment, as a Developer you will find yourself spending most of your time deploying / redeploying your applications.
One strategy I used to save some time from this tasks is just to Rebuild non BizTalk artifacts such as Dataaccess, Helper and Pipeline Component, then just use gacutil.exe to install the latest build to GAC.

Over the years I have collected several ways on how to automate the boring task of installing to GAC. I listed some of them below for reference.

The great gacutil.exe that everyone knows.

Using Windows Folder Context Menu.

My favorite of them all, Powershell!

And Below is my modified version of the same powershell script that allows pipeline input and selection of framework version.

        [parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$True)]
		[Parameter(Position=1)] [String] $CLRVersion = ""
	Write-Host "Using ToolVersion: $CLRVersion"

FUNCTION Invoke-CLR4PowerShellCommand {
    PARAM (


    if ($PSVersionTable.CLRVersion.Major -eq 4) {
        Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList

    $RunActivationConfigPath = $Env:TEMP | Join-Path -ChildPath ([Guid]::NewGuid())
    New-Item -Path $RunActivationConfigPath -ItemType Container | Out-Null
<!--?xml version="1.0" encoding="utf-8" ?-->

"@ | Set-Content -Path $RunActivationConfigPath\powershell.exe.activation_config -Encoding UTF8

    $EnvVarName = 'COMPLUS_ApplicationMigrationRuntimeActivationConfigPath'
    $EnvVarOld = [Environment]::GetEnvironmentVariable($EnvVarName)
    [Environment]::SetEnvironmentVariable($EnvVarName, $RunActivationConfigPath)

    try {
        & powershell.exe -inputformat text -command $ScriptBlock -args $ArgumentList
	Catch { write-host "An Error was encountered" }
	finally {
        [Environment]::SetEnvironmentVariable($EnvVarName, $EnvVarOld)
        $RunActivationConfigPath | Remove-Item -Recurse


		[String] $NetFxVersion


	    $ErrorActionPreference = "Stop"

	    if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices, Version=$NetFxVersion, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) ) {
	        [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=$NetFxVersion, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null

	    $publish = New-Object System.EnterpriseServices.Internal.Publish


	    foreach( $file in $files ) {
	        $assembly = $file
	        if ((!$assembly) -or (-not (Test-Path $assembly -type Leaf)) ) {
	            throw "The assembly '$assembly' does not exist."

	        if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0 ) {
	          throw "The assembly '$assembly' must be strongly signed."

	        Write-Output "Copying to GAC: $assembly"

	        $publish.GacInstall( $assembly )

If ($CLRVersion -ne "")
	$sBlock = $Function:CopyToGAC
	Invoke-CLR4PowerShellCommand -ScriptBlock $sBlock -ArgumentList $AssemblyFullNames, $CLRVersion, $AssemblyPattern
	CopyToGAC $AssemblyFullNames $CLRVersion $AssemblyPattern

Did I missed some other ways? please let me know and I will be very much willing to update here.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s