pf-gac.ps1

function Add-GacAssembly_2 ($Path, [Switch]$Force, [Switch]$Verbose) {
    [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    $publish = New-Object System.EnterpriseServices.Internal.Publish
    $publish.GacInstall($Path)
    ( "GAC ADD " + $Path ) >> ( $Path + ".log" )
}

function Remove-GacAssembly{
    param (
        [Parameter(ValueFromPipeline=$true)]
        $Path, 
        [Switch]$Force
    )
    process {
        $gacutil = 'c:\code\psmodules\tools\gacutil.exe'
        #$path = "Microsoft.Practices.EnterpriseLibrary.Logging"
        Invoke-Exe $gacutil /u $path
        ( "GAC REMOVE " + $Path ) >> ( $Path + ".log" )
    }
}

function Add-GacAssembly ($Path, [Switch]$Force, [Switch]$Verbose) {

    $psFolder =  Split-Path ( Get-ScriptPath -parent ) -Parent
    $gacutil = "$psFolder\tools\gacutil.exe"
    $Path = Get-Path -path $Path

    Apply-Item_Conditional -Path $Path -action {
        $previousVersionInGAC = Get-GacAssembly -Path $Path
        $previousVersionInGAC = $previousVersionInGAC.Name | Select-Object -Unique

        if ( $previousVersionInGAC ) {
            Write-Host "Removing previous version"
            Invoke-Exe $gacutil /nologo /uf $previousVersionInGAC | Out-Null
        }

        $pathQuoted = $path | Update-String_Enclose -prefix '"'

        $output = Invoke-Exe $gacutil /nologo /if $pathQuoted

        $log = ( $Path + ".log" )
    
        ( "GAC ADD " + $Path ) > $log
        $output >> $log

        # Verify the process is correct
        if (-not ( Test-GacAssembly -Path $Path ) ) {
            throw "Failed to update GAC '$Path' "
        }
        else {
            "Test-GacAssembly OK" >> $log
        }
    }
}

function Get-GacAssembly ($Path, $name) {
    $files = @()
    if ( $Path ) {
        $dll = Get-PathItem $Path
        $files += $dll
        $name = $dll.Name
    }
    $gacDll = Get-ChildItem -Path C:\Windows\Microsoft.NET\assembly -Filter $name -Recurse
    $files += $gacDll
    write-host ( $files | Format-List | Out-String )
    return $gacDll
}

function Test-GacAssembly($Path) {
    $dll = Get-PathItem -path $Path
    $gacDll = Get-GacAssembly -path $Path

    if (-not $gacDll ) {
        return $false
    }
    
    $files = @($dll, $gacDll)
    $sameLength = ( @() + ( $files.Length | Select-Object -Unique ) ).Count -eq 1

    $fileList = $files.fullname -join "`n"
    if ( -not $sameLength ) {
        Write-Warning "Length does not corresponds for:`n$fileList"
        return $false
    }

    return $true
}
function Test-GacAssembly:::Example {
    $dllPath = 'c:\code\Deploy\CustomerControls.dll'
    Test-GacAssembly $dllPath
}