如何通过C#.net运行PowerCli [英] How to run PowerCli through C#.net

查看:85
本文介绍了如何通过C#.net运行PowerCli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 C#.net 运行 PowerCli 工具,我不知道该怎么做。我想运行 Powercli ,我想在其中运行一些命令,这一切都必须在单击按钮下完成。



这是我的代码:



How to run PowerCli tool through C#.net, I have no idea how to do it. I want to run Powercli and i want to run some commands in it and it all has to done under single button click.

here is my code:

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.Windows.Forms;
            using System.IO;
                       
            namespace OVF_ImportExport
            {
                public partial class Form1 : Form
                {
                    string sPath = "";
                    string sName = "";
                    
            public Form1()
                    {
                        InitializeComponent();
                    }
        
                private void Form1_Load(object sender, EventArgs e)
                {
                    
                }
        
                private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
                {
                    //richTextBox1.Text = "";
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        sName = row.Cells[0].Value.ToString();
                        sPath = row.Cells[1].Value.ToString();
        
                        //richTextBox1.Text = richTextBox1.Text + sName + Environment.NewLine;
        
                    }
                }
        
                private void BtnBrowse_Click(object sender, EventArgs e)
                {
                    fbd.ShowDialog();
                    TxtBrowsepath.Text = fbd.SelectedPath;
                }
        
                private void BtnCreate_Click(object sender, EventArgs e)
                {
                    richTextBox1.Text = "";
                    StreamWriter file = new StreamWriter("Export.bat");
                    file.WriteLine("c: ");
                    file.WriteLine("cd \\");
                    file.WriteLine("cd Program Files ");
                    file.WriteLine("cd VMware");
                    file.WriteLine("cd VMware OVF Tool");
        
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        sName = row.Cells[0].Value.ToString();
                        sPath = row.Cells[1].Value.ToString();
        
                        //richTextBox1.Text = richTextBox1.Text + Environment.NewLine + sName;
                        file.WriteLine("start ovftool.exe --powerOffSource vi://" + TxtUsername.Text + ":" + TxtPassword.Text + "@"
                           + TxtIP.Text + sPath + " " + "\"" + TxtBrowsepath.Text + "\\" + sName + "\\"
                           + sName + ".ovf" + "\"" + Environment.NewLine);
        
                        //Console.WriteLine("sName: " + sName + "sPath: " + sPath);
        
                    }
                    file.WriteLine("pause");
                    file.Close();
                }
        
                       
                private void button1_Click(object sender, EventArgs e)
                {
                    string delimiter = ",";
                    string tablename = "export";
                    string filename = ("c:\\izaz\\test.csv");
        
        
                    DataSet dataset = new DataSet();
                    StreamReader sr = new StreamReader(filename);
        
                    dataset.Tables.Add(tablename);
                    dataset.Tables[tablename].Columns.Add("Name");
                    dataset.Tables[tablename].Columns.Add("Path");
                    string allData = sr.ReadToEnd();
                    string[] rows = allData.Split("\r".ToCharArray());
                    foreach (string r in rows)
                    {
                        string[] items = r.Split(delimiter.ToCharArray());
                        dataset.Tables[tablename].Rows.Add(items);
                    }
                    this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        
                }
        
            }
        
        }

推荐答案

您可以使用视图 -

使用来自.NET的VMware vSphere PowerCLI视图 [ ^ ]



以下是指南 - https:/ /www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/doc/viwin_devg.pdf [ ^ ]
You can use views -
Using VMware vSphere PowerCLI Views from .NET[^]

Here is the guide - https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/doc/viwin_devg.pdf[^]


这篇关于如何通过C#.net运行PowerCli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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