Referencing System.Management.Automation.dll in a

2020-07-20 04:11发布

问题:

I am writing a piece of C# that will invoke PS scripts. As part of this I’m referencing System.Management.Automation.dll, the library that the PS guys expose that makes my life easier. I assume this is the right library to use (internet searching seems to confirm but if I’m wrong let me know). This library is installed as part of the PS install / Windows SDK.

I’m finding two weird things about this library:

  • I always have to reference it with a full path (even though it’s in the GAC?)
  • The path is different on different OSs

I’ve found it in the following paths (ignoring GAC paths which I assume I shouldn’t use):

  • win2k8 r2: C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0
  • Win7 x64: C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\

What's the correct way to reference this assembly in a multi-OS-version friendly way?

回答1:

The S.M.A assembly is architecture neutral (MSIL) so copy one into your solution dir (or perhaps an Imports dir in your source structure) and reference the assembly by path from that location. That is what we do on the PowerShell Community Extensions project.