SpectrumConnect-Client.Sample.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
##############################################################################
# #
# Licensed Materials - Property of IBM #
# #
# IBM Storage Automation Plugin for PowerShell #
# #
# (C) COPYRIGHT International Business Machines Corp. 2013, 2018 #
# All Rights Reserved #
# #
# US Government Users Restricted Rights: Use, duplication or #
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. #
# #
##############################################################################
<#
This sample script demostrates how to integrate the SpectrumConnect-Client module in your automation solution to provision volumes for Microsft/HyperV hosts. And the following cmdlets are used in this sample script:
 
* New-SCConnection
* Get-SCService
* Get-SCHostVolMapping
* Get-SCVolume
* New-SCVolume
* Resize-SCVolume
* Remove-SCVolume
 
For other cmdlets which are not covered in this sample, please refer to corresponding Help Text or Examples for more information.
#>

function Task([object] $information){
    Write-Host $information -ForegroundColor Green
}

function Info([Object] $info){
    Write-Host $info -ForegroundColor Yellow
}

function ASSERT_NOTNULL([Object] $obj){
    if($obj -eq $null){
        throw "NULL for the object($obj)"         
    }else{
        Write-Verbose "ASSERT: Object is not null"
    }
}

#Retrieve the FC WWPN or iSCSI initiator ports from Windows host server; Wrapper of Get-InitiatorPort and Get-WmiObject.
#Alternatively, you can just issue the Get-SCHost to retrived the initiators information.
function Get-HostInitiator{
    param(
        [Alias("ComputerName")]
        [String] $HostName,        
        [PSCredential] $Credential,
        [ValidateSet("FibreChannel","iSCSI","SAS")]    
        [String] $ConnectionType
    )
    $CmdletName="Get-HostInitiator"
    Write-Verbose ("$CmdletName.ENTER: HostName=$HostName, ConnectionType=$ConnectionType")
    $pcname=""
    if([String]::IsNullOrEmpty($HostName)){
        $pcname="localhost"
    }else{
        $pcname=$HostName
    }    
    try{
        $cmd=Get-Command -Name Get-InitiatorPort
        Write-Verbose("$CmdletName`: Get-InitiatorPort")
        if([String]::IsNullOrEmpty($ConnectionType)){
            return Get-InitiatorPort -CimSession $pcname|%{if($_.ConnectionType -eq "Fibre Channel"){$_.PortAddress.ToUpper()}else{$_.NodeAddress}}
        }else{
            return Get-InitiatorPort -ConnectionType $ConnectionType -CimSession $pcname|%{if($_.ConnectionType -eq "Fibre Channel"){$_.PortAddress.ToUpper()}else{$_.NodeAddress}}
        }
        Write-Verbose("$CmdletName.EXIT")
    }
    catch{
        Write-Verbose "$CmdletName`: Exception found with information $($_.Exception.Message)"
        Write-Verbose "$CmdletName`: Fail to find the Cmdlet Get-InitiatorPort. Please ensure Storage Cmdlets are installed. Also, please ensure Windows PowerShell Remoting is enabled"
        Write-Verbose "$CmdletName`: Try getting the initiator ports via WS-Management."
        try{
            if($credential -ne $null){
                $hbas = Get-WmiObject -Namespace root/Microsoft/Windows/Storage -Class MSFT_InitiatorPort -ComputerName $pcname -Credential $credential
            }else{
                $hbas = Get-WmiObject -Namespace root/Microsoft/Windows/Storage -Class MSFT_InitiatorPort -ComputerName $pcname
            }
            Write-Verbose("$CmdletName.EXIT")        
            if($hbas -ne $null){
                if([String]::IsNullOrEmpty($ConnectionType)){
                    return $hbas | %{if($_.ConnectionType -eq 1){$_.PortAddress.ToUpper()}else{$_.NodeAddress}}
                }else{
                    $conType="Other|FibreChannel|iSCSI|SAS".Split("|").IndexOf($ConnectionType)
                    return ($hbas|?{$_.ConnectionType -eq $conType})%{if($_.ConnectionType -eq 1){$_.PortAddress.ToUpper()}else{$_.NodeAddress}}
                }
            }else{
                return $null
            }
        }catch{
            throw $_.Exception
        }
    }
}

Task "[1] Import the PowerShell Client Module for IBM SpectrumConnect"
Info "You can get the module from PowerShell Gallery."
Info "To get the module from PowerShell Gallery, you can just issue ""Find-Module -Name SpectrumConnect-Client|Install-Module -Force"";"
Info "For this sample script, the module is just saved to local folder and is imported directly using Import-Module cmdlet."
Import-Module R:\SpectrumConnect-Client\SpectrumConnect-Client.psd1
$module=Get-Module -Name SpectrumConnect-Client 
if($module.Name -eq "SpectrumConnect-Client"){
    Info "The Module SpectrumConnect-Client has been imported."
    Info "Detailed information about SpectrumConnect-Client module:"
    $module|Format-List
}

Info "PREPARATION 1: Define storage pools for allocations, and ensure that all the required hosts are connected to the storage system."
$computerName="win2012r2x215"        # The Windows Host server with iSCSI(or FC) enabled.

$domainAdmin="Domain01\User01"        # Specifies a user account that has permission to perform this action on the windows host $computerName.
$domainPassword="Passw0rd"

Info "PREPARATION 2: On the SC server, Add a PowerShell interface and create the following PowerShell interface user"
$scLocation="https://10.0.1.15:8440"    # The Uri of the SC server, in the format of https:\\IP:Port
$scUsername="powershell"                    # Credential for the PowerShell interface user.
$scPassword="passw0rd"

Info "PREPARATION 3: On the SC server, add a new storage space, add storage services, and attach services to the PowerShell interface created in PREPARATION 2."
$spaceName="DemoSpace"                        # Space name.
$serviceName="CompressionService"            # Service name. For this sample, this service can be used to create Compression volumes.

$LunPrefix="SCDemoVol"    # The Prefix for the created volumes

Task "[2] Connect to the SC Server and Create Volumes via SC Service"
##Note: New-SCConnection supports creating the connection using PowerShell Credential object, and you can also pass a PScredential object here
## Or use the user name/password explicitly: New-SCConnection -ConnectionUri $scLocation -UserName $scUsername -Password $scPassword
$ssPsw=ConvertTo-SecureString -AsPlainText -Force $scPassword; 
$cred=New-Object pscredential $scUsername,$ssPsw; 
$sc=New-SCConnection -ConnectionUri $scLocation -Credential $cred
Info "Successfully Connect to the SC server($scLocation)!"
$sc|Format-Table -AutoSize

if([String]::IsNullOrEmpty($sc.Token)){
    Throw "Fail to build the connection to the SC server."
    return $False
}

Task "[2.1] Get the service instance via service name"
$service=Get-SCService -SCConnection $sc -ServiceName $serviceName
ASSERT_NOTNULL $service
Info "Service with name $serviceName found:"
$service|Format-Table name,id,total_capacity,space_name -AutoSize

Task "[2.2] Get the host initiators from Windows host"
$ssDomainPsw=ConvertTo-SecureString -AsPlainText -Force $domainPassword
$credential=New-Object PSCredential $domainAdmin,$ssDomainPsw
$iSCSIPort=(Get-HostInitiator -HostName $computerName -Credential $credential -ConnectionType iSCSI)

Task "[2.3] Add a volume via SC service, and register it to specified iSCSI initiator"
$volName= $LunPrefix + [System.DateTime]::Now.ToString("yyMMddHHmmss")
$vol=New-SCVolume -SCConnection $sc -Name $volName -Size 40  -SizeUnit GiB -ServiceID $service.ID -Initiators $iSCSIPort
if($vol -eq $null){
    Info "Fail to create new volume"
    return $False
}

Info "The following volume is created:"
$vol|Format-Table name,id,array_name,pool_name,service_name,ratio -AutoSize
$mapping=Get-SCHostVolMapping -SCConnection $sc -VolumeID $vol.id 
Info "And its mapping information is listed below:"
$mapping|Format-Table -AutoSize

Task "[3] Mount the volume on Windows host"
Update-HostStorageCache -CimSession $computerName
$disk=Get-Disk -CimSession $computerName -UniqueId $vol.id
ASSERT_NOTNULL $disk
Info "Disk found on Windows host $computerName with following information:"
$disk|Format-Table Number,ProvisioningType,OperationalStatus,AllocatedSize,UniqueId,PartitionStyle,IsOffline,IsReadOnly,PSComputerName -AutoSize
Task "[3.1] Bring the Disk Online if the disk is Offline"
if($disk.OperationalStatus -eq "Offline"){
    Info "The disk is offline by default and will bring the disk Online"
    Set-Disk -CimSession $computerName -UniqueId $disk.UniqueId -IsOffline $False
    $disk=Get-Disk -CimSession $computerName -UniqueId $disk.UniqueId    
    if($disk.OperationalStatus -eq "Online"){
        Info "Succeed to bring the disk Online"
        $disk|Format-Table Number,ProvisioningType,OperationalStatus,AllocatedSize,UniqueId,PartitionStyle,IsOffline,IsReadOnly,PSComputerName -AutoSize
    }else{
        Info "Fail to bring the disk online"
        return $false
    }
}

Task "[3.2] Initialize the disk"
if($disk.PartitionStyle -eq "RAW"){
    Info "The partition style of the disk is detected to be RAW, and will initialize this disk to MBR partition style"
    Initialize-Disk -CimSession $computerName -UniqueId $disk.UniqueId -PartitionStyle MBR -Confirm:$False
    $disk=Get-Disk -CimSession $computerName -UniqueId $disk.UniqueId    
    if($disk.PartitionStyle -ne "RAW"){
        Info "Succeed to initialize the disk to $($disk.PartitionStyle)"
        $disk|Format-Table Number,ProvisioningType,OperationalStatus,AllocatedSize,UniqueId,PartitionStyle,IsOffline,IsReadOnly,PSComputerName -AutoSize
    }
}else{
    Info "Ignored for the partition style of the disk is detected to be $($disk.PartitionStyle)"
}

Task "[3.3] Format the Disk and Mount with a DriveLetter"
$partition=New-Partition -DiskNumber $disk.Number -AssignDriveLetter -UseMaximumSize -CimSession $computerName
$partition|Format-Table PartitionNumber,Offset,Size,IsActive,DriveLetter,PSComputerName -AutoSize
Format-Volume -DriveLetter $partition.DriveLetter -FileSystem NTFS -NewFileSystemLabel "Compressed" -Force -Confirm:$False -CimSession $computerName
Get-Volume -DriveLetter $partition.DriveLetter -CimSession $computerName|Format-Table DriveLetter,DriveType,FileSystem,HealthStatus,Size,SizeRemaining,FileSystemLabel,PSComputerName -AutoSize


Task "[4] Resize the volume"
Info "Before Resizing the Volume via SC Service"
$vol|Format-Table name,id,array_name,pool_name,logical_capacity,ratio -AutoSize

Task "[4.1] Resize the volume to 80GiB"
$ret=Resize-SCVolume -SCConnection $sc -VolumeID $vol.id -NewSize 80 -SizeUnit GiB
if($ret){
    Info "Succeed to resize the disk, and the information of the volume is listed below:"
    $vol=Get-SCVolume -SCConnection $sc -VolumeID $vol.id
    $vol|Format-Table name,id,array_name,pool_name,logical_capacity,ratio -AutoSize
}

Info "[4.2] Update the Volume on Windows Host"
Update-Disk -CimSession $computerName -UniqueId $disk.UniqueId
$disk=Get-Disk -CimSession $computerName -UniqueId $disk.UniqueId    
Info "Disk information after updating"
$disk|Format-Table Number,ProvisioningType,OperationalStatus,AllocatedSize,UniqueId,PartitionStyle,IsOffline,IsReadOnly,PSComputerName -AutoSize

Task "[5] Remove the volume $($vol.name)"
Pause
$ret=Remove-SCVolume -SCConnection $sc -ID $vol.id -Force
if($ret){
    info "Succeed to remove the volume $($vol.name)!"
}else{
    info "Fail to remove the volume $($vol.name)!"
}

# SIG # Begin signature block
# MIINMwYJKoZIhvcNAQcCoIINJDCCDSACAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUBc81I3RQwqXceKGlr8G9VTyT
# QiygggpoMIIFBzCCA++gAwIBAgIQO50nymBTQHmVgRJdr11LljANBgkqhkiG9w0B
# AQsFADB/MQswCQYDVQQGEwJVUzEdMBsGA1UEChMUU3ltYW50ZWMgQ29ycG9yYXRp
# b24xHzAdBgNVBAsTFlN5bWFudGVjIFRydXN0IE5ldHdvcmsxMDAuBgNVBAMTJ1N5
# bWFudGVjIENsYXNzIDMgU0hBMjU2IENvZGUgU2lnbmluZyBDQTAeFw0xNzExMTAw
# MDAwMDBaFw0yMDEyMDkyMzU5NTlaMIGgMQswCQYDVQQGEwJDTjEQMA4GA1UECAwH
# QmVpamluZzEQMA4GA1UEBwwHQmVpamluZzEvMC0GA1UECgwmSUJNIChDaGluYSkg
# SW52ZXN0bWVudCBDb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