VMware Cloud Community
Roey1206
Contributor
Contributor
Jump to solution

Get folder of VIrtual Machine Using VI Toolkit

Hi all!

i tried a lot but i cant find a way to find out in witch folder in the "Virtual Machines & Templates" the vm...

i try to get the all hierarchy folders...

for example in the photo that i add...

under "Templates\Linux"...

hope i make myself clear....

thanks!!!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This script reports all VMs and the path to the "blue" folder they are in

$report = @()
Get-VM | Get-View | %{
  $row = "" | select Name, Path
  $row.Name = $_.Name

  $current = Get-View $_.Parent
  $path = $_.Name
  do {
    $parent = $current
	if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	$current = Get-View $current.Parent
  } while ($current.Parent -ne $null)
  $row.Path = $path
  $report += $row
}
$report

The script can easily be converted to a filter, this will allow you to find the folder path for one or more specific VMs.

The filter looks like this

filter Get-FolderPath {
	$_ | Get-View | % {
		$row = "" | select Name, Path
		$row.Name = $_.Name

		$current = Get-View $_.Parent
		$path = $_.Name
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row.Path = $path
		$row
	}
}

With the filter you can now use it like this

Get-VM PC* | Get-FolderPath


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

This script reports all VMs and the path to the "blue" folder they are in

$report = @()
Get-VM | Get-View | %{
  $row = "" | select Name, Path
  $row.Name = $_.Name

  $current = Get-View $_.Parent
  $path = $_.Name
  do {
    $parent = $current
	if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	$current = Get-View $current.Parent
  } while ($current.Parent -ne $null)
  $row.Path = $path
  $report += $row
}
$report

The script can easily be converted to a filter, this will allow you to find the folder path for one or more specific VMs.

The filter looks like this

filter Get-FolderPath {
	$_ | Get-View | % {
		$row = "" | select Name, Path
		$row.Name = $_.Name

		$current = Get-View $_.Parent
		$path = $_.Name
		do {
			$parent = $current
			if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
			$current = Get-View $current.Parent
		} while ($current.Parent -ne $null)
		$row.Path = $path
		$row
	}
}

With the filter you can now use it like this

Get-VM PC* | Get-FolderPath


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Roey1206
Contributor
Contributor
Jump to solution

Working good!

Thanks again!

0 Kudos