VMware Cloud Community
mburutzis
Contributor
Contributor
Jump to solution

VM info with folder

Hi all,

I am looking for a way to export to csv/xls all VM info:

  • Datacenter
  • Cluster
  • VM name
  • VM folder
  • VM disk size (provisionned) per disk
  • VM disk datastore location
  • If any RDM and size

Is this possible?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
25 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

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?

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

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

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

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.

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

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!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 Smiley Wink, 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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Thanks LucD.  I keep getting the following error:

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Scripts\vm-disk-info2.ps1:12 char:22
+         $t = Get-View <<<<  $t.Parent -Property Name,Parent
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Also, How do I get this into a csv?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Yes, there is some.  Then this would be the error and should be ignored?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Excellent, this works like a charm.

Thanks again LucD

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Is it possible to get the following info:

datastore, VM name, Host, total provisioned space, folder name

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

Did you want that included in the script above ?


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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Instead of the individual disks, yes... please

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Exactly what I was looking for... Thanks again RvdNieuwendijk

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

is there a way to use this script with an import of csv that has a list of servers in it?

0 Kudos