-
Notifications
You must be signed in to change notification settings - Fork 1
/
VM-Restore-Backup.ps1
52 lines (41 loc) · 1.23 KB
/
VM-Restore-Backup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#requires -version 3.0
#requires -module Hyper-V
<#
.SYNOPSIS
Returns details about the most recent backup of a specific VM.
.PARAMETER VMname
The name of the virtual machine.
.PARAMETER BaseFolder
(optional) Where the VM backups are stored.
.EXAMPLE
PS C:\> Get-Latest-VM-Backup('STAGING-Web02')
#>
Function Get-Latest-VM-Backup()
{
Param([string]$VMname,
[string]$BaseFolder = 'E:\VMBackups')
Get-ChildItem ($BaseFolder + '\*\' + $VMname) |
select name,
FullName,
*time,
@{Name = "DateString";
Expression = {
[regex]::match($_.FullName, '(Monthly|Weekly)_(\d{4}_\d{2}_\d{2}_\d{4})').Groups[2] }} |
Sort-Object DateString -Descending |
Select-Object -first 1
}
<#
.SYNOPSIS
Import a virtual machine backup that was generated using the VM-Backup script.
.PARAMETER VMname
The name of the virtual machine to be imported.
.EXAMPLE
PS C:\> Import-Latest-VM-Backup('STAGING-Web02')
#>
Function Import-Latest-VM-Backup()
{
Param([string]$VMname)
$baseFolder = Get-Latest-VM-Backup($VMname) | select FullName
$xmlConfig = Get-ChildItem -Path ($baseFolder.FullName + '\Virtual Machines') -Filter *.xml
Import-VM -Path ($xmlConfig.FullName) -Copy -GenerateNewId
}