These functions provide assistance dealing with Datastores. I have included the code in this post and in an attached file.
Comments and improvements are welcomed.
All functions, with some usage information and a sample, is in the attachment
Get-DatastoreHost -Datastore $DS -IsConnected $True returns a list of VIM VMHost objects that have access to the store. Use IsConnected if you only want ones that are properly alive.
function Get-DatastoreHost {
Param(
[http://VMware.VimAutomation.Client20.DatastoreImpl|http://VMware.VimAutomation.Client20.DatastoreImpl] $Datastore,
[<b>boolean</b>] $IsConnected=$True
)
($Datastore | Get-View).Host | ForEach-Object { get-view -id $_.Key } | `
Where-Object { (-not $IsConnected) -or ($_.Runtime.ConnectionState -eq "connected") }
}
Get-LocalDatastore -VMHost $Host returns a list of VIToolkit Datastore objects which are hosted on devices of type ParallelSCSIHBA or BlockHBA. I think these are only owned by a local ESX server. I wanted this code to reliably rename local datastores so they aren't accidentally selected when operations teams provisions VMs
function Get-LocalDatastore {
Param(
[http://VMware.VimAutomation.Client20.VMHostImpl|http://VMware.VimAutomation.Client20.VMHostImpl] $VMHost
)
$LocalDevices=(Get-VMHostStorage -VMHost $VMHost | Get-View).StorageDeviceInfo.HostBusAdapter | `
Where-Object { `
($_.Key -like "key-vim.host.ParallelSCSIHBA-*") -or `
($_.Key -like "key-vim.host.BlockHBA-*") `
} | %{ [string]($_.Device) }
Get-Datastore -VMHost $VMHost | Where-Object {
(($_ | Get-View).Info.vmfs.extent | %{ ([string]$_.DiskName).split(":",2)[0]} ) `
-contains $LocalDevices
}
}
Get-DatastoreDatacenter -Datastore $DS There are some (e.g., vifs.pl) processes that insist on a Datacenter in some cases. This returns the datacenter VIToolkit object a given Datastore is in. This could also be easily adapted to hosts.
function Get-DatastoreDatacenter {
Param(
$Datastore
)
Get-Datacenter | Where-Object {
(($_ | Get-View).Datastore | %{ (Get-View -Id $_).Summary.Url}) `
-contains (($Datastore | Get-View).Summary.Url)
}
}
Edited on Oct 10 to replace completely wrong Get-DatastoreDatacenter