下列 PowerShell 函數會使用 vSphere PowerCLI 傳回 vSphere 詳細目錄物件的完整路徑。

# 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
    }
}