使用 Powershell 在 Windows Server 2008 R2 上的 GAC 中安装 dll
Posted
技术标签:
【中文标题】使用 Powershell 在 Windows Server 2008 R2 上的 GAC 中安装 dll【英文标题】:Install dll in GAC on Windows Server 2008 R2 using Powershell 【发布时间】:2018-07-05 02:47:42 【问题描述】:所以我有这个 Powershell 脚本可以在 gac 中安装 MySql dll。
[System.Reflection.Assembly]::Load("System.EnterpriseServices,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Program Files (x86)\mysql\MySQL Connector Net 6.9.9\Assemblies\v4.5\MySql.Data.Entity.EF6.dll")
此脚本在 Windows Server 2012 R2 上运行良好,但在 Windows Server 2008 r2 上抛出异常。
使用“1”参数调用“加载”的异常:“无法加载文件或 程序集'System.EnterpriseServices,版本=4.0 .0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其其中之一 依赖关系。系统找不到指定的文件。”在 C:\Users\Administrator\Desktop\MyScript.ps1:2 char:35 + [System.Reflection.Assembly]::Load
1) Powershell 以管理员身份运行
2) System.EnterpriseServices存在于4.0.0版本的系统中(位置:C:\Windows\Microsoft.NET\assembly\GAC_64\System.EnterpriseServices)
3) GacUtil 不是一个选项,因为 sdk 在生产环境中不可用。
在 windows server 2008 r2 上安装 Gac 有什么限制吗?
为什么在 windows server 2012 r2 中运行的脚本在 windows server 2008 r2 上不运行?
我是否缺少服务器上的任何设置以使其正常工作?
【问题讨论】:
可以去掉程序集名称的版本部分吗? 试过这样“System.EnterpriseServices, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”,同样的错误 只是System.EnterpriseServices
同样的错误:使用“1”参数调用“加载”的异常:“无法加载文件或程序集‘System.EnterpriseServices’或其依赖项之一。系统找不到文件指定的。”在 C:\Users\Administrator\Desktop\MySqlEf6Gac.ps1:2 char:35 + [System.Reflection.Assembly]::Load
@DanielA.White 虽然如果我使用程序集版本 2.0.0 作为 [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken= b03f5f7f11d50a3a") ,脚本成功运行,但 GAC 中没有安装任何内容。在事件查看器中,我收到警告:在全局程序集缓存中安装失败:C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5\MySql.Data.Entity.EF6.dll:来源: COM+ SOAP 服务
【参考方案1】:
我找到了答案:
Windows Server 2008 r2 上的 Powershell 使用 CLR 版本 2.0,并且 System.Enterpriseservice dll 是使用 CLR 版本 4.0 创建的。 windows server 2012 r2 上的 Powershell 版本是 4.0,这就是为什么这个脚本在 windows server 2012 r2 上运行良好的原因。 可以使用以下命令检查版本:$psversiontable
因此,powershell 无法加载此 dll。
现在如何解决此错误:更改 powershell 版本以使用 CLR 4.0
如何: https://community.dynamics.com/ax/b/daxbeginners/archive/2013/12/08/this-assembly-is-built-by-a-runtime-newer-than-the-currently-loaded-runtime-and-cannot-be-loaded
运行脚本以更改 PS CLR 版本:
打开记事本并粘贴以下代码:
$config_text = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
"@
$config_text| Out-File $pshome\powershell.exe.config
$config_text| Out-File $pshome\powershell_ise.exe.config
保存,然后将文件扩展名重命名为 .PS1。以管理员身份运行。
偶然发现这篇文章解释了有关错误的所有内容以及如何更改 Powershell 版本。
希望这可以帮助某人:)
【讨论】:
【参考方案2】:“请重新安装您的 .NET Framework(3.5 或更高版本),重新启动计算机并重试。” https://community.powerbi.com/t5/Desktop/Unnable-to-connect-to-SQL-Server-2012/m-p/177331 “安装了框架 4.5 并启动系统,现在它可以工作了。”
【讨论】:
以上是关于使用 Powershell 在 Windows Server 2008 R2 上的 GAC 中安装 dll的主要内容,如果未能解决你的问题,请参考以下文章
如何使用XenDesktop的PowerShell來做Update Machines--限定Pool
Python 可以从 Windows Powershell 命名管道中读取吗?
powershell 使用PowerShell配置IP地址(在Windows上)