views:

1772

answers:

3

Is there a simpe 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
}
A: 

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
+3  A: 

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
ahh it was meant to be on a sep. line but seems it got put together on the site..let me try this
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.
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
+2  A: 

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