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

此脚本使用PowerShell实现虚拟机(VM)的在线和离线导出功能,根据不同的条件选择停机、创建检查点或直接导出运行中的VM,并在完成后发送状态邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值