System: Windows 7 Professional 64 bit, PowerShell v 2.0, no Visual Studio (can't be installed and others too)
Trying to run PowerShell from C#. This is the code snippet:
using System;
using System.Management.Automation;
class Hello {
static void Main(string[] args) {
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process");
Console.WriteLine("Process Id");
Console.WriteLine("----------------------------");
foreach (PSObject result in ps.Invoke()) {
Console.WriteLine(
"{0,-24}{1}",
result.Members["ProcessName"].Value,
result.Members["Id"].Value);
}
}
}
Error:
e:\foo.cs(2,25): error CS0234: The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)
Since no Visual Studio is there, I am running code in raw manner. Because of error, I downloaded dll from http://www.dll-found.com/system.management.automation.dll_download.html and placed in dir as per instruction. After rebooting machine, there was no success.
First, I want to ask a general question. How to install missing assembly or dll file (only), because for some you might have to install whole Windows or PowerShell SDK or .NET Framework.
EDIT
I have place downloaded dll file in C:\Windows\SysWOW64, C:\Windows\system32, C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5 and C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0.
I am compiling using: C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:exe /out:E:\foo.exe E:\foo.cs
WARNING: Never ever download DLLs from random websites in any sane situation. Use NuGet packages (or whatever your preferred package manager is for your project) whenever possible. If a DLL is not found, there is almost always a good reason for it, and you need to find out what it is and fix it, not just get the DLL from the internet.
If you have this problem after re-targeting an old project to, for example, .NET 4.8, then it is because NuGet package names have changed.
System.Management.AutomationMicrosoft.PowerShell.5.1.ReferenceAssemblies (NuGet)This package name appears in the documentation of classes in the namespace System.Management.Automation.
NuGet worked for me.
PM> Install-Package System.Management.Automation.dll -Version 10.0.10586
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With