Skip to content

Commit e2e50ce

Browse files
Merge pull request #9 from mikenelson-io/main
Added more examples, corrected typos
2 parents e63bd89 + dc46ac8 commit e2e50ce

File tree

1 file changed

+102
-45
lines changed

1 file changed

+102
-45
lines changed

SDK2-Examples.ps1

+102-45
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Example PowerShell scripts for the Pure Storage PowerShell SDK
33
This contains the step-by-step example script that follows along with the Quick Start Guide (https://support.purestorage.com/Solutions/Microsoft_Platform_Guide/a_Windows_PowerShell) as well as some further examples to help you create your own scripts using the SDK.
44
5-
This script is AS-IS. No warranties expressed or implied by Pure Storage or the creator.
5+
These examples are presented AS-IS. No warranties expressed or implied by Pure Storage or the creators.
66
77
: REVISION HISTORY
88
:: 04.23.2021 - Fixed the -Credential parameter from plural to singular
@@ -20,12 +20,16 @@ This script is AS-IS. No warranties expressed or implied by Pure Storage or the
2020
# Basic PowerShell routines to see the cmdlets available, updating Help, and getting various degrees of Help
2121
Get-Module -ListAvailable
2222
$env:PSModulePath
23+
2324
# All of the available cmdlets in the module
2425
Get-Command -Module PureStoragePowerShellSDK2
26+
2527
# How many cmdlets are available in the module
2628
(Get-Command -Module PureStoragePowerShellSDK2).Count
29+
2730
# Periodically, the Help should be updated for all PowerShell modules. This does require PowerShell be "Run as Administrator".
2831
Update-Help -Module PureStoragePowerShellSDK2
32+
2933
# All the variations of Help available for cmdlets
3034
Get-Help New-Pfa2Array
3135
Get-Help New-Pfa2Array -Full
@@ -37,7 +41,7 @@ Get-Help New-Pfa2Array -Examples
3741
<#
3842
Connecting to the FlashArray
3943
40-
There are a couple of ways to connect to the FlashArray - via API Tokens or OAuth2. OAuth2 is more secure, but also a bit more complex.
44+
There are a few ways to connect to the FlashArray - via API Tokens or OAuth2. OAuth2 is more secure, but also a bit more complex.
4145
To see all of the ways to authenticate to an array with the SDK, inclusing with OAuth2, please see this article -
4246
https://support.purestorage.com/Solutions/Microsoft_Platform_Guide/a_Windows_PowerShell/How-To%3A_Connect_to_FlashArray_with_SDK_v12x
4347
@@ -50,112 +54,164 @@ https://<FQDN or IP Address of Array>/api/api_version
5054

5155
# For these examples, we will do a simple API Token exchange.
5256
$Creds = Get-Credential
53-
# Change the <IP/FQDN> to match your array
54-
$FlashArray = Connect-Pfa2Array -EndPoint <IP/FQDN> -Credential $Creds -IgnoreCertificateError
57+
58+
# Change the EndPopint IP/FQDN to match your array
59+
$FlashArray = Connect-Pfa2Array -EndPoint 192.0.0.1 -Credential $Creds -IgnoreCertificateError
60+
5561
# Once connected, run a few commands to verify connectivity
5662
Get-Pfa2Controller -Array $FlashArray
57-
$Controllers = Get-Pfa2Controller Array $FlashArray
63+
$Controllers = Get-Pfa2Controller -Array $FlashArray
5864
$Controllers
5965

6066
## Working with Volumes
6167
#######################
68+
6269
# Create a new volume that is 200GB in size and just to see what's happening, turn on the -Verbose parameter
63-
New-Pfa2Volume -Array $FlashArray -Name 'SDKv2-Sample' -Provisioned 214748364800 -Verbose
70+
New-Pfa2Volume -Array $FlashArray -Name "SDKv2-Sample" -Provisioned 214748364800 -Verbose
71+
6472
# Create 5 volumes, all 200GB in size
6573
ForEach ($i in 1..5) { New-Pfa2Volume -Array $FlashArray -Name "SDKv2-TestSample-$i" -Provisioned 214748364800 }
74+
6675
# View the volumes after creation
67-
Get-Pfa2Volume -Array $FlashArray -Name 'SDKv2-TestSample-3' | Format-Table –Autosize
68-
Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.name -like 'SDKv2-TestSample*' } | Format-Table -AutoSize
76+
Get-Pfa2Volume -Array $FlashArray -Name "SDKv2-TestSample-3" | Format-Table -Autosize
77+
Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.name -like "SDKv2-TestSample*" } | Format-Table -AutoSize
78+
6979
# Change the name of a volume
70-
Update-Pfa2Volume -Array $FlashArray -Name 'SDKv2-TestSample-5' -VolumeName 'SDKv2-TestSample-500'
80+
Update-Pfa2Volume -Array $FlashArray -Name "SDKv2-TestSample-5" -VolumeName "SDKv2-TestSample-500"
81+
7182
# Create a volume using the CLI via SSH
7283
$CommandText = "purevol create --size 10G DEMO-VOL10"
7384
Invoke-Pfa2CLICommand -EndPoint $ArrayEndpoint -Username $ArrayUsername -Password $ArrayPassword -CommandText $CommandText
7485

86+
# Use PowerShell built-in math functionaility to do data size conversions to bytes. You can see this from the PowerShell prompt by simply typing "1tb".
87+
<# This allows for some cmdlets that have a -Provisioned or -Size type parameter to use the simplier value and have it converted to the required value automagically.
88+
Example: The New-Pfa2Volume cmdlet requires a -Provisioned parameter value that is in bytes. By specifying the conversion value, it is automaticallyconverted to bytes by PowerShell.
89+
#>
90+
New-Pfa2Volume -Name DemoVol -Provisioned 100gb
91+
<# The result is:
92+
Space : class Space {
93+
TotalProvisioned: 107374182400
94+
}
95+
#>
96+
7597
## Volume and Host Connections
7698
##############################
99+
77100
# Define some (fake) WWNs and create a host
78-
$wwn=@('10:00:00:00:00:00:11:11','10:00:00:00:00:00:12:12')
79-
New-Pfa2Host –Array $FlashArray –Name SDKv2-host –WwnList $wwn
101+
$wwn=@("10:00:00:00:00:00:11:11","10:00:00:00:00:00:12:12")
102+
New-Pfa2Host -Array $FlashArray -Name "SDKv2-host" -WwnList $wwn
103+
80104
# Do the same for iSCSI Iqns
81-
$iqn = @('iqn.1998-01.com.sample1.iscsi', 'iqn.1998-01.com.sample2.iscsi')
82-
New-Pfa2Host -Array $FlashArray -Name 'SDK2-IQNS-host' -Iqns $iqn
105+
$iqn = @("iqn.1998-01.com.sample1.iscsi", "iqn.1998-01.com.sample2.iscsi")
106+
New-Pfa2Host -Array $FlashArray -Name "SDK2-IQNS-host" -Iqns $iqn
107+
83108
# Create a new Host Group
84-
New-Pfa2HostGroup -Array $FlashArray -Name 'SDKv2-HostGroup'
109+
New-Pfa2HostGroup -Array $FlashArray -Name "SDKv2-HostGroup"
110+
85111
# Add a host to the group
86-
New-Pfa2HostGroupHost -GroupNames 'SDKv2-HostGroup' -MemberNames 'TEST-HOST1'
112+
New-Pfa2HostGroupHost -GroupNames "SDKv2-HostGroup" -MemberNames "SDKv1-HOST1"
113+
87114
# Retrieve a volume and connect it to the host & host group
88-
Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.name -like 'SDK*' } | Format-Table -AutoSize
89-
New-Pfa2Connection -Array $FlashArray -VolumeNames 'SDKv2-Sample-2' -HostNames 'SDKv2-host'
90-
New-Pfa2Connection -Array $FlashArray -VolumeNames 'SDKv2-Sample-2' -HostGroupNames 'SDKv2-HostGroup'
115+
Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.name -like "SDK*" } | Format-Table -AutoSize
116+
New-Pfa2Connection -Array $FlashArray -VolumeNames "SDKv2-Sample-2" -HostNames "SDKv2-host"
117+
New-Pfa2Connection -Array $FlashArray -VolumeNames "SDKv2-Sample-2" -HostGroupNames "SDKv2-HostGroup"
118+
91119
# Remove the volume from the host group
92-
Remove-Pfa2Connection -Array $FlashArray -VolumeNames 'SDKv2-Sample-2' -HostGroupNames 'SDKv2-HostGroup'
120+
Remove-Pfa2Connection -Array $FlashArray -VolumeNames "SDKv2-Sample-2" -HostGroupNames "SDKv2-HostGroup"
93121

94122
## FlashRecover Snapshots
95123
#########################
124+
96125
# Create snapshots, view them, and copy a snapshot to a new volume
97-
New-Pfa2VolumeSnapshot -Array $FlashArray -SourceNames 'SDKv2-sample-1', 'SDKv2-sample-2' -Suffix 'EXAMPLE'
98-
Get-Pfa2VolumeSnapshot -Name 'SDKv2-sample-1.EXAMPLE'
99-
$src = New-Pfa2ReferenceObject -Id <returned snapshot ID> -Name <returned snapshot name>
126+
New-Pfa2VolumeSnapshot -Array $FlashArray -SourceNames "SDKv2-sample-1", "SDKv2-sample-2" -Suffix "EXAMPLE"
127+
$snap = Get-Pfa2VolumeSnapshot -Name "SDKv2-sample-1.EXAMPLE"
128+
$src = New-Pfa2ReferenceObject -Id $snap.Id -Name $snap.Name
100129
New-Pfa2Volume -Name volumecopy -Source $src
101130

102131
## Protection Groups
103132
####################
133+
104134
# Create a Protection group
105-
New-Pfa2ProtectionGroup -Array $FlashArray -Name 'SDKv2-PGROUP'
135+
New-Pfa2ProtectionGroup -Array $FlashArray -Name "SDKv2-PGROUP"
136+
106137
# Create a new volume
107-
New-Pfa2Volume -Name 'SDKv2-VOL1' -Provisioned 10485760
138+
New-Pfa2Volume -Name "SDKv2-VOL1" -Provisioned 10485760
139+
108140
# Add the volume to the Protection Group
109-
New-Pfa2ProtectionGroupVolume -GroupNames 'SDKv2-PGROUP' -MemberNames 'SDKv2-VOL1'
141+
New-Pfa2ProtectionGroupVolume -GroupNames "SDK-PGROUP" -MemberNames "SDK-VOL1"
142+
110143
# Snapshot the volume
111-
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames 'SDKv2-PGROUP' -Suffix 'EXAMPLE'
144+
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames "SDK-PGROUP" -Suffix "EXAMPLE"
145+
112146
# Set the snapshot schedule for the Protection Group
113-
Get-Pfa2ProtectionGroupSnapshot -Array $FlashArray -Name 'SDKv2-PGROUP'
114-
$schedule = Get-Pfa2ProtectionGroup -Array $FlashArray -Name 'SDKv2-PGROUP'
147+
Get-Pfa2ProtectionGroupSnapshot -Array $FlashArray -Name "SDK-PGROUP"
148+
$schedule = Get-Pfa2ProtectionGroup -Array $FlashArray -Name "SDK-PGROUP"
115149
$schedule.SnapshotSchedule
116150
$snapschedule=New-Pfa2SnapshotScheduleObject -At 7200000 -Enabled $false -Frequency 259200000
117-
Update-Pfa2ProtectionGroup -Name 'SDKv2-PGROUP' -SnapshotSchedule $snapschedule
151+
Update-Pfa2ProtectionGroup -Name "SDK-PGROUP" -SnapshotSchedule $snapschedule
152+
118153
# Remove the Protection Group
119-
Remove-Pfa2VolumeProtectionGroup -Array $FlashArray -MemberNames 'SDKv2-VOL1' -GroupNames 'SDKv2-PGROUP'
154+
Remove-Pfa2VolumeProtectionGroup -Array $FlashArray -MemberNames "SDK-VOL1" -GroupNames "SDK-PGROUP"
155+
120156
# Add a host to a Protection Group
121-
New-Pfa2ProtectionGroupHost -Array $FlashArray -GroupNames 'SDKv2-PGROUP' -Membernames 'SDKv2-HOST'
157+
New-Pfa2ProtectionGroupHost -Array $FlashArray -GroupNames "SDK-PGROUP" -Membernames "SDK-HOST"
158+
122159
# Create a snapshot
123-
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames 'SDKv2-PGROUP'
160+
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames "SDK-PGROUP"
161+
124162
# Remove the host
125-
Remove-Pfa2ProtectionGroupHost -Array $FlashArray -MemberNames 'SDKv2-HOST' -GroupNames 'SDKv2-PGROUP'
163+
Remove-Pfa2ProtectionGroupHost -Array $FlashArray -MemberNames "SDK-HOST" -GroupNames "SDK-PGROUP"
164+
126165
# Create a new Host Protection Group
127-
New-Pfa2HostGroupsProtectionGroup -Array $FlashArray -MemberNames 'SDKv2-HOSTGROUP' -GroupNames 'SDKv2-PGROUP'
166+
New-Pfa2HostGroupsProtectionGroup -Array $FlashArray -MemberNames "SDK-HOSTGROUP" -GroupNames "SDK-PGROUP"
167+
128168
# Create a snapshot of the group
129-
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames 'SDKv2-PGROUP'
169+
New-Pfa2ProtectionGroupSnapshot -Array $FlashArray -SourceNames "SDK-PGROUP"
170+
171+
# Alternative via the CLI with ReplicateNow
172+
$CommandText = "purepgroup snap --replicate-now SDK-PGROUP"
173+
Invoke-Pfa2CLICommand -EndPoint array1 -Credential (Get-Credential) -IgnoreCertificateError -CommandText $CommandText
130174

131175
## Performance and Space Metrics
132176
################################
133177
# These are just a small sample of many performance and metric cmdlets available in the SDK.
178+
134179
# This example will retrieve all of the cmdlets that have Performance in the name, and then run a few Array performance commands outputting in different formats
135180
Get-Command -Module PureStoragePowerShellSDK2 *Performance*
136-
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime '2020-08-07T01:00:00Z' -starttime '2020-08-06T01:00:00Z' -resolution 28800000
137-
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime '2020-08-07T01:00:00Z' -starttime '2020-08-06T01:00:00Z' -resolution 28800000 | Format-Table –AutoSize
138-
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime '2020-08-07T01:00:00Z' -starttime '2020-08-06T01:00:00Z' -resolution 28800000 | Export-Csv -Path C:\temp\test.csv
139-
Get-Pfa2ArraySpace -Array $FlashArray -endtime '2020-08-07T01:00:00Z' -starttime '2020-08-06T01:00:00Z' -resolution 28800000
181+
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime "2020-08-07T01:00:00Z" -starttime "2020-08-06T01:00:00Z" -resolution 28800000
182+
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime "2020-08-07T01:00:00Z" -starttime "2020-08-06T01:00:00Z" -resolution 28800000 | Format-Table –AutoSize
183+
Get-Pfa2ArrayPerformance -Array $FlashArray -endtime "2020-08-07T01:00:00Z" -starttime "2020-08-06T01:00:00Z" -resolution 28800000 | Export-Csv -Path "C:\temp\test.csv"
184+
Get-Pfa2ArraySpace -Array $FlashArray -endtime "2020-08-07T01:00:00Z" -starttime "2020-08-06T01:00:00Z" -resolution 28800000
140185

141186
## Logging
142187
##########
188+
143189
# Start SDK logging to a file calles session.log in the current folder
144190
Set-Pfa2Logging -LogFilename session.log
191+
145192
# You could now open a new PowerShell session and use the Get-Content cmdlet to view that file as it is written to
146193
Get-Content session.log -Wait
147194

148195

149196
## Pipeline examples
150197
####################
198+
199+
# Create a PowerShell array of authenticated FlashArray objects and perform a command against all of the objects in series
200+
$flasharray @()
201+
$flasharray += Connect-Pfa2Array -Endpoint array1 -Credential (Get-Credential) -IgnoreCertificateError
202+
$flasharray += Connect-Pfa2Array -Endpoint array2 -Credential (Get-Credential) -IgnoreCertificateError
203+
$flasharray += Connect-Pfa2Array -Endpoint array3 -Credential (Get-Credential) -IgnoreCertificateError
204+
$flasharray += Connect-Pfa2Array -Endpoint array4 -Credential (Get-Credential) -IgnoreCertificateError
205+
Get-Pfa2Volume -Array $flasharray
206+
151207
# Remove all of existing hosts
152208
Get-Pfa2Host -Array $FlashArray | Remove-Pfa2Host -Array $FlashArray
153209

154210
# Remove all hosts containing "test" in name
155-
(Get-Pfa2host -Array $FlashArray) | Where-Object { $_.name -like '*test*' } | Remove-Pfa2Host -Array $FlashArray
211+
(Get-Pfa2host -Array $FlashArray) | Where-Object { $_.name -like "*test*" } | Remove-Pfa2Host -Array $FlashArray
156212

157213
# Filter hosts and remove. Change name as fit.
158-
Get-Pfa2Host -Array $FlashArray | Where-Object { $_.name -eq 'a-test1' } | Remove-Pfa2Host -Array $FlashArray
214+
Get-Pfa2Host -Array $FlashArray | Where-Object { $_.name -eq "a-test1" } | Remove-Pfa2Host -Array $FlashArray
159215

160216
## Retrive formatted Volume info
161217
Get-Pfa2Volumes -Array $FlashArray | Format-Table -AutoSize
@@ -172,7 +228,7 @@ Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.Name -like $VolName } -Des
172228
Get-Pfa2Volume | Update-Pfa2Volume -Destroyed $True
173229

174230
# Destroy (delete) volumes
175-
Get-Pfa2Volume -Array $FlashArray -Filter "name='vol1'" -Destroyed $False | Update-Pfa2Volume -Array $FlashArray -Destroyed $True
231+
Get-Pfa2Volume -Array $FlashArray -Filter -name="vol1" -Destroyed $False | Update-Pfa2Volume -Array $FlashArray -Destroyed $True
176232

177233
# Query volumes to test for Destroy success
178234
$Volume = Get-Pfa2Volume -Array $FlashArray | Where-Object { $_.Name -like $VolName } -Destroyed $True
@@ -186,13 +242,14 @@ Get-Pfa2VolumeSnapshot -Array $FlashArray | Where-Object { $_.Suffix -eq "JustAD
186242

187243
## Miscellaneous
188244
################
245+
189246
# Use Invoke method to pass CLI command
190-
$CommandText = "purevol create --size 10G test-volume-name"
247+
$CommandText = "purevol create --size 10G SDK-volume-name"
191248
Invoke-Pfa2CLICommand -EndPoint $FlashArray -Username $FlashArrayUsername -Password $FlashArrayPassword -CommandText $CommandText
192249

193250
# Get snapshot information, create reference pbject, & create new volume from snapshot
194-
Get-Pfa2VolumeSnapshot -Name test-volume.testSnap.test1
195-
$src = New-Pfa2ReferenceObject -Id <returned snapshot ID> -Name <returned snapshot name>
251+
$snap = Get-Pfa2VolumeSnapshot -Name test1
252+
$src = New-Pfa2ReferenceObject -Id $snap.Id -Name $snap.Name
196253
New-Pfa2Volume -Name volumecopy -Source $src
197254

198255
# Query volumes to test for eradication success

0 commit comments

Comments
 (0)