Skip to content

Instantly share code, notes, and snippets.

@gowatana
Created October 21, 2020 11:15
Show Gist options
  • Save gowatana/e8c0d9166e3081c8039a6bacbc071ea7 to your computer and use it in GitHub Desktop.
Save gowatana/e8c0d9166e3081c8039a6bacbc071ea7 to your computer and use it in GitHub Desktop.
Print full-path of VM's folder and Resource Pool.
# Print full-path of VM's folder and Resource Pool.
# Created by: gowatana
# Usage:
# PS> Connect-VIServer
# PS> ./get_vm_path.ps1
# PS> ./get_vm_path.ps1 | ft -AutoSize
# PS> ./get_vm_path.ps1 | Export-Csv -Encoding UTF8 -NoTypeInformation -Path ./vm_path.csv
$vm_name = "*"
function get_rp_path($vm) {
$rp = $vm.ResourcePool
if($null -ne $rp){
while($null -ne $rp){
$rp_path = "/" + $rp.Name + $rp_path
$rp = $rp.Parent
}
}
$rp_path
}
function get_vmfolder_path($vm) {
$folder = $vm.Folder
if($null -ne $folder){
while($null -ne $folder){
$folder_path = "/" + $folder.Name + $folder_path
$folder = $folder.Parent
}
}
$folder_path
}
Get-VM -Name $vm_name -pv vm | select `
Name,
PowerState,
@{N="vCenter";E={$_.Uid -replace ".*@|:.*",""}},
@{N="MoRefId";E={$_.ExtensionData.MoRef.Value}},
VMHost,
@{N="VmFolder";E={get_vmfolder_path $vm}},
@{N="ResourcePool";E={get_rp_path $vm}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment