3

我正在从 C# 控制台应用程序运行 PowerShell。我的开发盒有 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”或其依赖项之一。该系统找不到指定的文件。

融合日志:

=== 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.

如果我在 VS 2012 中关闭公共语言运行时异常,我会得到

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

在输出日志中。

我查看了融合日志中引用的文档,但其中大部分似乎不适用于此案例或对我的理解有很大帮助。Microsoft.PowerShell.Security 在 GAC 中,所以有什么问题?

4

1 回答 1

2

问题是您针对 powershell 3.0 system.management.automation (sma) 程序集 - 3.0.0.0 - 而不是 powershell 2.0 sma (即 1.0.0.0)进行编译。

打开您的项目,浏览到 1.0.0.0 版本并重新编译。PowerShell 3.0 将 1.0.0.0 和 3.0.0.0 sma 程序集安装到 GAC。然后您的程序将在具有 2.0 或 3.0 版本的 powershell 的系统上运行。

于 2013-05-12T19:26:02.390 回答