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 } } } |
13 comments
Thanks to Hendrick who spot my typos ! 🙂
GREAT Funtion.. one thing to note is it does not work when powercli is connected to multiple vcenters
set-powercliconfiguration -defaultVIservermode multiple
I believe this is because it calls the parent up to a null value.
Hi JD, i have tested it when connected to multiple VC. It works ok. Can you tell more what/when it is not working ? can you tell what version are you using ? Get-PowerCLIVersion ?
I can confirm that this does not work when connected to 2 vcenter servers at the same time.
If connected to 2 servers I get:
System.Object[]\System.Object[]\SupportCase\SupportVeeamJob9\
Meet in #powercli to discuss further
PowerCLI C:\> Connect-VIServer -Server abc.dcsprod.dcsroot.local -User dcsutil\auamu -Password “Tr1234”
Name Port User
—- —- —-
abc.dcsprod.dcsr… 443 dcsutil\auamu
PowerCLI C:\> get-vm -name ‘vmname’ | .\get-vmfolderpath.ps1
PowerCLI C:\>
The script is doing nothing for me why?
Please advice what I doing wrong as I pasted your script as Powershell script file and run it
Muhammad, first load the function so:
cd c:\folder\where\is\the\ps1\file
. ./get-vmfolderpath.ps1
and then
get-vm ‘vm123′ |Get-VMfolderPath
or just copy paste the function to your powercli/powershell window.
[…] swapped out LucD’s function for psvmware’s function Get-VMFolderPath and changed my code to the […]
Hi, I get a LOT of input for command get-vm “roast beef sandwich” | get-vmfolderpath – it looks like the path is repeating. Is this a known issue? For example, I’m getting something like TestDatacenter\VM\Test\Sandwiches\RedMeat\roast beef sandwich\TestDatacenter\VM\Test\Sandwiches\RedMeat\roast beef sandwich, or it will start outputting paths of different VM’s or folders. This is an amazing script if I can get it to work for me. Thanks!
Can you check :
$a = get-vm ‘My VM’
$a | get-vmfolderpath
or
get-vmfolderpath -folderid $a.folderid
and that
$a.count is not more than 1 ?
Ideally, join #powercli on freenode, and we can check it there. I tested it today, and that works for me, no problems.
Thanks, saved some time!
$VM_name = Read-Host Get vm name
$VM = Get-VM $VM_name
$VM.ExtensionData.Config.Files
Great, thank you 🙂