# encoding: ascii
# api: powershell
# title: Create datastore by LUN 
# description: Creates a VMFS datastore via powershell by specifying the target LUN ID
# version: 0.1
# type: function
# author: Ed Goad
# license: CC0
# function: New-DatastoreByLun
# x-poshcode-id: 3412
# x-archived: 2012-05-16T21:39:27
# x-published: 2012-05-14T08:48:00
#
# Modified from the original version at http://snipplr.com/view.php?codeview&id=48048
# To run: New-DatastoreByLun “myESXHost” “SANB” 9  0 “Datastore01”
#
function New-DatastoreByLun { param( [string]$vmHost, [string]$hbaId, [int]$targetId, [int]$lunId, [string]$dataStoreName )

  $view = Get-VMHost $vmHost | get-view

  $lun = $view.Config.StorageDevice.ScsiTopology | ForEach-Object { $_.Adapter } | Where-Object {$_.Key -match $hbaId} | ForEach-Object {$_.Target} | Where-Object {$_.Target -eq $targetId} | ForEach-Object {$_.Lun} | Where-Object {$_.Lun -eq $lunId}

  $scsiLun = Get-VMHost $vmHost | Get-ScsiLun | Where-Object {$_.Key -eq $lun.ScsiLun}

  New-Datastore -VMHost $vmHost -Name $dataStoreName -Path $scsiLun.CanonicalName -Vmfs -BlockSizeMB 8 -FileSystemVersion 3
}
