调用Powershell相关类库的版本问题:Could not load file or assembly System.Management.Automation, Version=3.0.0.0

今天有一部分测试用例Fail了。

从Case的日志看都是PS检查不成功的原因,但是在自己的coding机器上面却无法重现类似场景,于是又跑到测试机器上面去调查。

在测试机器上面的调试发现这样的错误:System.IO.FileNotFoundException: Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified

由此确定是加载类库的版本不对导致的。

根据MSDN提供的信息,System.Management.Automation.dll是PowerShell的一个重要类库,该加载版本属于Powershell3.0,而由于测试机器上面都为PowerShell2.0的缘故测试的Cases就失败了。

三个Solutions:

1. 根据运行机器上面Powershell版本,使测试代码运行的时候动态加载引用,只需

a. 判断Powershell的版本:


PS C:\OpConsoleInstaller> get-host
Name             : ConsoleHost
Version          : 2.0
InstanceId       : 004d3b48-755e-416b-811a-8a3832115100
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : zh-CN
CurrentUICulture : zh-CN
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
PS C:\OpConsoleInstaller> $host
Name             : ConsoleHost
Version          : 2.0
InstanceId       : 004d3b48-755e-416b-811a-8a3832115100
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : zh-CN
CurrentUICulture : zh-CN
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
 
PS C:\OpConsoleInstaller> $psversiontable
Name                           Value
—-                           —–
CLRVersion                     2.0.50727.4927
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

b. 使用Assembly.Load();

2. 使powerShell自动映射两个版本,只需:

a. create powershell.exe.config with following 

<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>

Copy the file into powershell root directory.[C:\Windows\System32\WindowsPowerShell\v1.0]

b. Run following from command line 

reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1

3. 最简单的解决方法是把测试机器升级为Powershell3.0。这个办法简单,但不是万能药,且并不能作为问题最终的解决方案,仅仅在测试版本猜想以及适当情景下推荐使用。

如何升级Powershell到3.0可参阅MSDN:http://technet.microsoft.com/en-us/library/hh847837.aspx


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值