Skip to content

Instantly share code, notes, and snippets.

@codyhosterman
Created December 23, 2022 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyhosterman/2f896df8536af64ac5b5f071ca0f8118 to your computer and use it in GitHub Desktop.
Save codyhosterman/2f896df8536af64ac5b5f071ca0f8118 to your computer and use it in GitHub Desktop.
Create new vVol datastore on Flasharray
[CmdletBinding()]
Param(
[Parameter(Position=0,mandatory=$true,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$Cluster,
[Parameter(Position=1,mandatory=$true)]
[string]$FlashArray,
[Parameter(Position=2,ValueFromPipeline=$True,mandatory=$true)]
[System.Management.Automation.PSCredential]$Credentials,
[Parameter(Position=3,mandatory=$true)]
[string]$DatastoreName
)
import-module PureStoragePowerShellSDK2
$FlashArray = Connect-Pfa2Array -EndPoint $FlashArray -Credential $Credentials -IgnoreCertificateError
Write-Host "Creating pod named $($DatastoreName)..."
$pod = New-Pfa2Pod -Name $DatastoreName
Write-Host "Creating protocol endpoint..."
$pe = New-Pfa2Volume -Name $($pod.name + "::" + $pod.name +"PE") -Subtype protocol_endpoint
$esxiHosts = $cluster |Get-VMHost
$fahosts = Get-Pfa2Host
Write-Host "Finding host group for cluster named $($Cluster.name)..."
foreach ($esxiHost in $esxiHosts)
{
$iscsiadapter = $esxi | Get-VMHostHBA -Type iscsi | Where-Object {$_.Model -eq "iSCSI Software Adapter"}
$wwns = $esxi | Get-VMHostHBA -Type FibreChannel | Select-Object VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
if ($null -ne $iscsiadapter)
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
foreach ($fahost in $fahosts)
{
if ($fahost.iqns.count -ge 1)
{
foreach ($fahostiqn in $fahost.iqns)
{
if ($iqn.ToLower() -eq $fahostiqn.ToLower())
{
$faHostGroup = $fahost.HostGroup
break
}
}
}
if ($null -ne $faHostGroup)
{
break
}
}
}
if (($null -ne $wwns) -and ($null -eq $faHostGroup))
{
foreach ($wwn in $wwns)
{
foreach ($fahost in $fahosts)
{
if ($fahost.wwns.count -ge 1)
{
foreach($fahostwwn in $fahost.wwns)
{
if ($wwn.ToLower() -eq $fahostwwn.ToLower())
{
$faHostGroup = $fahost.HostGroup
break
}
}
if ($null -ne $faHostGroup)
{
break
}
}
if ($null -ne $faHostGroup)
{
break
}
}
if ($null -ne $faHostGroup)
{
break
}
}
}
if ($null -ne $faHostGroup)
{
break
}
}
Write-Host "Connecting PE to host group named $($faHostGroup.name)..."
New-Pfa2Connection -HostGroupNames $faHostGroup.Name -VolumeNames $pe.Name |Out-Null
Write-Host "Refreshing VASA Provider container list..."
Get-VasaProvider -id (Get-VasaStorageArray -Id ("com.purestorage:" + (Get-Pfa2Array).id)).provider.id -Refresh
Write-Host "Mounting vVol Storage Container as a vVol datastore to cluster named $($Cluster.Name)..."
foreach ($esxi in $esxiHosts)
{
Write-Host "Mounting on ESXi host $($esxi.NetworkInfo.HostName)..."
$datastoreSystem = Get-View -Id $esxi.ExtensionData.ConfigManager.DatastoreSystem
$spec = New-Object VMware.Vim.HostDatastoreSystemVvolDatastoreSpec
$spec.Name = $DatastoreName
$spec.ScId = ($("vvol:" + ($pod.id).replace("-",""))).insert(21,"-")
$foundDatastore = $esxi |get-datastore |Where-Object {$_.Type -eq "VVol"} |Where-Object {$_.ExtensionData.Info.VVolds.Scid -eq $spec.ScId}
if ($null -eq $foundDatastore)
{
$datastore = Get-Datastore -Id ($datastoreSystem.CreateVvolDatastore($spec))
}
}
return $datastore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment