通过调用powershell命令监控虚拟机,并实时返回数据显示到listview上

来源:互联网 发布:java 商城源码 编辑:程序博客网 时间:2024/06/05 19:36

示例代码: 这里需要使用多线程,因为在powershell监控数据时,同时程序在运行其它操作

Thread  bgroundThread =newThread(newThreadStart(print));   ///start a new thread

 bgroundThread.IsBackground = true;

 bgroundThread.Start();

 

string vmmModulePath =@"C:\'Program Files'\'Microsoft System Center 2012'\'Virtual Machine Manager'\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1";        

Runspace runspace = RunspaceFactory.CreateRunspace();

       runspace.Open();        

RunspaceInvoke scriptInvoker = newRunspaceInvoke(runspace);

  scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Force");   //C# run powershell cmdlet

    scriptInvoker.Invoke("Import-Module " + vmmModulePath);     

While(true)

            {

          scriptInvoker.Invoke("$vm=get-vm -name S-DC01");             

var _SDC01_Status = scriptInvoker.Invoke("$vm.status");

 

listView1.Item[0].SubItem[2].Text = _SDC01_Status[0].ToString();

Thread.Sleep(2000);

}

 

原创粉丝点击