Skip to content

Commit 0f934e3

Browse files
committed
Add samples from Jase's repo
1 parent 6f76d0a commit 0f934e3

4 files changed

+936
-0
lines changed

Copy-RDMs-MountToVM.ps1

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<#==========================================================================
2+
Script Name: Copy-RDMs-MountToVM.ps1
3+
Created on: 12/08/2021
4+
Created by: Jase McCarty
5+
Github: http://www.github.com/jasemccarty
6+
Twitter: @jasemccarty
7+
===========================================================================
8+
.DESCRIPTION
9+
Copy RDMs and mount them to a new VM
10+
Powershell Core supported - Requires PowerCLI, PureStorage.FlashArray.VMware, & PureStoragePowerShellSDK (v1) modules.
11+
.SYNTAX
12+
Copy-RDMs-MountToVM.ps1 -vCenter <VCENTER> -FlashArray <FlashArray> -VM <VM> -SourceVolumes <SourceRDMs>
13+
.EXAMPLE
14+
Copy-RDMs-MountToVM.ps1 -vCenter vc02.fsa.lab -FlashArray sn1-m70-f06-33.puretec.purestorage.com -VM SQLVM -SourceVolumes 'RDMD','RDME','RDMF'
15+
#>
16+
17+
# Set our Parameters
18+
[CmdletBinding()]Param(
19+
[Parameter(Mandatory= $False)][string]$Vcenter,
20+
[Parameter(Mandatory = $False)][String]$FlashArray,
21+
[Parameter(Mandatory = $False)][String]$VM,
22+
[Parameter(Mandatory = $False)][Array]$SourceVolumes
23+
)
24+
25+
# Variables Section
26+
# Target Variables - Replace any of these as defaults if parameters are not passed
27+
if (-Not $Vcenter) { $Vcenter = 'vc02.fsa.lab' } # vCenter
28+
if (-Not $FlashArray) { $FlashArray = 'sn1-m70-f06-33.puretec.purestorage.com' } # FlashArray
29+
if (-Not $VM) { $VM = 'SQLVM' } # VM
30+
if (-Not $SourceVolumes) { $sourcevolumes = @('RDMD','RDME','RDMF') } # Source Volumes
31+
32+
###########################################################
33+
# It should not be necessary to make any changes below #
34+
###########################################################
35+
###########################################################
36+
# Check for proper PowerShell modules installation #
37+
###########################################################
38+
39+
# Get the PowerCLI Version
40+
$PowerCLIVersion = Get-Module -Name VMware.PowerCLI -ListAvailable | Select-Object -Property Version
41+
42+
# If the PowerCLI Version is not v10 or higher, recommend that the user install PowerCLI 12 or higher
43+
If ($PowerCLIVersion.Version.Major -ge "12") {
44+
Write-Host "PowerCLI version 12 or higher present, " -NoNewLine
45+
Write-Host "proceeding" -ForegroundColor Green
46+
} else {
47+
Write-Host "PowerCLI version could not be determined or is less than version 12" -Foregroundcolor Red
48+
Write-Host "Please install PowerCLI 12 or higher and rerun this script" -Foregroundcolor Yellow
49+
Write-Host " "
50+
exit
51+
}
52+
# Check for Pure Storage PowerShell SDK (v1) installation, required to facilitate some FlashArray tasks
53+
If (-Not (Get-Module -ListAvailable -Name "PureStoragePowerShellSDK")) {
54+
Write-Host "Please install the Pure Storage PowerShell SDK (v1) Module and rerun this script to proceed" -ForegroundColor Yellow
55+
Write-Host "It can be installed using " -NoNewLine
56+
Write-Host "'Install-Module -Name PureStoragePowerShellSDK'" -ForegroundColor Green
57+
Write-Host
58+
exit
59+
}
60+
# Check for Pure Storage FlashArray Module for VMware installation, required to facilitate some VMware-based FlashArray tasks
61+
If (-Not (Get-Module -ListAvailable -Name "PureStorage.FlashArray.VMware")) {
62+
Write-Host "Please install the Pure Storage FlashArray Module for VMware and rerun this script to proceed" -ForegroundColor Yellow
63+
Write-Host "It can be installed using " -NoNewLine
64+
Write-Host "'Install-Module -Name PureStorage.FlashArray.VMware'" -ForegroundColor Green
65+
Write-Host
66+
exit
67+
}
68+
69+
###################################################################################################
70+
# Check to see if we're connected to the vCenter, if not, prompt for Credentials to connect to it.#
71+
###################################################################################################
72+
73+
# Check to see if a current vCenter Server session is in place
74+
If ($Global:DefaultVIServer.Name -eq $Vcenter) {
75+
Write-Host "Connected to " -NoNewline
76+
Write-Host $Global:DefaultVIServer -ForegroundColor Green
77+
} else {
78+
# If not connected to vCenter Server make a connection
79+
Write-Host "Not connected to vCenter Server" -ForegroundColor Red
80+
# Prompt for credentials using the native PowerShell Get-Credential cmdlet
81+
$VICredentials = Get-Credential -Message "Enter credentials for vCenter Server: $($Vcenter)"
82+
try {
83+
# Attempt to connect to the vCenter Server
84+
Connect-VIServer -Server $Vcenter -Credential $VICredentials -ErrorAction Stop | Out-Null
85+
Write-Host "Connected to $Vcenter" -ForegroundColor Green
86+
# Note that we connected to vCenter so we can disconnect upon termination
87+
$ConnectVc = $True
88+
}
89+
catch {
90+
# If we could not connect to vCenter report that and exit the script
91+
Write-Host "Failed to connect to $Vcenter" -BackgroundColor Red
92+
Write-Host $Error
93+
Write-Host "Terminating the script " -BackgroundColor Red
94+
# Note that we did not connect to vCenter Server
95+
$ConnectVc = $False
96+
exit
97+
return
98+
}
99+
}
100+
101+
#############################################################################################################
102+
# Check to see if we're connected to the Target FlashArray, if not, prompt for Credentials to connect to it.#
103+
#############################################################################################################
104+
105+
If ($DefaultFlashArray.EndPoint -eq $FlashArray) {
106+
# Connect to specified FlashArray
107+
$TargetFlashArray = $DefaultFlashArray
108+
} else {
109+
try {
110+
$FaCredentials = Get-Credential -Message "Please enter the FlashArray Credentials for $($FlashArray)"
111+
$TargetFlashArray = New-PfaConnection -EndPoint $FlashArray -Credentials $FaCredentials -ErrorAction Stop -IgnoreCertificateError -DefaultArray
112+
$ConnectFA = $True
113+
}
114+
catch {
115+
write-host "Failed to connect to the FlashArray" -BackgroundColor Red
116+
write-host $Error
117+
write-host "Terminating Script" -BackgroundColor Red
118+
$ConnectFA = $False
119+
exit
120+
return
121+
}
122+
}
123+
# Bulk of operations start here now that the PowerShell Modules have been confirmed loaded & we've connected to vCenter & FlashArray
124+
125+
# Informational
126+
Write-Host "Performing prep work"
127+
Write-Host "*********************************************************"
128+
Write-Host
129+
130+
# Denote & Get the VM object and put it in $WorkingVM
131+
Write-Host "Retrieving VM: " -NoNewline
132+
Write-Host $VM -ForegroundColor Green
133+
$WorkingVM = Get-VM -Name $VM
134+
135+
# Denote & Get the current datastore the VM is on so the RDM pointers will be placed in the same location
136+
Write-Host "Getting the current datastore for $($VM) to ensure the RDM pointers are place on the same datastore"
137+
$Datastore = $WorkingVM | Get-Datastore
138+
139+
# Denote & Get a random number between 00000-99999 to ensure we create unique volumes
140+
Write-Host "Generating a random number to ensure unique volume names"
141+
$Random = Get-Random -Maximum 99999
142+
143+
# Informational
144+
Write-Host "*********************************************************"
145+
Write-Host
146+
147+
# Loop through each of the Source RDM volumes and create a new RDM that corresponds with each
148+
Foreach ($SourceVolume in $SourceVolumes) {
149+
# Denote the current RDM we're working with
150+
Write-Host "Retrieving Volume $($SourceVolume) " -NoNewLine
151+
152+
# Retrieve the current RDM Volume Details
153+
$CurrentVolume = New-PfaRestOperation -ResourceType volume/$($SourceVolume) -RestOperationType Get -Flasharray $TargetFlashArray -SkipCertificateCheck
154+
155+
# Create the new volume based on the naming RDM-VMNAME-SOURCERDMNAME-RANDOM
156+
$NewVolumeName = "RDM-$($WorkingVM)-$($CurrentVolume.Name)-$($Random)"
157+
158+
# Retrieve the current RDM volume size so we can overwrite appropriately (Source & New volumes must be the same size)
159+
$CurrentVolumeSize = $CurrentVolume.size/1GB
160+
161+
# Denote the Current RDM volume we're working with
162+
Write-Host "Current Volume: $($CurrentVolume.name)" -ForegroundColor Yellow
163+
Write-Host
164+
165+
# Denote that we're creating a new EMPTY RDM and attaching it to the VM
166+
Write-Host "Creating RDM Volume $($NewVolumeName) that is the same capacity ($($CurrentVolumeSize) GB) as $($CurrentVolume.Name) and attaching it to $($WorkingVM)"
167+
168+
# Create the new EMPTY RDM that is the same size as the source RDM
169+
New-PfaRdm -Flasharray $TargetFlashArray -VM $WorkingVM -Datastore $Datastore -SizeinGB $CurrentVolumeSize -Volname $NewVolumeName | Out-Null
170+
171+
# Denote we're overwriting the newly created EMPTY RDM with the contents of the Source RDM
172+
Write-Host "Overwriting $($NewVolumeName) with the contents of $($CurrentVolume.Name)" -ForegroundColor Yellow
173+
174+
# Overwrite the newly created EMPTY RDM with the contents of the source RDM
175+
New-PfaRestOperation -ResourceType volume/$($NewVolumeName) -RestOperationType POST -Flasharray $TargetFlashArray -SkipCertificateCheck -jsonBody "{`"overwrite`":true,`"source`":`"$($CurrentVolume.Name)`"}" | Out-Null
176+
177+
# Informational
178+
Write-Host
179+
}
180+
181+
# Informational
182+
Write-Host "*********************************************************"
183+
Write-Host "Complete" -ForegroundColor Green
184+
185+
# Disconnect From FlashArray if we had to log into it
186+
If ($ConnectFA -eq $true) {
187+
Disconnect-PfaArray -Array $TargetFlashArray
188+
# Informational
189+
Write-Host "*********************************************************"
190+
Write-Host "Disconnecting from $($FlashArray)"
191+
}
192+
193+
# Disconnect from vCenter if we had to log into it
194+
If ($ConnectVc -eq $true) {
195+
Disconnect-VIserver -Server $Vcenter -Confirm:$False
196+
# Informational
197+
Write-Host "*********************************************************"
198+
Write-Host "Disconnecting from $($Vcenter)"
199+
}

0 commit comments

Comments
 (0)