13

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
}
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
  • 1
    So is this a question or a statement? Did you run that? Did it work? What was the problem? I can tell you that it won't run as written. – EBGreen Mar 24 '09 at 20:30
  • This library helps a lot: https://powershellgac.codeplex.com/ – Chris S Aug 04 '14 at 12:40

7 Answers7

24

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($_)}
  • 1
    Found this related idea: http://weblogs.asp.net/adweigert/archive/2008/10/31/powershell-install-gac-gacutil-for-powershell.aspx – Mike Schenk Apr 18 '11 at 20:07
5

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
zdan
  • 28,667
  • 7
  • 60
  • 71
  • ahh it was meant to be on a sep. line but seems it got put together on the site..let me try this –  Mar 24 '09 at 20:47
  • 1
    awesome..worked like a charm. FYI for anyone that needs to know in case you want to gac a 2.0 assembly you need to point to the gacutil.exe for .NET 2.0. –  Mar 24 '09 at 20:59
  • You could also improve this by creating an alias for gacutil, which would make it easier to change the version you're targeting and make the filter more readable. – Charlie May 15 '09 at 17:44
3

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.

Keith Hill
  • 2,329
  • 1
  • 19
  • 7
2

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
Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
1

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

Jan H
  • 4,287
  • 5
  • 24
  • 34
1

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.

Pete Skelly
  • 775
  • 6
  • 16
0

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 
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143