获取 Export-VM 的进度输出
Get an progress output for Export-VM
我最近创建了一个脚本,它能够从 "template" 创建选定数量的 Hyper-V VM,方法是导出一个 VM 并使用不同的 ID 和名称导入它。
一切顺利,但导出需要一些时间。有没有办法从 export-vm 获得进度输出?如果我查看 hyper-v 命令中心,我看到我的 VM 在我启动脚本后被导出,并且它也有一个进度值,所以不知何故似乎有一种方法可以获得当前进度...
Import-VM 也有进度输出也很好,但这并不重要。
如果您有兴趣,这是我当前的脚本。我知道硬编码路径和其他方面并不是很好,所以请不要告诉我任何有关代码风格或类似内容的信息。首先是工作脚本,然后是漂亮的代码。
param(
[int]$Anzahl = 0,
[string]$BasisVM = 'Schulung',
[string]$ExportDir = 'C:\VMConf\Export\',
[string]$ExportConf = 'Schulung\Virtual Machines[=10=]D444AF2-3E63-4ACF-867E-34440AA99C42.xml',
[string]$VMDir = 'C:\VMs\',
[string]$VMNamePrefix = 'Schulung'
)
if ($Anzahl -eq 0) {
$Anzahl = Read-Host "Bitte Anzahl der benötigten VMs eingeben"
}
"`nStarte Export der Vorlage..."
if (-Not(Test-Path $ExportDir)) {
New-Item -ItemType directory -Path $ExportDir | out-null
}
Export-VM -Name $BasisVM -Path $ExportDir
"Abgeschlossen.`n"
if (Test-Path $VMDir) {
$err = $null
$i = 1
while (-not($err)) {
if ($i -gt 1) {
"Alte VM Nummer $($i - 1) wurde gelöscht."
}
Remove-VM -Name $($VMNamePrefix + ("{0:D2}" -f $i)) -Force -ErrorVariable err -ErrorAction SilentlyContinue
$i++
}
Remove-Item -r $VMDir | out-null
New-Item -ItemType directory -Path $VMDir | out-null
}
"`n$Anzahl VMs werden erstellt..."
for ($i=1; $i -le $Anzahl; $i++) {
"`tErstelle VM Nummer $i..."
$name = ($VMNamePrefix + ("{0:D2}" -f $i))
$path = ($VMDir + $name)
$VM = Import-VM -Path $($ExportDir + $ExportConf) -VhdDestinationPath $($path + "/Virtual Hard Discs") -VirtualMachinePath $path -Copy -GenerateNewId
Rename-VM -VM $VM -NewName $name
"`t$VM Nummer $i wurde erzeugt."
}
"Abgeschlossen.`n"
"Aufräumen..."
if (Test-Path $ExportDir) {
Remove-Item -r $ExportDir | out-null
}
"Abgeschlossen."
您可以将导出(甚至导入)作为后台作业启动,然后记录作业的进度。示例代码:
$ExportJob = Export-VM -Name $BasisVM -Path $ExportDir -AsJob;
while( $ExportJob.State -eq "Running" -or $ExportJob.State -eq "NotStarted")
{
Write-Output ("[Export] " + $($ExportJob.Progress.PercentComplete) + "% complete");
sleep(5);
}
if($ExportJob.State -ne "Completed")
{
Write-Error ("Export Job did not complete: " +$ExportJob.State);
throw $ExportJob.Error;
}
我最近创建了一个脚本,它能够从 "template" 创建选定数量的 Hyper-V VM,方法是导出一个 VM 并使用不同的 ID 和名称导入它。
一切顺利,但导出需要一些时间。有没有办法从 export-vm 获得进度输出?如果我查看 hyper-v 命令中心,我看到我的 VM 在我启动脚本后被导出,并且它也有一个进度值,所以不知何故似乎有一种方法可以获得当前进度...
Import-VM 也有进度输出也很好,但这并不重要。
如果您有兴趣,这是我当前的脚本。我知道硬编码路径和其他方面并不是很好,所以请不要告诉我任何有关代码风格或类似内容的信息。首先是工作脚本,然后是漂亮的代码。
param(
[int]$Anzahl = 0,
[string]$BasisVM = 'Schulung',
[string]$ExportDir = 'C:\VMConf\Export\',
[string]$ExportConf = 'Schulung\Virtual Machines[=10=]D444AF2-3E63-4ACF-867E-34440AA99C42.xml',
[string]$VMDir = 'C:\VMs\',
[string]$VMNamePrefix = 'Schulung'
)
if ($Anzahl -eq 0) {
$Anzahl = Read-Host "Bitte Anzahl der benötigten VMs eingeben"
}
"`nStarte Export der Vorlage..."
if (-Not(Test-Path $ExportDir)) {
New-Item -ItemType directory -Path $ExportDir | out-null
}
Export-VM -Name $BasisVM -Path $ExportDir
"Abgeschlossen.`n"
if (Test-Path $VMDir) {
$err = $null
$i = 1
while (-not($err)) {
if ($i -gt 1) {
"Alte VM Nummer $($i - 1) wurde gelöscht."
}
Remove-VM -Name $($VMNamePrefix + ("{0:D2}" -f $i)) -Force -ErrorVariable err -ErrorAction SilentlyContinue
$i++
}
Remove-Item -r $VMDir | out-null
New-Item -ItemType directory -Path $VMDir | out-null
}
"`n$Anzahl VMs werden erstellt..."
for ($i=1; $i -le $Anzahl; $i++) {
"`tErstelle VM Nummer $i..."
$name = ($VMNamePrefix + ("{0:D2}" -f $i))
$path = ($VMDir + $name)
$VM = Import-VM -Path $($ExportDir + $ExportConf) -VhdDestinationPath $($path + "/Virtual Hard Discs") -VirtualMachinePath $path -Copy -GenerateNewId
Rename-VM -VM $VM -NewName $name
"`t$VM Nummer $i wurde erzeugt."
}
"Abgeschlossen.`n"
"Aufräumen..."
if (Test-Path $ExportDir) {
Remove-Item -r $ExportDir | out-null
}
"Abgeschlossen."
您可以将导出(甚至导入)作为后台作业启动,然后记录作业的进度。示例代码:
$ExportJob = Export-VM -Name $BasisVM -Path $ExportDir -AsJob;
while( $ExportJob.State -eq "Running" -or $ExportJob.State -eq "NotStarted")
{
Write-Output ("[Export] " + $($ExportJob.Progress.PercentComplete) + "% complete");
sleep(5);
}
if($ExportJob.State -ne "Completed")
{
Write-Error ("Export Job did not complete: " +$ExportJob.State);
throw $ExportJob.Error;
}