码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net使用powershell管理hyper-v

时间:2015-11-02 22:44:29      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        RunspaceConfiguration rconfig = RunspaceConfiguration.Create();
        PSSnapInException Pwarn = new PSSnapInException();

        Runspace runspace = RunspaceFactory.CreateRunspace();
        string test = "Import-Module VirtualMachineManager\r\n";
        runspace = RunspaceFactory.CreateRunspace(rconfig);
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(test);
        try
        {
            var results = pipeline.Invoke();

            using (Pipeline pipe = runspace.CreatePipeline())
            {
                //Start-VM -name XXXXX
Command cmd = new Command("Start-VM"); cmd.Parameters.Add("Name", "test_machine2"); pipe.Commands.Add(cmd); var result = pipe.Invoke(); Label1.Text = results.ToString(); } } catch (Exception ex) { throw ex; Label1.Text = ex.ToString(); } } }

 

Asp.net使用powershell管理hyper-v

标签:

原文地址:http://www.cnblogs.com/wicrecend/p/4931397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!