- Add-Type -TypeDefinition @"
- using System;
- using System.Management.Automation;
- using System.Linq;
-
- public class Code
- {
- public static string RunPs(string command)
- {
- using (var ps = PowerShell.Create())
- {
- var results = ps.AddScript(command).Invoke();
- return string.Join(Environment.NewLine,results.Select(delegate(PSObject pso){return pso.ToString();}).ToArray()); //测试时,结果是对的,但不能生成dll,报错
- //return results.ToString(); //测试时,结果是错的, 但是能生成dll
- }
- }
- }
- "@ -outputType library -outputAssembly "code.dll" -ReferencedAssemblies System.Core,System.Management.Automation
复制代码
|