Hyper-V 虚拟机导出的四种方式,保存Save-VM,停止Stop-VM,检查点Checkpoint-VM,在线-不做前三项操作直接Export-VM

#region Export der VM
if ($save -match "true")
{
    Write-Host -ForegroundColor Green (Get-Date) "VM is saved"
    Save-VM -Name $VM -verbose
    Write-Host -ForegroundColor Green (Get-Date) "Exporting the VM"
    $DestinationPathPresent = Test-Path $exportpath\$VM
    if 
        ($DestinationPathPresent -eq $False) { Export-VM -Name $VM -Path $exportpath -verbose }
    else 
        { Remove-Item -Recurse -Force $exportpath\$VM -verbose; Export-VM -Name $VM -Path $exportpath -verbose }
    Write-Host -ForegroundColor Green (Get-Date) "Export completes,VM is turned back on"
    Start-VM -Name $VM -verbose
    if ($statusmail -match "true")
        { Mail; exit }
    else
        { exit }
}

if ($shutdown -match "true")
{
    $ShutdownStatus = get-vm -Name $VM | Get-VMIntegrationService | where { $_.Name -EQ "Shutdown" -or $_.Name -EQ "shutdown" }
    if ($ShutdownStatus.Enabled -eq "True")
    {
        Write-Host -ForegroundColor Green (Get-Date) "Export seems possible,VM shuts down"
        Stop-VM -Name $VM -Force -verbose
        Write-Host -ForegroundColor Green (Get-Date) "Exporting the VM"
        $DestinationPathPresent = Test-Path $exportpath\$VM
        if 
            ($DestinationPathPresent -eq $False) { Export-VM -Name $VM -Path $exportpath -verbose }
        else 
            { Remove-Item -Recurse -Force $exportpath\$VM -verbose; Export-VM -Name $VM -Path $exportpath -verbose }
        Write-Host -ForegroundColor Green (Get-Date) "VM is turned back on"
        Start-VM -Name $VM -verbose
        if ($statusmail -match "true")
            { Mail; exit }
        else
            { exit }
    }
    else
    {
        Write-Host -ForegroundColor Red (Get-Date) "Export does not seem to be possible,operation is canceled!"
        exit
    }
}

if ($ProductionCheckpoint -match "true")
{
    $SnapshotName = "ExportScriptCheckpoint"
    Write-Host -ForegroundColor Green (Get-Date) "Checkpoint is Create"
    $DestinationPathPresent = Test-Path $exportpath\$VM
    if 
        ($DestinationPathPresent -eq $False) 
        { Checkpoint-VM -Name $VM -SnapshotName $SnapshotName -verbose
          Export-VMSnapshot -VMName $VM -Name $SnapshotName -Path $exportpath -verbose
          Remove-VMSnapshot -VMName $VM -Name $SnapshotName -verbose
        }
    else 
        { Remove-Item -Recurse -Force $exportpath\$VM -verbose
          Checkpoint-VM -Name $VM -SnapshotName $SnapshotName -verbose
          Export-VMSnapshot -VMName $VM -Name $SnapshotName -Path $exportpath -verbose
          Remove-VMSnapshot -VMName $VM -Name $SnapshotName -verbose
        }
    Write-Host -ForegroundColor Green (Get-Date) "Export completed"
    if ($statusmail -match "true")
        { Mail; exit }
    else
        { exit }
}
else
{
    Write-Host -ForegroundColor Green (Get-Date) "VM is exported online"
    Write-Host -ForegroundColor Green (Get-Date) "Exporting the VM"
    $DestinationPathPresent = Test-Path $exportpath\$VM -verbose
    if 
        ($DestinationPathPresent -eq $False) { Export-VM -Name $VM -Path $exportpath -verbose }
    else 
        { Remove-Item -Recurse -Force $exportpath\$VM -verbose; Export-VM -Name $VM -Path $exportpath -verbose }
    Write-Host -ForegroundColor Green (Get-Date) "Export of VM $VM completed"
    if ($statusmail -match "true")
        { Mail; exit }
    else
        { exit }
}
#endregion

相关文章

Hyper-V上运行的Linux虚拟机验证是否安装了集成服务 ps aux|...
目录虚拟化介绍虚拟化厂商介绍微软虚拟化发展历程Hyper-v 3....
最近有win10系统用户在开启Hyper-V功能的时候发现无法启动...
Win10系统中的Hyper-V虚拟机组件可以让用户很方便的创建虚拟...
在Win10中使用Hyper-V来创建虚拟机很方便,下面小编就把Hype...
大家都知道Win10家庭版、中文版、普通版和专业版相比是缺少很...