I'm using a .ps1 script to register a set of .NET 4.0 assemblies to the GAC on a production Win 2008 server r2. The assemblies have strong names and the script returns no errors:

[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices");
[System.EnterpriseServices.Internal.Publish] $publish = New-Object System.EnterpriseServices.Internal.Publish;

$publish.GacInstall("D:\MyComponents\EZTrac.Domain.Dealer.dll")

After I run this I look in GAC_MSIL (where it should be), and then GAC_32 and GAC_64 for good measure, but it is not in any of these.

I used this post as a guide. Any idea what I'm forgetting here?

有帮助吗?

解决方案 2

After testing this out, my comments under the original question are correct. The problem was that v2 of PowerShell cannot register .NET 4.0 components. In order to do that you have to install either WMF 3 or WMF 4 so that you get Powershell 3 or 4.

其他提示

Here is a small util function I wrote for gaccing assemblies. It does a few check before doing so:

function Gac-Util
{
    param (
        [parameter(Mandatory = $true)][string] $assembly
    )

    try
    {
        $Error.Clear()

        [Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
        [System.EnterpriseServices.Internal.Publish] $publish = New-Object System.EnterpriseServices.Internal.Publish

        if (!(Test-Path $assembly -type Leaf) ) 
            { throw "The assembly $assembly does not exist" }

        if ([System.Reflection.Assembly]::LoadFile($assembly).GetName().GetPublicKey().Length -eq 0 ) 
            { throw "The assembly $assembly must be strongly signed" }

        $publish.GacInstall($assembly)

        Write-Host "`t`t$($MyInvocation.InvocationName): Assembly $assembly gacced"
    }

    catch
    {
        Write-Host "`t`t$($MyInvocation.InvocationName): $_"
    }
}

As you noted in the comments: Using System.EnterpriseServices.Internal.Publish in .Net 2.0 does not allow you to register .Net 4.0 assemblies and it returns no errors if anything went wrong. Updating to PowerShell v3 or higher or running older PowerShell versions using .Net 4.0 is a solution. Read more about it here.

Other alternatives are using .Net 4.0 gacutil or have a look at my PowerShell GAC module which is able to install .Net 4.0 assemblies in PowerShell v2 and does give errors if anything went wrong.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top