Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to System.Management for Powershell on C# .NET 4.0 not working

Tags:

c#

powershell

I have a .NET 4.0 C# project where I need to run Powershell via C# in powershell runspace. I had tested my code it was all working good.

When opening same project on a new computer (Win10, VS2017) and trying to run same project getting issues. I have referenced the DLL on:

C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

Also doing this using commands

using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

but System.Management.Automation and System.Management.Automation.Runspaces are not recognized.

Getting the error: The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

Somehow the references are not being used...

like image 988
MSE Avatar asked Jul 26 '17 13:07

MSE


1 Answers

Not sure what exactly happens but when I was referencing the following DLL: C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

VS was ignoring this DLL and complaining that we're missing the Automation namespace... although this DLL was showing as .NET 4.0, apparently it was compiled with a higher version of .NET which may explain why it was ignored.

To resolve the issue I had to add a reference by browsing and picking this DLL: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

which may seem like an older version of the powershell engine.

One more note which may have contributed to this is that on my new Win10 machine, I installed VS2017 so I did not manually install the .NET 4.0 FW and maybe the VS2017 installation puts higher version of the System.Management.Automation DLLs...

like image 172
MSE Avatar answered Oct 14 '22 03:10

MSE