Integration

Table Of Contents
VMware, Inc. 49
Chapter 3 Using View PowerCLI
}
}
Determining Paths to vSphere Inventory Objects
Define a PowerShell function that uses vSphere PowerCLI to return the full path to a vSphere inventory object.
For a function that you can use to determine datastore paths, see “Determining Paths to vSphere Datastore
Objects” on page 49.
# VVGetInventoryPath
# Parameters
# $InvObject Inventory object in vSphere PowerCLI.
#
# Examples
# VVGetInventoryPath (Get-VM -name myVM)
# VVGetInventoryPath (Get-ResourcePool | Select -first 1)
function VVGetPath($InvObject){
if($InvObject){
$objectType = $InvObject.GetType().Name
$objectBaseType = $InvObject.GetType().BaseType.Name
if($objectType.Contains("DatastoreImpl")){
Write-Error "Use the VVGetDataStorePath function to determine datastore paths."
break
}
if(-not ($objectBaseType.Contains("InventoryItemImpl") -or
$objectBaseType.Contains("FolderImpl") -or
$objectBaseType.Contains("DatacenterImpl") -or
$objectBaseType.Contains("VMHostImpl") ) ){
Write-Error ("The provided object is not an expected vSphere object type. Object type
is " + $objectType)
break
}
$path = ""
# Recursively move up through the inventory hierarchy by parent or folder.
if($InvObject.ParentId){
$path = VVGetPath(Get-Inventory -Id $InvObject.ParentId)
} elseif ($InvObject.FolderId){
$path = VVGetPath(Get-Folder -Id $InvObject.FolderId)
}
# Build the path, omitting the "Datacenters" folder at the root.
if(-not $InvObject.isChildTypeDatacenter){ # Add object to the path.
$path = $path + "/" + $InvObject.Name
}
$path
}
}
Determining Paths to vSphere Datastore Objects
Define a PowerShell function that uses vSphere PowerCLI to return the full path to a datastore in a cluster as
specified by a resource pool.
# VVGetDatastorePath
# Parameters
# $Datastore Datastore object in vSphere PowerCLI.
# $ResourcePool Resource pool in cluster.
#
#Example
# VVGetDatastorePath (Get-Datastore "datastore1") (Get-ResourcePool "Resources")
function VVGetDatastorePath($Datastore,$ResourcePool){
if($Datastore -and $ResourcePool){