sp93
Contributor
Contributor

How to add datastore name to nested report

I currently have the following export which shows VMname, VMDisksize, ESX server, and resource pool for all VMDK files. I now wish to add the datastore name.

Can anyone help:

$report = @()

$vms = Get-VM

foreach ($vm in $vms){

foreach($hd in $vm | Get-HardDisk){

$row = "" | select VMname, VMdisksize, server, respool

$row.VMname = $vm.Name

$row.VMdisksize = $hd.CapacityKB

$row.server = ($vm | Get-VMHost).Name

$rpImpl = $vm | Get-ResourcePool

$rp = Get-View -id ($vm | Get-ResourcePool).Id

$nested = ""

while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){

$nested = "/" + $rp.Name + "/" + $nested

$rp = Get-View $rp.Parent

}

$row.respool = $nested.TrimEnd("/")

$report += $row

}

}

$report | Export-Csv "C:\test.csv" -noTypeInformation

0 Kudos
2 Replies
LucD
Leadership
Leadership

I think this already came up in the thread:

In any case this should do the trick

$report = @()

$vms = Get-VM
foreach ($vm in $vms){
	foreach($hd in $vm | Get-HardDisk){
		$row = "" | select VMname, VMdisksize, server, respool, datastore
		$row.VMname = $vm.Name
		$row.VMdisksize = $hd.CapacityKB
		$row.server = ($vm | Get-VMHost).Name
		$row.datastore = ($hd.Filename).Substring(0, ($hd.Filename).IndexOf("]") + 1)
		$rpImpl = $vm | Get-ResourcePool
		$rp = Get-View -id ($vm | Get-ResourcePool).Id
		$nested = ""
		while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){
			$nested = "/" + $rp.Name + "/" + $nested
			$rp = Get-View $rp.Parent
		}
		$row.respool = $nested.TrimEnd("/")
		$report += $row
	}
}

$report | Export-Csv "C:\test.csv" -noTypeInformation


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

0 Kudos
sp93
Contributor
Contributor

This worked perfectly, thanks so much.

0 Kudos