我正在从C#控制台应用程序运行PowerShell。我的dev box安装了PowerShell 3.0 --目标机器可能安装了v2.0。我的项目文件有一个对程序集的引用:
<Reference Include="System.Management.Automation" />这似乎是推荐的方式。当我调用runspace.Open()时,我得到了一个System.IO.FileNotFoundException:
using (var runspace = RunspaceFactory.CreateRunspace(initialSessionState))
{
runspace.AvailabilityChanged += runspace_AvailabilityChanged;
runspace.StateChanged += runspace_StateChanged;
runspace.Open();
...
}异常详细信息包括:
消息:无法加载文件或程序集“Microsoft.PowerShell.Security”或其依赖项之一。系统找不到指定的文件。
FusionLog:
=== Pre-bind state information ===
LOG: User = MYDOMAIN\MyUser
LOG: DisplayName = Microsoft.PowerShell.Security
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.PowerShell.Security | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\MyUser.MYDOMAIN\Projects\PoSh\PoShRunner1\bin\Debug\PoShRunner1.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.EXE.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.EXE.如果我在VS2012中关闭公共语言运行时异常,我会得到
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.DllNotFoundException' occurred in System.Management.Automation.dll
A first chance exception of type 'System.Management.Automation.Host.HostException' occurred in System.Management.Automation.dll在输出日志中。
我看过fusion日志中引用的文档,但其中大部分似乎不适用于这种情况,对我的理解也没有太大帮助。Microsoft.PowerShell.Security在GAC中,那么有什么问题呢?
发布于 2013-05-13 03:26:02
问题是您使用Powershell3.0 system.management.automation (s.m.a.)进行编译。assembly - 3.0.0.0 -而不是1.0.0.0版本的powershell 2.0 s.m.a。
打开您的项目,浏览到1.0.0.0版本并重新编译。GAC3.0将1.0.0.0和3.0.0.0 s.m.a程序集安装到PowerShell。然后,您的程序将在装有2.0或3.0版本的powershell的系统上运行。
https://stackoverflow.com/questions/16506366
复制相似问题