I'm using vSphere 6.0 and the newest PowerCLI. I want to get the name of a VM and it's path. Something like get-vm myvmware | get-folderpath
I've tried a couple solutions found online, but either they don't work at all or they get all the VMs. I have a list of VMs I need to feed to this to get their paths
You could do something like this with a function. It doesn't appear that VMware has anything like this built-in.
Function Get-VMFolderPath {
param([string]$VMFolderId)
$Folders = [system.collections.arraylist]::new()
$tracker = Get-Folder -Id $VMFolderId
$Obj = [pscustomobject][ordered]@{FolderName = $tracker.Name; FolderID = $tracker.Id}
$null = $Folders.add($Obj)
while ($tracker) {
if ($tracker.parent.type) {
$tracker = (Get-Folder -Id $tracker.parentId)
$Obj = [pscustomobject][ordered]@{FolderName = $tracker.Name; FolderID = $tracker.Id}
$null = $Folders.add($Obj)
}
else {
$Obj = [pscustomobject][ordered]@{FolderName = $tracker.parent.name; FolderID = $tracker.parentId}
$null = $Folders.add($Obj)
$tracker = $null
}
}
$Folders.Reverse()
$Folders.FolderName -join "/"
}
$VM = Get-VM "VM Name"
Get-VMFolderPath $VM.Folder.Id
This will list the folder path starting from the Data Center container down to the VM folder that contains the VM.