微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐