Error 30 The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)
This is my C# code:
using System.Management.Automation; using System.Management.Automation.Runspaces; //----------------------------------- static void Main(string[] args) { Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript("Get-Date"); Collection<PSObject> results = pipeline.Invoke(); runspace.Close(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { Console.WriteLine(obj.ToString()); } }
Fix/Solution
The issue was fixed after I have changed the reference of System.Management.Automation to c:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll.
I have added the reference Assembly System.Management.Automation from the location “C:Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShell3.0” and my C# projects targets .NET Framework 3.5. Initially I didn’t find the the reference System.Management.Automation.dll when I search the Assemblies in Add Reference window, so that I have added the reference by manual browse option from the location “C:\Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShell3.0”.
Finally I found that the assembly file System.Management.Automation.dll (under the folder PowerShell 3.0) is supported only .NET Framework 4.0, so that have changed the reference Assembly to c:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll to fix this issue.
In visual studio direct install of "system.management.automation" from NuGet Packages won't work it will show that error.
You have to add:
Microsoft PowerShell version (5/4/3/..) Reference Assembly.
You can search for "PowerShell" in NuGet Packages Manager search bar
https://www.simple-talk.com/dotnet/net-development/using-c-to-create-powershell-cmdlets-the-basics/#first
great