#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