InStor.PowerShell.Sample.ps1
<#
This sample script demostrates how to integrate the InStor.PowerShell module in your automation solution to provision volumes for Microsoft hosts. The following cmdlets are used in this sample script: *New-InStorConnection *Get-InStorPool *New-InStorHost *New-InStorVolume *New-InStorVolHostMap For other cmdlets which are not covered in this sample, please refer to User Guide or Examples for more information. #> function Task([object] $information){ Write-Host $information -ForegroundColor Green } function Info([Object] $info){ Write-Host $info -ForegroundColor Yellow } function Error([Object] $error){ Write-Host $error -ForegroundColor Red } function ASSERT_NOTNULL([Object] $obj){ if($null -eq $obj){ 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. function Get-WinHostInitiator{ param( [Alias("ComputerName")] [String] $HostName, [PSCredential] $Credential, [ValidateSet("FibreChannel","iSCSI","SAS")] [String] $ConnectionType ) $CmdletName="Get-WinHostInitiator" Write-Verbose ("$CmdletName.ENTER: HostName=$HostName, ConnectionType=$ConnectionType") $pcname="" if([String]::IsNullOrEmpty($HostName)){ $pcname="localhost" }else{ $pcname=$HostName } $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") } Task "[1] Import the PowerShell Client Module for InStor.PowerShell" Info "You can get the module from PowerShell Gallery." Info "To get the module from PowerShell Gallery, you can just issue ""Find-Module -Name InStor.PowerShell|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 C:\InStor.PowerShell_V1.0.0.0.Build_20190104\InStor.PowerShell\1.0.0.0\InStor.PowerShell.psd1 $module = Get-Module -Name InStor.PowerShell if ($module) { Info "The Module InStor.PowerShell has been imported." Info "Detailed information about InStor.PowerShell Module:" $module|Format-List } else { Error "The Module InStor.PowerShell has not been imported." } Info "Inspur mcs storage credential information." $instorUrl="100.7.46.160" $instorUsername = "superuser" $instorPassword = "Passw0rd." Info "Host that is connected to the Storage system." $computerName="win-ps-test01" # The Windows Host server with iSCSI enabled. $domainAdmin="administrator" # Specifies a user account that has permission to perform this action on the windows host $computerName. $domainPassword="123456a!" Task "[2] Connect to the MCS Storage , create a volume and map to the host." $conn = New-InStorConnection -StorUrl $instorUrl -UserName $instorUsername -Password $instorPassword Info "Successfully connect to the mcs storage" $conn|Format-Table Task "[2.1] Get the Storage Pool named 'Pool1600'." $pool0 = Get-InStorPool -Conn $conn -PoolName "Pool160" Info "Successfully get storage pool named Pool160." $pool0|Format-Table Task "[2.2] Get the host initiators from Windows host" $ssDomainPsw=ConvertTo-SecureString -AsPlainText -Force $domainPassword $credential=New-Object PSCredential $domainAdmin,$ssDomainPsw $iSCSIPort=(Get-WinHostInitiator -HostName $computerName -ConnectionType iSCSI) Info "iSCSI:$iSCSIPort" Task "[2.3] Create a Host with iSCSI on the Storage " $winHost = New-InStorHost -Conn $conn -Name $computerName -Iscsiname $iSCSIPort -WinHostName $computerName -PsCredential $credential -Portals @("100.7.46.55") Info "Successfully create host." $winHost|Format-Table Task "[2.4]Create Volume" $psVolume = New-InStorVolume -Conn $conn -Mdiskgrp $pool0.id -Name "PsDemoVolume" -Size 4 -Unit "gb" Info "Successfully create volume." $psVolume|Format-Table Task "[2.5]Maps volume to host" $volHostmap = New-InStorVolHostMap -Conn $conn -Host $winHost.id -Vdisk $psVolume.id Info "Successfully create hostmapping" $volHostmap|Format-Table Task "[3]Mount the volume on Windows host" Update-HostStorageCache -CimSession $computerName $disk=Get-Disk -CimSession $computerName -UniqueId $psVolume.vdisk_UID ASSERT_NOTNULL $disk Info "Disk found on Windows host $computerName with following information:" $disk|Format-Table "id", "name", "mdisk_grp_name","capacity","vdisk_UID","status" -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 "id", "name", "mdisk_grp_name","capacity","vdisk_UID","status" -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] Remove the volume $($psVolume.name)" #Remove-InStorVolume -Conn $conn -Force -Vdisk $psVolume.id |