Recently i wrote a function to check in which vm folder particular vm resides. I thought i could share it 😉 This function can output folder names or folder morefs when -moref switch is specified. Sample output would be this:
get-vm ‘vm123’ | get-vmfolderpath
MY DC1\vm\test123\ntest\btest\xtest\
Get-VMfolderPath (get-vm vm1).folderid
MY DC1\vm\test123\ntest\btest\xtest\
When you want to instead of names , folder morefs:
Get-VMfolderPath (get-vm vm1).folderid -moref or
get-vm ‘vm123’ |Get-VMfolderPath -moref
Datacenter-datacenter-242\Folder-group-v243\Folder-group-v740\Folder-group-v1434\Folder-group-v1435\Folder-group-v1436\
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
function Get-VMFolderPath { <# .Synopsis Get vm folder path. From Datacenter to folder that keeps the vm. .Description This function returns vm folder path. As a parameter it takes the current folder in which the vm resides. This function can throw either ‘name’ or ‘moref’ output. Moref output can be obtained using the -moref switch. .Example get-vm ‘vm123’ | get-vmfolderpath Function will take folderid parameter from pipeline .Example get-vmfolderpath (get-vm myvm123|get-view).parent Function has to take as first parameter the moref of vm parent folder. DC\VM\folfder2\folderX\vmvm123 Parameter will be the folderX moref .Example get-vmfolderpath (get-vm myvm123|get-view).parent -moref Instead of names in output, morefs will be given. .Parameter folderid This is the moref of the parent directory for vm.Our starting point.Can be obtained in serveral ways. One way is to get it by: (get-vm ‘vm123’|get-view).parent or: (get-view -viewtype virtualmachine -Filter @{‘name’= ‘vm123’}).parent .Parameter moref Add -moref when invoking function to obtain moref values .Notes NAME: Get-VMFolderPath AUTHOR: Grzegorz Kulikowski LASTEDIT: 09/14/2012 NOT WORKING ? #powercli @ irc.freenode.net .Link http://psvmware.wordpress.com #> param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$folderid, [switch]$moref ) $folderparent = get-view $folderid if ($folderparent.name -ne 'vm') { if ($moref) { $path = $folderparent.moref.toString() + '\' + $path } else { $path = $folderparent.name + '\' + $path } if ($folderparent.parent) { if ($moref) { get-vmfolderpath $folderparent.parent.tostring() -moref } else { get-vmfolderpath($folderparent.parent.tostring()) } } } else { if ($moref) { return (get-view $folderparent.parent).moref.tostring() + '\' + $folderparent.moref.tostring() + '\' + $path } else { return (get-view $folderparent.parent).name.toString() + '\' + $folderparent.name.toString() + '\' + $path } } } |
