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
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
This worked perfectly, thanks so much.