Using Powershell to Register a file in the Gac
-
21-08-2019 - |
Question
Is there a simple way to in PowerShell (I imagine using gacutil.exe) to read from a text document a path\assembly and register it in the GAC? So for example a .txt file that looks like:
c:\test\myfile.dll
c:\myfile2.dll
d:\gac\gacthisfile.dll
The PowerShell script would read that into a stream and then run gacutil on each of those assemblies found? I guess it would be something like:
#read files into array?
foreach ($file in Get-ChildItem -Filter "*.dll" )
{
Write-Host $file.Name
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $file.Name
}
Solution
If you sort out your text file such that the each dll is on a separate line, you could use the Get-Content command and pipe each to a filter that did your command:
filter gac-item { C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $_}
get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | gac-item
OTHER TIPS
How about let the .Net worry about gacutil?
# load System.EnterpriseServices assembly
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") > $null
# create an instance of publish class
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish
# load and add to gac :)
get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | Foreach-Object {$publish.GacInstall($_)}
I would suggest calling the function to add an assembly to the GAC something following PowerShell guidelines like Add-GacItem. Also the location of gacutil.exe varies based on your system. If you have VS 2008 installed, it should be at the location shown below.
function Add-GacItem([string]$path) {
Begin {
$gacutil="$env:ProgramFiles\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe"
function AddGacItemImpl([string]$path) {
"& $gacutil /nologo /i $path"
}
}
Process {
if ($_) { AddGacItemImpl $_ }
}
End {
if ($path) { AddGacItemImpl $path }
}
}
Get-Content .\dlls.txt | Split-String | Add-GacItem
Note that the Split-String cmdlet comes from Pscx. The function isn't super robust (no wildcard support doesn't check for weird types like DateTime) but at least it can handle regular invocation and pipeline invocation.
Do you want to replace gacutil.exe? If not, why not use gacutil's included /il switch?
From the gacutil /h:
/il <assembly_path_list_file> [ /r <...> ] [ /f ]
Installs one or more assemblies to the global assembly cache.
<assembly_list_file> is the path to a text file that contains a list of
assembly manifest file paths. Individual paths in the text file must be
separated by CR/LF.
Example: /il MyAssemblyList.txt /r FILEPATH c:\projects\myapp.exe "My App"
myAssemblyList.txt content:
myAsm1.dll
myAsm2.dll
If you create an alias in your profile (just type $profile at a ps prompt to determine this file location) like so new-alias "gac" ($env:ProgramFiles+"\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe")
then you can use gac like so:
get-childitem $basedirectory "*$filter.dll" | foreach-object -process{ WRITE-HOST -FOREGROUND GREEN "Processing $_"; gac /i $_.FullName /f}
the last part is the most important. it calls gacutil with the switches you want.
Hope this helps.
This PowerShell script will add assemblies to the GAC without using GacUtil. http://blog.goverco.com/2012/04/use-powershell-to-put-your-assemblies.html
After downloading the Add-AssemblyToGlobalAssemblyCache.ps1 you can deploy to the gac.
Usage example for adding multiple assemblies Dir C:\MyWorkflowAssemblies | % {$_.Fullname} | .\Add-AssemblyToGlobalAssemblyCache.ps1
See the full documentation by running Get-Help .\Add-AssemblyToGlobalAssemblyCache.ps1 -Detailed
Not wanting to install the Windows 8 SDK on all machines I needed to put assemblies in the GAC to get gacutil, I've written a powershell module using the GAC API. It works with any .Net version. With PowerShell GAC you can do it like so:
Get-Content ListOfAssemblies.txt | Add-GacAssembly