Automating remote Global Assembly Cache (GAC) deployments using PowerShell allows you to register .NET assemblies across multiple remote Windows Servers simultaneously without manual RDP sessions. This is highly useful for managing centralized enterprise dependencies, BizTalk applications, or custom SharePoint and SSIS components.
Because gacutil.exe is a developer tool and is not installed by default on Windows Server, automation relies on pairing remote execution protocols with either portable copies of gacutil or native .NET API workarounds. 🔑 The Core Problem & Best Approaches
To register a .dll file into a remote machine’s GAC, you must address two hurdles: getting the assembly/installer files onto the remote machine, and running the command with full local Administrator permissions. There are two primary methods to build this automation:
The Native .NET Method (Recommended): Uses PowerShell’s deep integration with Windows and the .NET runtime. This eliminates the need to deploy or manage separate gacutil.exe binaries on target servers.
The Portable Gacutil Method: Copies the binary executable explicitly to the target machine alongside your assembly before executing it.
🛠️ Method 1: Automated Deployment Using Native .NET APIs
Using the native System.EnterpriseServices.Internal.Publish class is the cleanest approach. It interacts directly with the GAC under the hood.
The following automated script copies an assembly to a target computer and registers it remotely via WinRM (Invoke-Command): powershell
# Define target server and file metrics \(TargetServer = "ProductionAppServer01" \)LocalDllPath = “C:\Builds\MyCompany.Service.dll” \(RemoteTempPath = "C:\Windows\Temp\Deployments\" # 1. Create a persistent WinRM session with the remote server \)Session = New-PSSession -ComputerName \(TargetServer -ErrorAction Stop # 2. Stage the files on the remote server if (-not (Invoke-Command -Session \)Session { Test-Path \(using:RemoteTempPath })) { Invoke-Command -Session \)Session { New-Item -ItemType Directory -Path \(using:RemoteTempPath | Out-Null } } Copy-Item -Path \)LocalDllPath -Destination \(RemoteTempPath -ToSession \)Session -Force # 3. Execute GAC installation natively inside the remote session Invoke-Command -Session \(Session -ScriptBlock { param(\)TempDir, \(FileName) \)FullDllPath = Join-Path \(TempDir \)FileName try { # Load necessary framework assembly natively [Reflection.Assembly]::LoadWithPartialName(“System.EnterpriseServices”) | Out-Null # Instantiate the GAC publishing utility object \(Publisher = New-Object System.EnterpriseServices.Internal.Publish # Perform the direct GAC installation \)Publisher.GacInstall(\(FullDllPath) Write-Output "Successfully deployed \)FileName to the GAC.” } catch { Write-Error “Failed to install assembly to GAC: \(_" } finally { # Clean up the staged assembly file Remove-Item -Path \)FullDllPath -Force -ErrorAction SilentlyContinue } } -ArgumentList \(RemoteTempPath, (Split-Path \)LocalDllPath -Leaf) # 4. Clean up session Remove-PSSession -Session $Session Use code with caution. 📦 Method 2: Automated Deployment via Portable Gacutil
If your codebase relies strictly on gacutil.exe switches (such as forcing overwrites with /f or checking trace references), you can package and bundle gacutil.exe dynamically. Prerequisites [PowerShell] Remote Deployment Script
Leave a Reply