【问题标题】:how do I integrate powershell into C sharp如何将 powershell 集成到 C 语言中
【发布时间】:2017-07-18 07:49:28
【问题描述】:

我已完成所有脚本,并在 Visual Studio Enterprise 2015 中编写了整个 C# GUI。

我只需要让按钮在单击时启动特定脚本(Control.OnClick 方法?)。

我尝试寻找示例,但它们非常模糊。

希望的结果如下:

###   Begin Code   ###

##   C# psuedo code here   ##

//make C# bunifu Button 1 call script on click of button1

Invoke.OnClick Method (Or Correct option)

//Functional Example of an Invoke.OnClick Method Here doing something here. This is so I can learn and understand.

Place holder

//powershell equivalent
$SOCMonkeyDoStuff = Invoke-Item "C:\Powershell\Scripts\BecomeOneWithCodeMonkeys\Script.ps1


//Button Interaction in Powershell

$Title = "Task Menu"
$Caption = @"
1 - Get Running Services
2 - Get Top Processes
3 - Get Disk Utilization
Q - Quit

Select a choice:
"@

$coll = @()


$a = [System.Management.Automation.Host.ChoiceDescription]::new("&1 Services")
$a.HelpMessage = "Get Running Services"
$a | Add-Member -MemberType ScriptMethod -Name Invoke -Value {Get-service |     where {$_.status -eq "running"}} -force

$coll+=$a

而且我知道,是的,这段代码已经可以运行了,为什么不直接在 powershell 中执行呢?原因是我计划在不久的将来将它变成一个平台无关的工具,这样 Linux 和 Windows 都可以使用它,并且我希望尽可能保持供应商中立。此外,我计划将其导出为 .exe,以便可以在任何地方安装。

【问题讨论】:

  • 您可以使用 "Process.Start("full path here")... 作为快速 hack 调用脚本
  • @BugFinder 好的,所以我的故障是这样的。我第一次接触 Visual Studio……大约 14 小时前。我第一次做一个看起来不错的 UI 是在我安装 VS Ent 的时候。 2015. 我不知道将 process.start 代码放在哪里,尽管我相信正确的代码是:'code' process.start(""C:\Powershell\Scripts\BecomeOneWithCodeMonkeys\Script.ps1") 'code ' 对吗?
  • 您需要在该参数前面添加一个 @ 或使用双 \\ 或者是您的选择。你把 Process.Start 放在你的按钮 click .. :)
  • @Mhd 我确实找到了那篇文章,这是一篇令人难以置信的文章,所以感谢您分享。对于没有包括我迄今为止使用的所有资源,我深表歉意。我能够按照说明导入所需的所有参考资料,并找到 system.management.automation.dll 文件。我的问题是这段代码,我不知道把它放在哪里或如何让它工作。我知道命名空间声明了实例,程序定义了类,但是我该如何将它放入 bunifuflatbutton1,或者我可以吗?我不知道。哈哈

标签: c# visual-studio powershell imgur


【解决方案1】:

您可以创建一个函数,将 PowerShell 脚本的路径作为参数,然后执行该脚本。可以这么简单:

private void RunPSScript(string path)
{
    using (PowerShell ps = PowerShell.Create())
    {
        // add the script to the PowerShell instance
        ps.AddScript(path);

        // run it
        ps.Invoke();
    }
}

那么您只需要一个事件处理程序,用于按钮的OnClick 事件来调用该函数。我从未使用过 Bunifu 库,所以我不知道 OnClick 是否是正确的事件名称,但这是使用 Forms 或 WPF 的样子:

bunifuFlatButton1.OnClick += bunifuFlatButton_OnClick;

事件处理程序看起来像:

private void Button1_Click(object sender, EventArgs e)
{
    string scriptPath;
    // assign value to scriptPath here

    // then invoke the method from before
    RunPSScript(scriptPath);
}

记得在您的项目中添加对System.Management.Automation 程序集的引用(基本上是PowerShell API)

【讨论】:

  • 我不确定我是否理解这一点。请参阅下面的代码: private void RunPSSCript(stringpath) { using (PowerShell ps = PowerShell.Create()) { ps.AddScript(path); ps.invoke(); } } 命名空间 VenatorUI { 公共部分类 Form1 : Form { 公共 Form1() { InitializeComponent(); } private void bunifuFlatButton1_Click_1(object sender, EventArgs e) { string scriptPath("C:\Script\Powershellscript.ps1"); RunPSScript(scriptPath); } } }
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
相关资源
最近更新 更多