如何使用C#编写的Win表单运行Powershell脚本 [英] How to run a powershell script from a win form written in C#

查看:50
本文介绍了如何使用C#编写的Win表单运行Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我拥有一些开发技能,但是自从我使用它们已经很久了.现在,我正在尝试自动执行服务器应用程序的安装.所以我在这里尝试做的事情,而我已经搜索了一段时间了,是打开一个Powershell脚本(我已经创建了该脚本,并将其添加到项目中.我已经添加了所有代码,因此您可以看到我要去哪里.脚本完成后,我需要if()来显示脚本是否正确完成.

first of all, I've got a bit of development skills but it's been ages since i've use them. Now i'm trying to automate an install for a server application. So what i'm trying to do here, and I've been googling for a while now, is to open a Powershell script (that I already created, and added to the project. I've added all the code so you can see where I'm going wrong. Once the script is done I need the if () to show me if it's been completed correct or not.

下面是代码:

 using System; 
 using System.Collections.Generic; 
 using System.ComponentModel; 
 using System.Data; 
 using System.Drawing; 
 using System.Linq; 
 using System.Text; 
 using System.Threading.Tasks; 
 using System.Collections.ObjectModel; 
 using System.Windows.Forms;

 namespace WindowsFormsApplication1 {

 public partial class Form3 : Form
 {
     public Form3()
     {
         InitializeComponent();
     }

     //here i want to launch the powershell script
 private void progressBar1_Click(object sender, EventArgs e)
     {
         if ( )
         {

         }
         else {
             MessageBox.Show("There is an error in the application or data", "Prerequisite",
             MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                 Application.Exit();
              }
     }

 } }

推荐答案

我认为您需要使用"PowerShellInstance.Invoke();"

I think you need to use "PowerShellInstance.Invoke();"

Microsoft在此处提供了很棒的教程. https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

Microsoft has a great tutorial here. https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

这篇关于如何使用C#编写的Win表单运行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆