-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfn_Update-XIOSnapshotSetFromCG_example.ps1
81 lines (70 loc) · 3.73 KB
/
fn_Update-XIOSnapshotSetFromCG_example.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
function Update-XIOSnapshotSetFromCG {
<# .Description
Function to update a snapshot set from a consistency group. This is meant to be an example of how to invoke the XIO REST API to perform such operation. The "real" Update-XIOSnapshot function will support all of the "from" and "to" options:
from consistencygroup, from snapshotset, or from volume
to consistencygroup, to snapshotset, or to volume
.Example
Update-XIOSnapshotSetFromCG -Backup -ComputerName somexms.dom.com -Credential $credMe -FromConsistencyGroup mattTestCG0 -ToSnapshotSet SnapshotSet.1455487516618
Update the given SnapshotSet form the given Consistency Group, and make a backup SnapshotSet of the original SnapshotSet
#>
param(
## Name of XMS to which to connect to perform action
[parameter(Mandatory=$true)]$ComputerName,
## Credential to use for connecting to XMS
[parameter(Mandatory=$true)]$Credential,
## Name of Consistency Group from which to update item
[parameter(Mandatory=$true)][string]$FromConsistencyGroup,
## Name of SnapshotSet to update from Consistency Group
[parameter(Mandatory=$true)][string]$ToSnapshotSet,
## Switch: Keep a backup of the destination SnapshotSet?
[switch]$Backup
)
process {
## make the parameter hashtable from which to make the JSON body for the request
$hshReqBodyForCreateAndReassign = @{
"from-consistency-group-id" = $FromConsistencyGroup
"to-snapshot-set-id" = $ToSnapshotSet
}
if (-not $Backup) {$hshReqBodyForCreateAndReassign["no-backup"] = $true}
## make the hashtable for the params for invoking the request
$hshParamForReq = @{
Uri = "https://$ComputerName/api/json/v2/types/snapshots"
## does not work in testing against XMS's apparent desire for Basic Auth -- need to use Authorization header
# Credential = $Credential
Body = $hshReqBodyForCreateAndReassign | ConvertTo-Json
Method = "Post"
Headers = @{Authorization = (Get-BasicAuthStringFromCredential -Credential $Credential)}
}
## call in traditional way with named parameters
# Invoke-RestMethod -Uri <someUri> -Credential $Credential -Body <somethingToGetJson> -Method Post
Invoke-RestMethod @hshParamForReq
}
}
function Get-BasicAuthStringFromCredential {
<# .Description
Function to get a Basic authorization string value from a PSCredential. Useful for creating the value for an Authorization header item for a web request, for example. Based on code from Don Jones at http://powershell.org/wp/forums/topic/http-basic-auth-request/
.Outputs
String
#>
param(
[parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$Credential
) ## end param
return "Basic $( [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credential.UserName.TrimStart('\')):$($Credential.GetNetworkCredential().Password)")) )"
} ## end function
<#
fundamentals:
JSON creation from data structure (instead of trying to craft JSON strings manually)
Creds handling
Certificates
$oOrigServerCertificateValidationCallback = [System.Net.ServicePointManager]::ServerCertificateValidationCallback
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
Parameter "splatting"
REST method invocation
not covered here:
XMS computer name validation (valid DNS entry, responsive, etc.)
pre-POST validation: ensure that source Consistency Group and destination SnapshotSet exist
add ShouldProccess() support ("-WhatIf" support)
return meaningful objects upon success, not just some HREFs
to further understand:
this Update does: updates given snapshotset from orig CG, and optionally makes new snapshotset that is a copy of the given snapshotset before refresh? Or, the new snapshotset is the updated-from-CG item, and if doing -Backup, that leaves the original snapshotset in place?
#>