今天有一部分测试用例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.1b. 使用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>