-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkp-clone-snap-to-datastore.ps1
108 lines (82 loc) · 3.48 KB
/
kp-clone-snap-to-datastore.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
#UNOFFICIAL SCRIPT FOR CLONING A DATASTORE AND PRESENTING IT BACK TO VMWARE
# ====================================================================
# Disclaimer: This script is written as best effort and provides no
# warranty expressed or implied. Please contact the author(s) if you
# have questions about this script before running or modifying
# ====================================================================
<##############################################################################
OVERVIEW
1. Initiates connections to element and vmware
2. Asks user for the volume name of the datastore it will clone
3. Lists snapshots associated to the volume
4. Asks for user input of the snapshot to use for the clone
5. Creates clone of the snapshot
6. Presents clone to vmware and resignatures it as a datastore to recover vms
################################################################################>
##################
#setup connections
##################
#$hci = "mvip"
#$hciuser = "admin"
#$cred = "password"
#$vcenterip = "vcenterIP"
#$vuser = "administrator@vsphere.local"
# remove Force:$true if you want to validate vSphere certificate
Connect-SFCluster -Target $hci -UserName $hciuser -Password $cred
Connect-VIServer -Server $vcenterip -Force:$true -User $vuser -Password $cred
#######################################
#grab datastore volume and snapshotinfo
#######################################
#grab the volume name
$name = Read-Host "Volume name of datastore you are restoring"
If(!(Get-SFVolume $name) -notcontains ""){
Write-Host "There are no volumes with this name"
Write-Host "quitting script"
break;
}
#grab the volumeid from the volume you entered above
$vname = get-sfvolume -name $name
$volid = $vname.VolumeID
$volaccessgroups = $vname.VolumeAccessGroups
#list all snapshots associated with the volume
$snaplist = Get-SFVolume -Name $name | get-sfsnapshot
If(!($snaplist) -notcontains ""){
Write-Host "There are no snapshots associated with volume $name"
Write-Host "quitting script"
break;
}
Get-SFVolume -Name $name | get-sfsnapshot
$snapid = Read-Host "What SnapshotID would you like to clone"
###########################################################
#create a clone from the original volume id and snapshot id
###########################################################
$ms = (get-date).millisecond
$clonename = $name + $ms
$newclone = New-SFClone -VolumeID $volid -SnapshotID $snapid -Name $clonename
Write-Host "waiting for clone to complete"
#loop process until the clone has completed
while($running -eq $null){
if($CheckUser -le '100'){
$CheckUser++
start-sleep -s 10
$running = Get-sfvolume -volumeid $newclone.VolumeID
write-host "waiting for clone to repeat. will try again in 10 seconds"
}else{
write-host "not working"
}
}
write-host "clone creation has completed. continuing"
#add volume to SF AccessGroup and account
$clonevol = Get-SFVolume $clonename
foreach ($number in $volaccessgroups){Add-SFVolumeToVolumeAccessGroup -VolumeID $clonevol.VolumeID -VolumeAccessGroupID $number}
###################################################
#take clone, create datastore, and map it to vmware
###################################################
#rescan VMware
Get-VMhost | Get-VMhostStorage -RescanAllHba -RescanVMFs
#setup esxlci
$vmhost = Get-VMhost | Select -First 1
$esxcli = Get-EsxCLi -VMhost $vmhost
#list snapshots to find the cloned volume
$snaplist = $esxcli.storage.vmfs.snapshot.list()
$esxcli.storage.vmfs.snapshot.resignature($name)