简体   繁体   English

使用WindowsPowerShell在GAC中安装和卸载程序集

[英]Installing and uninstalling assemblies in GAC with WindowsPowerShell

On a Windows Server 2012 machine, an msi file has installed the assembly ABCD.dll into the GAC_32. 在Windows Server 2012计算机上,一个msi文件已将程序集ABCD.dll安装到GAC_32中。 I try to patch this file with WindowsPowerShell, with the following commands: 我尝试使用以下命令使用WindowsPowerShell修补此文件:

[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
$publish = New-Object System.EnterpriseServices.Internal.Publish            
$publish.GacRemove("C:\Windows\Microsoft.NET\assembly\GAC_32\ABCD\v4.0_1.0.0.0__8a93b7fd09f0e7e7\ABCD.dll")      
$publish.GacInstall("C:\Patch1\ABCD.dll")

However the GacRemove command fails. 但是,GacRemove命令失败。 In the Windows Event Log/Application I get the following message: 在Windows事件日志/应用程序中,我收到以下消息:

Removal of an assembly from the global assembly cache failed: C:\\Windows\\Microsoft.NET\\assembly\\GAC_32\\ABCD\\v4.0_1.0.0.0__8a93b7fd09f0e7e7\\ABCD.dll ABCD,Version=1.0.0.0 从全局程序集缓存中删除程序集失败:C:\\ Windows \\ Microsoft.NET \\ assembly \\ GAC_32 \\ ABCD \\ v4.0_1.0.0.0__8a93b7fd09f0e7e7 \\ ABCD.dll ABCD,Version = 1.0.0.0

Does somebody have an idea, what could be the reason? 有人有想法吗,可能是什么原因?

I tried as well the 32 bit (C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe) as well the 64 bit (C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe) WindowsPowershell in administrator mode. 我也尝试了32位(C:\\ Windows \\ SysWOW64 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe)和64位(C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe)WindowsPowershell管理员模式。

gacutil does not work either. gacutil也不起作用。 I try the following: 我尝试以下方法:

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\gacutil" -u ABCD.dll

and get the following message: 并得到以下消息:

Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.
No assemblies found matching: ABCD.dll
Number of assemblies uninstalled = 0
Number of failures = 0

However when I get the list of the assemblies with 但是当我得到的程序集列表

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\gacutil" -l

I see the following line: 我看到以下行:

ABCD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8a93b7fd09f0e7e7, processorArchitecture=x86

As mentioned in the article "Microsoft Installer does not remove assemblies from the GAC - Global Assembly Cache Pin" the problem is that the registry key in HKEY_CLASSES_ROOT\\Installer\\Assemblies\\Global\\[Assembly full name] has a value, which was set, when the assembly was installed by the msi. 如文章“ Microsoft Installer不会从GAC中删除程序集-全局程序集缓存针”一样 ,问题是HKEY_CLASSES_ROOT \\ Installer \\ Assemblies \\ Global \\ [程序集全名]中的注册表项具有已设置的值,当该组件由msi安装时。 Therefore, to patch the assembly, the registry value has to be cleared, the assembly has to be uninstalled, the patched assembly has to be installed, and the registry value has to be set to the old value. 因此,要修补程序集,必须清除注册表值,必须卸载程序集,必须安装修补后的程序集,并且注册表值必须设置为旧值。 The registry value could be left empty, but then upon uninstalling the msi, the registry key is not deleted. 注册表值可以保留为空,但是在卸载msi时,不会删除注册表项。 Therefore, it is best to set the registry value again to the old value. 因此,最好将注册表值再次设置为旧值。 I finally wrote the following VB.NET Code to patch the assembly: 我终于写了以下VB.NET代码来修补程序集:

        Dim subkey = My.Computer.Registry.ClassesRoot.OpenSubKey("Installer\Assemblies\Global", True)
        Dim keyname = "ABCD,Version=""1.0.0.0"",Culture=""neutral"",ProcessorArchitecture=""MSIL"",PublicKeyToken=""8A93B7FD09F0E7E7"""
        Dim values As String() = subkey.GetValue(keyname)
        subkey.SetValue(keyname, {""})
        Dim assemblyname =
            "C:\Windows\Microsoft.NET\assembly\GAC_32\ABCD\v4.0_1.0.0.0__8a93b7fd09f0e7e7\ABCD.dll"
        Dim p = New System.EnterpriseServices.Internal.Publish()
        p.GacRemove(assemblyname)
        p.GacInstall("D:\Patch1\ABCD.dll")
        subkey.SetValue(keyname, values)

In the above code, ABCD has to be changed to the right assembly name, and 8a93b7fd09f0e7e7 to the public key token of the assembly. 在上面的代码中,必须将ABCD更改为正确的程序集名称,并将8a93b7fd09f0e7e7更改为程序集的公钥令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM