Hi all,
I am looking for a way to export to csv/xls all VM info:
Is this possible?
Since the Select-Object is in a Foreach loop, you'll have to do this a bit different.
Store the results in a variable and then pipe that variable to the CSV file.
$report = foreach($vm in Get-VM){ $vm.HardDisks | Select @{N="VM";E={$vm.Name}}, @{N="Folder";E={$vm.Folder.Name}}, @{N="Cluster";E={(Get-Cluster -VM $vm).Name}}, @{N="Datacenter";E={(Get-Datacenter -VM $vm).Name}}, @{N="Disk";E={$_.Name}}, @{N="Disk datastore";E={$_.Filename.Split(']')[0].TrimStart('[')}}, @{N="Disk capacity";E={$_.CapacityKB}}, @{N="Disk type";E={$_.DiskType}} }
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try it like this
foreach($vm in Get-VM){ $vm.HardDisks | Select @{N="VM";E={$vm.Name}}, @{N="Folder";E={$vm.Folder.Name}}, @{N="Cluster";E={(Get-Cluster -VM $vm).Name}}, @{N="Datacenter";E={(Get-Datacenter -VM $vm).Name}}, @{N="Disk";E={$_.Name}}, @{N="Disk datastore";E={$_.Filename.Split(']')[0].TrimStart('[')}}, @{N="Disk capacity";E={$_.CapacityKB}}, @{N="Disk type";E={$_.DiskType}} }
If you have a larger environment, then it will be better to go for a Get-View solution.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well, I have over 800 vms. I would need to export this into a csv. If I add export-csv at the end, will it work?
Ugh LucD, you are too good.
I like how you did the .harddisks first and then used it in the pipeline later on....Super Slick
Since the Select-Object is in a Foreach loop, you'll have to do this a bit different.
Store the results in a variable and then pipe that variable to the CSV file.
$report = foreach($vm in Get-VM){ $vm.HardDisks | Select @{N="VM";E={$vm.Name}}, @{N="Folder";E={$vm.Folder.Name}}, @{N="Cluster";E={(Get-Cluster -VM $vm).Name}}, @{N="Datacenter";E={(Get-Datacenter -VM $vm).Name}}, @{N="Disk";E={$_.Name}}, @{N="Disk datastore";E={$_.Filename.Split(']')[0].TrimStart('[')}}, @{N="Disk capacity";E={$_.CapacityKB}}, @{N="Disk type";E={$_.DiskType}} }
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
wow, this is taking some time... still runing. I started it about an hour and a half ago. Will let you know if its working.
Excellent. This worked great. The only issue I had was the time it took and does not give the full path for the VM Folder.
Thanks LucD. You rock!
Ok, this is the faster version based on the Get-View cmdlet.
The code looks a bit more complex but it should be faster.
And it should give the full folder path
Update: must be getting late , this should be faster
foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Parent,ResourcePool,Config.Hardware.Device)){ $t = Get-View $vm.Parent -Property Name,Parent $path = $t.Name while($t.GetType().Name -eq "Folder"){ $t = Get-View $t.Parent if($t.Name -ne "vm"){ $path = $t.Name + "/" + $path
} } $t = Get-View $vm.ResourcePool -Property Name,Parent while($t.getType().Name -ne "ClusterComputeResource"){ $t = Get-View $t.Parent -Property Name,Parent } $cluster = $t.Name while($t.getType().Name -ne "Datacenter"){ $t = Get-View $t.Parent -Property Name,Parent } $datacenter = $t.Name $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | Select @{N="VM";E={$vm.Name}}, @{N="Folder";E={$path}}, @{N="Cluster";E={$cluster}}, @{N="Datacenter";E={$datacenter}}, @{N="Disk";E={$_.DeviceInfo.Label}}, @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}}, @{N="Disk capacity";E={$_.CapacityInKB}}, @{N="Disk type";E={ if($_.Backing.GetType().Name -match "flat"){ "Flat" } else{ $_.Backing.CompatibilityMode }}} }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD. I keep getting the following error:
Also, How do I get this into a csv?
The same as the previous one
$report = foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Parent,ResourcePool,Config.Hardware.Device)){ ... $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you perhaps have VMs that are not in a cluster but directly created under an ESX(i) host ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, there is some. Then this would be the error and should be ignored?
No, try this updated version
foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Parent,ResourcePool,Config.Hardware.Device)){ $t = Get-View $vm.Parent -Property Name,Parent $path = $t.Name while($t.GetType().Name -eq "Folder"){ $t = Get-View $t.Parent if($t.Name -ne "vm"){ $path = $t.Name + "/" + $path
} } $t = Get-View $vm.ResourcePool -Property Name,Parent while($t.getType().Name -eq "ResourcePool"){ $t = Get-View $t.Parent -Property Name,Parent } if($t.GetType().Name -eq "ClusterComputeResource"){ $cluster = $t.Name } else{ $cluster = "na"
} while($t.getType().Name -ne "Datacenter"){ $t = Get-View $t.Parent -Property Name,Parent } $datacenter = $t.Name $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | Select @{N="VM";E={$vm.Name}}, @{N="Folder";E={$path}}, @{N="Cluster";E={$cluster}}, @{N="Datacenter";E={$datacenter}}, @{N="Disk";E={$_.DeviceInfo.Label}}, @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}}, @{N="Disk capacity";E={$_.CapacityInKB}}, @{N="Disk type";E={ if($_.Backing.GetType().Name -match "flat"){ "Flat"
} else{ $_.Backing.CompatibilityMode }}} }
The output will say "na" when the VM doesn't belong to a cluster
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Excellent, this works like a charm.
Thanks again LucD
Is it possible to get the following info:
datastore, VM name, Host, total provisioned space, folder name
You can get the requested info with:
Get-VM | Select-Object -Property @{N="Datastore";E={[string]::Join(",",($_ | Get-Datastore))}},@{N="VM";E={$_.Name}}, ProvisionedSpaceGB,VMHost,@{N="Folder";E={$_.Folder.Name}}
Regards, Robert
Did you want that included in the script above ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Instead of the individual disks, yes... please
Exactly what I was looking for... Thanks again RvdNieuwendijk
is there a way to use this script with an import of csv that has a list of servers in it?