I am attempting to use someold code I originally got from here:
Get-VMFolderPath to check in which vm folder our vm resides | VMware and Powershell
Which extracts the folder the VM resides in given a VM object.
I am finding it problematic when connected to 2 vcenters and end up getting:
System.Object[]\System.Object[]\SupportCase\SupportVeeamJob9\
instead of
DC\vm\SupportCase\SupportVeeamJob9\
Thoughts? Bug? Any help appreciated.
Dion
PSvmware's code:
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
https://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
}
}
}
Try this function, that works with multiple vCenter connections
function Get-FolderPath{
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[PSObject[]]$VM
)
Process{
$VM | %{
$server = ($_.GetClient()).ConnectionId.Split('@')[1].Split(':')[0]
$vmPath = New-Object 'System.Collections.Generic.List[string]'
$entity = Get-View -VIObject $_
$vmPath.Add($entity.Name)
while($entity.parent -ne $null){
$entity = Get-View -Id $entity.Parent
if('Datacenters','vm' -notcontains $entity.Name){
$vmPath.Insert(0,$entity.Name)
}
}
New-Object PSObject -Property @{
VM = $_.Name
FolderPath = $vmPath -join '/'
}
}
}
}
Get-VM | Get-FolderPath
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am still seeing different behaviors when connected to 2 different vcenters:
1 vcenter connected - looks good:
PS C:\Users\user> get-vm loadtest100 |get-FolderPath
Method invocation failed because [VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl] does not contain
a method named 'GetClient'.
At line:17 char:13
+ $server = ($_.GetClient()).ConnectionId.Split('@')[1].Spl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
VM FolderPath
-- ----------
loadtest100 DC/SupportCase/SupportVeeamJob9/loadtest100
2 Connected:
PS C:\Users\user> get-vm -Server vc.local loadtest100 |get-FolderPath
Method invocation failed because [VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl] does not contain a method named 'GetClient'.
At line:17 char:13
+ $server = ($_.GetClient()).ConnectionId.Split('@')[1].Spl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
VM FolderPath
-- ----------
loadtest100 Datacenters Datacenters Datacenters Datacenters Datacenters Datacenters Datacenters Datacenters/DC DC DC DC/vm vm/SupportCase/SupportVeeamJob9/loadtest100
Thoughts?
Was there some intention of using the -Server variable in this line:
$entity = Get-View -Id $entity.Parent -Server $Server
I get A parameter cannot be found that matches parameter name 'Server' when trying to run that.
I had to add $Server as a function argument - I assume its not a global:
function Get-FolderPath{
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[PSObject[]]$VM,
[PSObject[]]$Server
)
Which PowerCLI version are you using?
Can you check if the GetClient method is not available?
With for example:
(Get-VM | Select -First 1).GetClient()
Another feature that could influence the behaviour of the function, do you have VMs with the same DisplayName in the vCenters?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
function Get-VIObjFullPath {
<#
.NOTES
===========================================================================
Created by: LucD
Modifyied by: Evan Waite
Date: 2018/02/08
Ref: https://communities.vmware.com/message/2743814
===========================================================================
.SYNOPSIS
Return the full path of a VM, Folder or other object type
.DESCRIPTION
Returns the full folder path
.EXAMPLE
Get-VM -Name "myVM1" | Get-VIObjFullPath
Pipe a VM object in to get the full path.
.EXAMPLE
Get-Folder -Name "myFolder" -Type VM | Get-VIObjFullPath
Pipe a Folder object in to get the full path.
#>
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[PSObject[]]$VIObj
)
Process{
$VIObj | %{
$server = $_.Client.ConnectionId.Split('@')[1].Split(':')[0]
$objPath = New-Object 'System.Collections.Generic.List[string]'
$entity = Get-View -VIObject $_
$objPath.Add($entity.Name)
while($entity.parent -ne $null){
$entity = Get-View -Id $entity.Parent -Server $Server
if('Datacenters','vm','datastore','host','network' -notcontains $entity.Name){
$objPath.Insert(0,$entity.Name)
}
if($entity.Name -like "network" -and $_.VirtualSwitch.Name -ne $null) {
$objPath.Insert(0,$_.VirtualSwitch.Name)
}
}
New-Object PSObject -Property @{
VIObj = $_.Name
FullPath = $objPath -join '/'
}
}
}
}