VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Putting Folder Heirarchy information in a PowerCLI report

Here is a script I'm currently using to report on my inventory. Is it possible to list not only the folder that a VM is in, but the folder on top of that folder? Or even Or all the folders on top of that folder?

Get-Cluster -Name clustername | Get-VM | select

Name,MemoryMB,NumCpu,Description,PowerState,ProvisionedSpaceGB,UsedSpaceGB,Version,VMHost,ResourcePool,

@{N="HDsizeKB";E={($_.Harddisks | Measure-Object -Property CapacityKB -Sum).Sum}},Folder,

@{N="Datastore";E={($_ | Get-Datastore).Name}},@{N="#Snapshots";E={($_ | Get-Snapshot).Count}},

@{N="OS Name";E={$_.Guest.OSFullName}} | Export-Csv "C:\report.csv" -NoTypeInformation

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$tgtEvents = "VmBeingDeployedEvent","VmCreatedEvent","VmRegisteredEvent","VmClonedEvent"
	
Get-Cluster -Name clustername | Get-VM | select Name,MemoryMB,NumCpu,Description,PowerState,
	ProvisionedSpaceGB,UsedSpaceGB,Version,VMHost,ResourcePool,
	@{N="HDsizeKB";E={($_.Harddisks | Measure-Object -Property CapacityKB -Sum).Sum}},
	@{N="Folder";E={
			$current = Get-View $_.Extensiondata.Parent
  		$path = $_.Name
  		do {
    		$parent = $current
	  		if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	  		if($current.parent){$current = Get-View $current.Parent}
  		} while ($current.Parent -ne $null)
  		$path}},
	@{N="Datastore";E={($_ | Get-Datastore).Name}},
	@{N="#Snapshots";E={($_ | Get-Snapshot).Count}},
	@{N="OS Name";E={$_.Guest.OSFullName}},
	@{N="CreatedTime";E={($_ | Get-VIEvent -Types Info | where {$tgtEvents -contains $_.Gettype().Name} | `
											Sort-Object -Property CreatedTime -Descending | Select -First 1).CreatedTime}} | `
	Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

An alternative would be to store the creation date in a Custom Attribute. As is documented in Alan's Who created that VM ? post.

Once you have the date in a custom attribute it's a lot easier (and faster) to include it in a report.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

To compose the complete folder path we need to follow the 'parent' trail.

The expression for the Folder property becomes a more complex scriptblock.

Try this

Get-Cluster -Name clustername | Get-VM | select Name,MemoryMB,NumCpu,Description,PowerState,
	ProvisionedSpaceGB,UsedSpaceGB,Version,VMHost,ResourcePool,
	@{N="HDsizeKB";E={($_.Harddisks | Measure-Object -Property CapacityKB -Sum).Sum}},
	@{N="Folder";E={
			$current = Get-View $_.Extensiondata.Parent
  		$path = $_.Name
  		do {
    		$parent = $current
	  		if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	  		if($current.parent){$current = Get-View $current.Parent}
  		} while ($current.Parent -ne $null)
  		$path}},
	@{N="Datastore";E={($_ | Get-Datastore).Name}},
	@{N="#Snapshots";E={($_ | Get-Snapshot).Count}},
	@{N="OS Name";E={$_.Guest.OSFullName}} | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

____________

Blog: LucD notes

Twitter: lucd22


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

junkbox101
Contributor
Contributor
Jump to solution

Thanks for this LucD.

Is it possible to add a VM creation date to an additional row within the final csv?

I know you can get these details from Get-VIEvent but not sure how to incorporate it into the same export (without duplicating the hostname field)

Any help with that would be greatly appreciated.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$tgtEvents = "VmBeingDeployedEvent","VmCreatedEvent","VmRegisteredEvent","VmClonedEvent"
	
Get-Cluster -Name clustername | Get-VM | select Name,MemoryMB,NumCpu,Description,PowerState,
	ProvisionedSpaceGB,UsedSpaceGB,Version,VMHost,ResourcePool,
	@{N="HDsizeKB";E={($_.Harddisks | Measure-Object -Property CapacityKB -Sum).Sum}},
	@{N="Folder";E={
			$current = Get-View $_.Extensiondata.Parent
  		$path = $_.Name
  		do {
    		$parent = $current
	  		if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	  		if($current.parent){$current = Get-View $current.Parent}
  		} while ($current.Parent -ne $null)
  		$path}},
	@{N="Datastore";E={($_ | Get-Datastore).Name}},
	@{N="#Snapshots";E={($_ | Get-Snapshot).Count}},
	@{N="OS Name";E={$_.Guest.OSFullName}},
	@{N="CreatedTime";E={($_ | Get-VIEvent -Types Info | where {$tgtEvents -contains $_.Gettype().Name} | `
											Sort-Object -Property CreatedTime -Descending | Select -First 1).CreatedTime}} | `
	Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

An alternative would be to store the creation date in a Custom Attribute. As is documented in Alan's Who created that VM ? post.

Once you have the date in a custom attribute it's a lot easier (and faster) to include it in a report.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
junkbox101
Contributor
Contributor
Jump to solution

Thanks.

It added the column and came back with some results but oddly enough a lot were blank and one was in a row all by itself.

I will have to play around with it some more to see if I can get it to output the results properly.

Not sure what is up.

I agree the alternative solution would be ideal.

Need to investigate that as well.

Thanks again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-VIEvent cmdlet returns 100 events if you don't use the -MaxSamples parameter or the -Start and -Finish parameters.

That would explain the ones with the empty CreatedDate property.

For the one row where you only had the CreatedDate field I have no explanation without looking at your environment I'm afraid.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
junkbox101
Contributor
Contributor
Jump to solution

OK Thanks.

I will give the -MaxSamples parameter a try.

0 Kudos
junkbox101
Contributor
Contributor
Jump to solution

-MaxSamples set at 9999 seemed to give me results for almost all of them.

Is there a Max value that you cannot go over for the -MaxSamples parameter?

If not, I guess the only downside is increased time for the script to run.

As for the extra date in an unpopulated row... that actually didn't happen.

The OS field is blank for some of them and it was for one of those.

Now on to troubleshoot why OS is not populating... but appears to be same under VM summary in VC.

So doesn't seem to be an issue with the script itself, but something with VC...

Weird.

But thanks again for the help and quick replies.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not that I know of.






____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
junkbox101
Contributor
Contributor
Jump to solution

One last question (hopefully)...

I modified this slightly to run against the whole DC vs. a specific cluster (by just starting with Get-VM instead of Get-Cluster).

But it doesn't appear that "Cluster" is an available option to select when using Get-VM and defining the items you want to view.

Any ideas on how to include a cluster column when running this against the entire DC?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this

$tgtEvents = "VmBeingDeployedEvent","VmCreatedEvent","VmRegisteredEvent","VmClonedEvent"
	
Get-VM | select Name,MemoryMB,NumCpu,Description,PowerState,
	ProvisionedSpaceGB,UsedSpaceGB,Version,VMHost,ResourcePool,
	@{N="HDsizeKB";E={($_.Harddisks | Measure-Object -Property CapacityKB -Sum).Sum}},
	@{N="Folder";E={
			$current = Get-View $_.Extensiondata.Parent
  		$path = $_.Name
  		do {
    		$parent = $current
	  		if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
	  		if($current.parent){$current = Get-View $current.Parent}
  		} while ($current.Parent -ne $null)
  		$path}},
	@{N="Datastore";E={($_ | Get-Datastore).Name}},	
        @{N="Cluster";E={($_ | Get-Cluster).Name}},
	@{N="#Snapshots";E={($_ | Get-Snapshot).Count}},
	@{N="OS Name";E={$_.Guest.OSFullName}},
	@{N="CreatedTime";E={($_ | Get-VIEvent -Types Info | where {$tgtEvents -contains $_.Gettype().Name} | `
											Sort-Object -Property CreatedTime -Descending | Select -First 1).CreatedTime}} | `
	Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

____________

Blog: LucD notes

Twitter: lucd22


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

junkbox101
Contributor
Contributor
Jump to solution

That did the trick.

Thanks again for all your help.

I am slowly beginning to learn all this stuff Smiley Happy

0 Kudos