/
New-RubrikMount.ps1
133 lines (113 loc) · 4.14 KB
/
New-RubrikMount.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#Requires -Version 3
function New-RubrikMount
{
<#
.SYNOPSIS
Create a new Live Mount from a protected VM
.DESCRIPTION
The New-RubrikMount cmdlet is used to create a Live Mount (clone) of a protected VM and run it in an existing vSphere environment.
.NOTES
Written by Chris Wahl for community usage
Twitter: @ChrisWahl
GitHub: chriswahl
.LINK
https://github.com/rubrikinc/PowerShell-Module
.EXAMPLE
New-RubrikMount -VM 'Server1' -Date '05/04/2015 08:00'
This will create a new Live Mount for the virtual machine named Server1 based on the first snapshot that is equal to or older than 08:00 AM on May 4th, 2015
.EXAMPLE
New-RubrikMount -VM 'Server1'
This will create a new Live Mount for the virtual machine named Server1 based on the first snapshot that is equal to or older the current time (now)
#>
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact = 'High')]
Param(
# Name of the virtual machine
[Parameter(Mandatory = $true,Position = 0,ValueFromPipelineByPropertyName = $true)]
[Alias('Name')]
[ValidateNotNullorEmpty()]
[String]$VM,
# An optional name for the Live Mount
# By default, will use the original VM name plus a date and instance number
[Parameter(Position = 1)]
[String]$MountName,
# Date of the snapshot to use for the Live Mount
# Format should match MM/DD/YY HH:MM
# If no value is specified, will retrieve the last known shapshot
[Parameter(Position = 2,ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullorEmpty()]
[String]$Date,
# ID of the host for the Live Mount to use
# Defaults to the hostId where the running virtual machine lives
[String]$HostID,
# Select the power state of the Live Mount
# Defaults to $false (powered off)
[Switch]$PowerOn,
# Rubrik server IP or FQDN
[String]$Server = $global:RubrikConnection.server,
# API version
[String]$api = $global:RubrikConnection.api
)
Process {
TestRubrikConnection
Write-Verbose -Message 'Determining which version of the API to use'
$resources = GetRubrikAPIData -endpoint ('VMwareVMMountPost')
if (!$Date)
{
Write-Verbose -Message 'No date entered. Taking current time.'
$Date = Get-Date
}
Write-Verbose -Message 'Query Rubrik for the list of protected VM details'
if (!$HostID)
{
$HostID = (Get-RubrikVM -VM $VM).hostId
}
Write-Verbose -Message 'Query Rubrik for the protected VM snapshot list'
$snapshots = Get-RubrikSnapshot -VM $VM
Write-Verbose -Message 'Comparing backup dates to user date'
$Date = ConvertFromLocalDate -Date $Date
Write-Verbose -Message 'Finding snapshots that match the date value'
foreach ($_ in $snapshots)
{
if (([datetime]$_.date) -le ($Date) -eq $true)
{
$vmsnapid = $_.id
Write-Verbose -Message "Found matching snapshot with ID $vmsnapid"
break
}
}
Write-Verbose -Message 'Building the URI'
$uri = 'https://'+$Server+$resources.$api.URI
# Create the body
$body = @{
$resources.$api.body.snapshotId = $vmsnapid
$resources.$api.body.hostId = $HostID
$resources.$api.body.disableNetwork = $true
$resources.$api.body.removeNetworkDevices = $false
$resources.$api.body.powerOn = [boolean]::Parse($PowerOn)
}
if ($MountName)
{
$body.Add($resources.$api.body.vmName,$MountName)
}
# Set the method
$method = $resources.$api.Method
try
{
if ($PSCmdlet.ShouldProcess($VM,'Creating a new Live Mount'))
{
$r = Invoke-WebRequest -Uri $uri -Headers $Header -Method $method -Body (ConvertTo-Json -InputObject $body)
if ($r.StatusCode -ne $resources.$api.SuccessCode)
{
Write-Warning -Message 'Did not receive successful status code from Rubrik for Live Mount request'
throw $_
}
$response = ConvertFrom-Json -InputObject $r.Content
return $response
}
}
catch
{
throw $_
}
} # End of process
} # End of function