Skip to content

Instantly share code, notes, and snippets.

@jonathanelbailey
Last active October 7, 2015 16:06
Show Gist options
  • Save jonathanelbailey/1571c9e3dc3e07f4cbe2 to your computer and use it in GitHub Desktop.
Save jonathanelbailey/1571c9e3dc3e07f4cbe2 to your computer and use it in GitHub Desktop.
A few functions that allow you to manage export of VMs for archiving.
Function Remove-Checkpointsforexport{
Param( [ValidateSet('All','RDP','SQL','DC')]
$VMScope )
BEGIN{}
PROCESS{
switch ($VMScope){
ALL { $VMs = Get-VM }
RDP { $VMs = Get-VM | Where-Object name -Match 'RDP' }
SQL { $VMs = Get-VM | Where-Object name -Match 'SQL' }
DC { $VMs = Get-VM | Where-Object name -Match 'DC' }
}
}
END{
$VMs | Get-VMSnapshot | Remove-VMSnapshot
}
}
Function New-VmExportArchive{
Param( $Path,
$Archivepath )
BEGIN{}
PROCESS{
if ((test-path $path) -ne $true){
write-host -foregroundcolor yellow "$path does not exist."
}
else{
7z a $archivepath $path
}
}
END{
7z t * $archivepath
}
}
Function Get-VMExports{
Param( [ValidateSet('All','RDP','SQL','DC')]
$VMScope,
[switch]$RemoveCheckpoint,
$Path )
BEGIN{
switch($VMScope){
ALL {$VMs = Get-VM}
RDP { $VMs = Get-VM | Where-Object name -Match 'RDP' }
SQL { $VMs = get-vm | where-object name -Match 'SQL' }
DC { $VMs = get-vm | Where-Object name -Match 'DC' }
}
if ($removecheckpoint -eq $true){
remove-checkpointsforExport -vmscope $vmscope
}
$result = @()
$i = 0
}
PROCESS{
foreach ($v in $VMS){
$vmexport = Export-VM -ComputerName $v -Path $Path
$result += $vmexport
$i++
Write-Progress -Activity "Exporting $($v.name)..." ` -Status "$i of $($vms.count) Completed" `
-PercentComplete "(($i / $($vms.count)) * 100)"
}
}
END{
new-vmexportarchive -path $path -archivename "$((get-date).datetime)Vm-Backup.7z"
write-output $result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment