Installing assemblies in GAC

This script installs assemblies in Global Assembly Cache (GAC).This script uses gacutil.exe, so it needs to be present on the target machine of deployment. Microsoft does not recommend using gacutil.exe to install on production machines, so this script should NOT be used on production target machines. Read this to learn more about GAC and different ways of installing assemblies.

Brief notes on how to use this script:
  • Add 'assemblyPath' variable in Deployment Manager UI and set it to the assembly you want to install in GAC. For example, 'c:\bar\foo.dll' if you would like to install foo.dll
  • By default, it looks for gacutil.exe in Program Files (%programFiles%) directory, however, you can set 'gacutilPath' variable to configure the search directory. If several gacutil.exe are found, it chooses the first one in the list. Which version of gacutil should I use?
Advanced usage:
Modify the script to use /r switch of gacutil.exe to specify trace references of the assembly. The reason for doing this is here.
# Inputs (REQUIRED)
# $assemblyPath- Full path of the dll to install in GAC. For example, c:\some\path\foo.dll

# Inputs (OPTIONAL)
# $gacutilSearchPath- The path to gacutil.exe, for instance, 'c:\foo\', if gacutil.exe is located at 'c:\foo\gacutil.exe'. 

# throw error if $assemblyPath not set
if ($assemblyPath -eq $null) {
	throw "'assemblyPath' variable must be set on Deployment Manager variables page for this project. It is used to specify the full path of the dll to install in GAC."
}

if ($gacutilSearchPath -eq $null) {
	Write-Host "Optional variable, 'gacutilSearchPath', not set on Deployment Manager variables page for this project. It should be used to specify the path to gacutil.exe."
	
	$gacutilSearchPath = $env:programfiles
	Write-Host "Searching '$gacutilSearchPath' for gacutil.exe..."
	# try to find gacutil.exe
	$gacutilPaths = Get-ChildItem -Filter gacutil.exe -Path $gacutilSearchPath -Recurse  -ErrorAction 'silentlycontinue'
	
	# throw error if gacutil not found
	if ($gacutilPaths -eq $null) {
		throw "gacutil.exe not found in '$gacutilSearchPath'"
	}
	
	Write-Host "Found gacutil.exe in the following locations:"
	$gacutilPaths | foreach { $_.DirectoryName.ToString() | Write-Host }
}

$gacutilExe = ($gacutilPaths | Select -first 1 ).DirectoryName + "\gacutil.exe"
Write-Host "Using '$gacutilExe' to install assembly, '$assemblyPath' in GAC"

# install assembly using gacutil
& $gacutilExe -i $assemblyPath | Write-Host

Chirayu Shishodiya
Software Engineer - Deployment Manager
Red Gate
Sign In or Register to comment.