I am currently writing a WPF application which allows a user to convert a
我目前正在编写一个WPF应用程序,允许用户转换a
.vmdk (VMware vm file)
.vmdk(VMware虚拟机文件)
to a
到一个
.vhdx (Hyper-V vm file).
.vhdx(hyper - v vm文件)。
I am using a cmdlet (ConvertTo-MvmcVirtualHardDisk) which resides in the MvmcCmdlet.psd1 module, which comes with
我正在使用一个cmdlet (ConvertTo-MvmcVirtualHardDisk),它驻留在MvmcCmdlet中。psd1模块
Microsoft Virtual Machine Converter 3.0.0
微软虚拟机转换器3.0.0
I am testing the functionality with hardcoded paths atm.
我正在使用硬编码路径atm对功能进行测试。
Here's my current code:
这是我目前的代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
using System.Windows;
namespace PowershellTestArea
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string output { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
new Thread(() =>
{
Runspace rs = RunspaceFactory.CreateRunspace();
rs.ThreadOptions = PSThreadOptions.UseCurrentThread;
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Import-Module", true).AddParameter("Name", @"C:\Program Files\Microsoft Virtual Machine Converter\MvmcCmdlet.psd1");
ps.Invoke();
Debug.WriteLine(ps.HadErrors);
Thread.Sleep(3000);
ps.AddCommand("ConvertTo-MvmcVirtualHardDisk")
.AddParameter("–SourceLiteralPath", @"'C:\VM\Windows 7 x64.vmdk'")
.AddParameter("-VhdFormat", "Vhdx");
Debug.WriteLine(ps.HadErrors);
ps.BeginInvoke();
IAsyncResult asyncResult = ps.BeginInvoke<PSObject, PSObject>(null, output);
while (!asyncResult.IsCompleted)
{
Debug.WriteLine("Running...");
}
Debug.WriteLine(ps.Streams.Error.ToString());
rs.Close();
}).Start();
}
}
}
The reason that i'm doing the Powershell code in a new Thread is, that the module is imported and can be used at runtime. I want to make sure that it is imported in the same session, so to speak. The process:
我在新线程中执行Powershell代码的原因是,模块是导入的,可以在运行时使用。我想确保它是在同一个会话中导入的。过程:
ps.AddCommand("ConvertTo-MvmcVirtualHardDisk")
.AddParameter("–SourceLiteralPath", @"'C:\VM\Windows 7 x64.vmdk'")
.AddParameter("-VhdFormat", "Vhdx");
Debug.WriteLine(ps.HadErrors);
ps.BeginInvoke();
finishes really fast (HadErrors returns false), and when run in powershell, it takes about 40 minutes to complete.
结束非常快(HadErrors返回false),在powershell中运行时,大约需要40分钟完成。
Can anyone tell me what to do, to track the progress, so the conversion can be done/finish?
谁能告诉我怎么做,跟踪进度,这样转换可以完成吗?
EDIT:
编辑:
Difference from this question is that my process does not finish correctly, and I'm wondering how to give the process time to finish.
与这个问题不同的是,我的流程没有正确完成,我想知道如何给流程时间来完成。
1 个解决方案
#1
0
Progress in Powershell is a stream, just like Error, Warning, Debug etc. All streams are available through the PSDataStreams class, exposed through the Powershell.Streams property.
Powershell中的进展是一个流,就像错误、警告、调试等。所有流都可以通过PSDataStreams类获得,通过Powershell公开。流属性。
You can listen for Progress events on the Progress stream by subscribing to the DataAdded
event, eg:
您可以订阅dataadd事件来侦听进度流上的进度事件,例如:
ps.Streams.Progress.DataAdded += (sender,args) => {
var records = (PSDataCollection<ProgressRecord>)sender;
var current=records[args.Index];
Console.WriteLine("VM conversion is {0} percent complete", current.PercentComplete);
};
You can listen to the rest of the streams in a similar way, eg the Errors stream is a collection of ErrorRecord objects. You can listen to information, Verbose, Warning messages in the same way, to construct a full log of what happened.
您可以以类似的方式侦听其他流,如错误流是错误记录对象的集合。您可以以同样的方式侦听信息、详细信息和警告消息,从而构建完整的事件日志。
Another option is to create a Transcript of a Powershell session. This saves all the commands and output of a session to a text file, from the moment you call Start-Transcript until you call Stop-Transcript, eg:
另一个选项是创建Powershell会话的副本。这将把会话的所有命令和输出保存到文本文件中,从调用Start-Transcript到调用Stop-Transcript,例如:
Start-Transcript -OutputDirectory 'c:\mylogs' -noclobber -IncludeInvocationHeader
ConvertTo-MvmcVirtualHardDisk ...
Stop-Transcript
or the equivalent code using C#.
或者使用c#的等效代码。
This is a quick&dirty way to capture everything for troubleshooting
这是一种快速而又肮脏的方法,可以捕获故障诊断所需的所有信息
#1
0
Progress in Powershell is a stream, just like Error, Warning, Debug etc. All streams are available through the PSDataStreams class, exposed through the Powershell.Streams property.
Powershell中的进展是一个流,就像错误、警告、调试等。所有流都可以通过PSDataStreams类获得,通过Powershell公开。流属性。
You can listen for Progress events on the Progress stream by subscribing to the DataAdded
event, eg:
您可以订阅dataadd事件来侦听进度流上的进度事件,例如:
ps.Streams.Progress.DataAdded += (sender,args) => {
var records = (PSDataCollection<ProgressRecord>)sender;
var current=records[args.Index];
Console.WriteLine("VM conversion is {0} percent complete", current.PercentComplete);
};
You can listen to the rest of the streams in a similar way, eg the Errors stream is a collection of ErrorRecord objects. You can listen to information, Verbose, Warning messages in the same way, to construct a full log of what happened.
您可以以类似的方式侦听其他流,如错误流是错误记录对象的集合。您可以以同样的方式侦听信息、详细信息和警告消息,从而构建完整的事件日志。
Another option is to create a Transcript of a Powershell session. This saves all the commands and output of a session to a text file, from the moment you call Start-Transcript until you call Stop-Transcript, eg:
另一个选项是创建Powershell会话的副本。这将把会话的所有命令和输出保存到文本文件中,从调用Start-Transcript到调用Stop-Transcript,例如:
Start-Transcript -OutputDirectory 'c:\mylogs' -noclobber -IncludeInvocationHeader
ConvertTo-MvmcVirtualHardDisk ...
Stop-Transcript
or the equivalent code using C#.
或者使用c#的等效代码。
This is a quick&dirty way to capture everything for troubleshooting
这是一种快速而又肮脏的方法,可以捕获故障诊断所需的所有信息