Skip to content

Commit e564c71

Browse files
committed
Merge branch 'dev'
2 parents b1c504a + d254c44 commit e564c71

21 files changed

+343
-472
lines changed

Rubrik/Private/Connect-TovCenter.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#
2+
Helper function to connect to a vCenter Server and allows self-signed certificates for vCenter connections
3+
#>
4+
function ConnectTovCenter($vCenter)
5+
{
6+
Write-Verbose -Message 'Importing required modules and snapins'
7+
$powercli = Get-PSSnapin -Name VMware.VimAutomation.Core -Registered
8+
try
9+
{
10+
switch ($powercli.Version.Major) {
11+
{
12+
$_ -ge 6
13+
}
14+
{
15+
Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop
16+
Write-Verbose -Message 'PowerCLI 6+ module imported'
17+
}
18+
5
19+
{
20+
Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction Stop
21+
Write-Warning -Message 'PowerCLI 5 snapin added; recommend upgrading your PowerCLI version'
22+
Write-Warning -Message 'Visit: http://www.vmware.com/go/powercli'
23+
}
24+
default
25+
{
26+
throw 'This script requires PowerCLI version 5 or later'
27+
}
28+
}
29+
}
30+
catch
31+
{
32+
throw $_
33+
}
34+
35+
Write-Verbose -Message 'Ignoring self-signed SSL certificates for vCenter Server (optional)'
36+
$null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$false -Scope User -Confirm:$false
37+
38+
Write-Verbose -Message 'Connecting to vCenter'
39+
try
40+
{
41+
$null = Connect-VIServer -Server $vCenter -ErrorAction Stop -Session ($global:DefaultVIServers | Where-Object -FilterScript {
42+
$_.name -eq $vCenter
43+
}).sessionId
44+
}
45+
catch
46+
{
47+
throw $_
48+
}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<#
2+
Helper function to determine if we are connected to a Rubrik cluster by validating the global $RubrikConnection array exists
3+
The array should contain four items:
4+
* Header (hashtable) - used for authenticating future RESTful API calls
5+
* Server (string) - Rubrik cluster IP
6+
* UserID (string) - GUID of the connected user
7+
* Token (string) - GUID of the security token
8+
#>
9+
function TestRubrikConnection()
10+
{
11+
Write-Verbose -Message 'Validating the Rubrik token exists'
12+
if (-not $global:RubrikConnection.token)
13+
{
14+
throw 'You are not connected to a Rubrik server. Use Connect-Rubrik.'
15+
}
16+
Write-Verbose -Message 'Found a Rubrik token for authentication'
17+
$script:Header = $global:RubrikConnection.header
18+
}
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<#
2+
Helper function to allow self-signed certificates for HTTPS connections
3+
This is required when using RESTful API calls over PowerShell
4+
#>
5+
function UnblockSelfSignedCerts()
6+
{
7+
Write-Verbose -Message 'Allowing self-signed certificates'
8+
Add-Type -TypeDefinition @"
9+
using System.Net;
10+
using System.Security.Cryptography.X509Certificates;
11+
public class TrustAllCertsPolicy : ICertificatePolicy {
12+
public bool CheckValidationResult(
13+
ServicePoint srvPoint, X509Certificate certificate,
14+
WebRequest request, int certificateProblem) {
15+
return true;
16+
}
17+
}
18+
"@
19+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
20+
}

Rubrik/Public/Connect-Rubrik.ps1

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,15 @@ function Connect-Rubrik
3939

4040
Process {
4141

42+
UnblockSelfSignedCerts
43+
4244
Write-Verbose -Message 'Validating that login details were passed into username/password or credentials'
4345
if ($Password -eq $null -and $Credential -eq $null)
4446
{
4547
Write-Warning -Message 'You did not submit a username, password, or credentials.'
4648
$Credential = Get-Credential -Message 'Please enter administrative credentials for your Rubrik cluster'
4749
}
4850

49-
Write-Verbose -Message 'Allowing self-signed certificates'
50-
Add-Type -TypeDefinition @"
51-
using System.Net;
52-
using System.Security.Cryptography.X509Certificates;
53-
public class TrustAllCertsPolicy : ICertificatePolicy {
54-
public bool CheckValidationResult(
55-
ServicePoint srvPoint, X509Certificate certificate,
56-
WebRequest request, int certificateProblem) {
57-
return true;
58-
}
59-
}
60-
"@
61-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
62-
6351
Write-Verbose -Message 'Build the URI'
6452
$uri = 'https://'+$Server+'/login'
6553

@@ -107,6 +95,7 @@ function Connect-Rubrik
10795
token = $token
10896
server = $Server
10997
header = $head
98+
time = (Get-Date)
11099
}
111100

112101
Write-Verbose -Message 'Adding connection details into the $global:RubrikConnections array'

Rubrik/Public/Get-RubrikMount.ps1

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ function Get-RubrikMount
1212
GitHub: chriswahl
1313
.LINK
1414
https://github.com/rubrikinc/PowerShell-Module
15+
.EXAMPLE
16+
Get-RubrikMount -VM Server1
17+
Will return all Live Mounts found for Server1
1518
#>
1619

1720
[CmdletBinding()]
@@ -22,35 +25,29 @@ function Get-RubrikMount
2225
[String]$VM,
2326
[Parameter(Mandatory = $false,Position = 1,HelpMessage = 'Rubrik FQDN or IP address')]
2427
[ValidateNotNullorEmpty()]
25-
[String]$Server = $global:RubrikServer
28+
[String]$Server = $global:RubrikConnection.server
2629
)
2730

2831
Process {
2932

30-
# Validate the Rubrik token exists
31-
if (-not $global:RubrikToken)
32-
{
33-
throw 'You are not connected to a Rubrik server. Use Connect-Rubrik.'
34-
}
33+
TestRubrikConnection
3534

36-
# Query Rubrik for SLA Domain Information
37-
$uri = 'https://'+$global:RubrikServer+':443/mount'
35+
Write-Verbose -Message 'Query Rubrik for any active live mounts'
36+
$uri = 'https://'+$Server+'/mount'
3837

39-
# Submit the request
4038
try
4139
{
42-
$r = Invoke-WebRequest -Uri $uri -Headers $global:RubrikHead -Method Get
40+
$r = Invoke-WebRequest -Uri $uri -Headers $Header -Method Get
4341
$response = ConvertFrom-Json -InputObject $r.Content
4442
[array]$mount = $response | Where-Object -FilterScript {
45-
$_.snapshot.virtualMachineName -like $VM
43+
$_.sourcevirtualMachineName -like $VM
4644
}
4745
if (!$mount)
4846
{
4947
Write-Verbose -Message "No mounts found for $VM"
5048
}
5149
else
5250
{
53-
# Send mount details to $result and console
5451
Write-Verbose -Message "Found $($mount.count) mounts for $VM"
5552
$result = $mount | Select-Object -Property @{
5653
N = 'MountName'
@@ -78,13 +75,12 @@ function Get-RubrikMount
7875
$_.id
7976
}
8077
}
81-
$result
78+
return $result
8279
}
8380
}
8481
catch
8582
{
86-
$ErrorMessage = $_.Exception.Message
87-
throw "Error connecting to Rubrik server: $ErrorMessage"
83+
throw $_
8884
}
8985

9086

Rubrik/Public/Get-RubrikSLA.ps1

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Requires -Version 3
22
function Get-RubrikSLA
33
{
4-
<#
4+
<#
55
.SYNOPSIS
66
Connects to Rubrik and retrieves details on SLA Domain(s)
77
.DESCRIPTION
@@ -13,6 +13,12 @@ function Get-RubrikSLA
1313
GitHub: chriswahl
1414
.LINK
1515
https://github.com/rubrikinc/PowerShell-Module
16+
.EXAMPLE
17+
Get-RubrikSLA
18+
Will return all known SLA Domains
19+
.EXAMPLE
20+
Get-RubrikSLA -SLA 'Gold'
21+
Will return details on the SLA Domain named Gold
1622
#>
1723

1824
[CmdletBinding()]
@@ -22,55 +28,35 @@ function Get-RubrikSLA
2228
[String]$SLA,
2329
[Parameter(Mandatory = $false,Position = 1,HelpMessage = 'Rubrik FQDN or IP address')]
2430
[ValidateNotNullorEmpty()]
25-
[String]$Server = $global:RubrikServer
31+
[String]$Server = $global:RubrikConnection.server
2632
)
2733

2834
Process {
2935

30-
# Allow untrusted SSL certs
31-
Add-Type -TypeDefinition @"
32-
using System.Net;
33-
using System.Security.Cryptography.X509Certificates;
34-
public class TrustAllCertsPolicy : ICertificatePolicy {
35-
public bool CheckValidationResult(
36-
ServicePoint srvPoint, X509Certificate certificate,
37-
WebRequest request, int certificateProblem) {
38-
return true;
39-
}
40-
}
41-
"@
42-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
43-
44-
# Validate the Rubrik token exists
45-
if (-not $global:RubrikToken)
46-
{
47-
throw 'You are not connected to a Rubrik server. Use Connect-Rubrik.'
48-
}
36+
TestRubrikConnection
4937

50-
# Build the URI
51-
$uri = 'https://'+$server+':443/slaDomain'
38+
Write-Verbose -Message 'Retrieving SLA Domains from Rubrik'
39+
$uri = 'https://'+$Server+'/slaDomain'
5240

53-
# Submit the request
5441
try
5542
{
56-
$r = Invoke-WebRequest -Uri $uri -Headers $global:RubrikHead -Method Get
43+
$result = ConvertFrom-Json -InputObject (Invoke-WebRequest -Uri $uri -Headers $Header -Method Get).Content
5744
}
5845
catch
5946
{
6047
throw $_
6148
}
6249

63-
# Report the results
64-
$result = ConvertFrom-Json -InputObject $r.Content
65-
if ($sla)
50+
Write-Verbose -Message 'Returning the SLA Domain results'
51+
if ($SLA)
6652
{
67-
$result | Where-Object -FilterScript {
68-
$_.name -match $sla
53+
return $result | Where-Object -FilterScript {
54+
$_.name -match $SLA
6955
}
7056
}
7157
else
7258
{
73-
$result
59+
return $result
7460
}
7561

7662
} # End of process

Rubrik/Public/Get-RubrikSnapshot.ps1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function Get-RubrikSnapshot
2+
{
3+
<#
4+
.SYNOPSIS
5+
Retrieves all of the snapshots (backups) for a given virtual machine
6+
.DESCRIPTION
7+
The Get-RubrikSnapshot cmdlet is used to query the Rubrik cluster for all known snapshots (backups) for a protected virtual machine
8+
.NOTES
9+
Written by Chris Wahl for community usage
10+
Twitter: @ChrisWahl
11+
GitHub: chriswahl
12+
.LINK
13+
https://github.com/rubrikinc/PowerShell-Module
14+
.EXAMPLE
15+
Get-RubrikSnapshot -VM 'Server1'
16+
This will return an array of details for each snapshot (backup) for Server1
17+
#>
18+
19+
[CmdletBinding()]
20+
Param(
21+
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'Required variable')]
22+
[ValidateNotNullorEmpty()]
23+
[String]$VM,
24+
[Parameter(Mandatory = $false,Position = 1,HelpMessage = 'Rubrik FQDN or IP address')]
25+
[ValidateNotNullorEmpty()]
26+
[String]$Server = $global:RubrikConnection.server
27+
)
28+
29+
Process {
30+
31+
TestRubrikConnection
32+
33+
Write-Verbose -Message 'Query Rubrik for the list of protected VM details'
34+
$vmid = (Get-RubrikVM -VM $VM).id
35+
36+
Write-Verbose -Message 'Query Rubrik for the protected VM snapshot list'
37+
$uri = 'https://'+$Server+'/snapshot?vm='+$vmid
38+
try
39+
{
40+
$r = Invoke-WebRequest -Uri $uri -Headers $Header -Method Get
41+
$result = (ConvertFrom-Json -InputObject $r.Content)
42+
if (!$result)
43+
{
44+
throw 'No snapshots found for VM.'
45+
}
46+
else {return $result}
47+
}
48+
catch
49+
{
50+
throw $_
51+
}
52+
53+
} # End of process
54+
} # End of function

0 commit comments

Comments
 (0)