usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Management.Automation;usingSystem.Management.Automation.Runspaces;usingSystem.Collections.ObjectModel;usingSystem.Text; Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { } protected voidButton1_Click (Objectsender, EventArgs e) {runspaceconfiguration Rconfig=runspaceconfiguration.create (); Pssnapinexception Pwarn=Newpssnapinexception (); Runspace Runspace=Runspacefactory.createrunspace (); stringTest ="Import-module virtualmachinemanager\r\n"; Runspace=Runspacefactory.createrunspace (rconfig); Runspace. Open (); Pipeline Pipeline=Runspace. Createpipeline (); Pipeline.Commands.AddScript (test); Try { varResults =pipeline. Invoke (); using(Pipeline pipe =Runspace. Createpipeline ()) {//start-vm-name XXXXX
Command cmd =NewCommand ("START-VM"); Cmd. Parameters.Add ("Name","Test_machine2"); PIPE.COMMANDS.ADD (CMD); varresult =Pipe. Invoke (); Label1.Text=results. ToString (); } } Catch(Exception ex) {Throwex; Label1.Text=Ex. ToString (); } }}
ASP. NET uses PowerShell to manage Hyper-V