【发布时间】:2015-10-13 23:31:22
【问题描述】:
大家好,我是 C# 新手。我知道powershell。我试图弄清楚如何从 c# 程序运行 powershell 脚本。我在下面为自己做一个测试程序。但是,当我尝试放入更复杂的脚本时,那些可以包含应该弹出的 GUI 窗口的脚本似乎没有运行。我在正确的轨道上解决这个问题吗?我还注意到,在 C# 中正确运行脚本需要每行后面的分号。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (PowerShell PS = PowerShell.Create())
{
PS.AddScript("$file = \"C:\\test\\csharp.txt\";"+
"Set-Content -path $file -value \"testc\";");
PS.Invoke();
}
}//end of main
}//end of class
}//end of namespace
【问题讨论】:
-
这里有一个很好的教程:codeproject.com/Articles/18229/…
-
感谢我之前阅读了该页面
标签: c# powershell