【发布时间】:2015-02-07 02:22:00
【问题描述】:
系统:Windows 7 Professional 64 位,PowerShell v 2.0,无 Visual Studio(无法安装等)
尝试从 C# 运行 PowerShell。这是代码sn-p:
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);
}
}
}
错误:
e:\foo.cs(2,25):错误 CS0234:命名空间“System.Management”中不存在类型或命名空间名称“Automation”(您是否缺少程序集引用?)
由于没有 Visual Studio,我以原始方式运行代码。由于错误,我从http://www.dll-found.com/system.management.automation.dll_download.html 下载了dll,并按照说明放置在目录中。重启机器后,没有成功。
首先,我想问一个一般性问题。如何安装缺少的程序集或 dll 文件(仅),因为对于某些人来说,您可能必须安装整个 Windows 或 PowerShell SDK 或 .NET Framework。
编辑
我已将下载的 dll 文件放在 C:\Windows\SysWOW64、C:\Windows\system32、C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5 和 C:\Program Files\Reference Assemblies\Microsoft\Framework \v3.0。
我正在编译使用: C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:exe /out:E:\foo.exe E:\foo.cs
【问题讨论】:
-
您是否添加了参考 System.Management.Automation.dll
-
你如何编译你的文件?如果您直接运行
csc.exe,则必须在其命令行中指定所有引用,而不仅仅是将 dll 文件“复制”到同一目录。
标签: c# .net powershell