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
25 Replies
CRad14
Hot Shot
Hot Shot
Jump to solution

Yes, but if it is only a list of servers, you would really only need a txt file with each one on a new line

$servers=Get-Content ./serverlist.txt

FOREACH ($server in $servers)

{

Connect-viserver $server

Get-VM | Select-Object -Property @{N="Datastore";E={[string]::Join(",",($_ | Get-Datastore))}},@{N="VM";E={$_.Name}},
ProvisionedSpaceGB,VMHost,@{N="Folder";E={$_.Folder.Name}}

Disconnect-viserver $server -confirm:$false

}

Atleast assuming I understand your request I think this is what you are looking for....


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

LucD, I tried running the following script:

$report = 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
            }}}
}
$report | Export-Csv ("C:\PS-Logs\VM-Disks-{0:yyyyMMdd}.csv" -f (get-date)) -NoTypeInformation -UseCulture

and it takes forever and ends up using 3.5 GB of RAM. I have to kill the process as it runs for hours and hours...  Am I doing something wrong?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Afaics you aren't doing anything wrong.

But perhaps you are running the script against a huge environment ?

How many VMs are we talking about ?


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

0 Kudos
mburutzis
Contributor
Contributor
Jump to solution

Yes, it is pretty huge:

  • 100 hosts
  • 800 VMs
  • 20 Clusters
  • 8 Datacenters
0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

To make it go faster or just confirm it works the way you want it to.. You could just change the first line slightly

$report = foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Parent,ResourcePool,Config.Hardware.Device){

to

$report= foreach($vm in (get-cluster "ClusterName" | Get-VM)){

Then it will just run it for the one cluster

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

When I run it through powerCLI, I get the following:

Unexpected token 'in' in expression or statement. At D:\Scripts\vmdk-info.ps1:1 char:25 + $report = foreach($vm in( <<<< get-cluster "BNC Viger Blade - Cluster 1" | Get-VM)){

Any idea why it is doing this?  If I run it from my system, I have no problem (I run it through PowerGui Script Editor on my system)

0 Kudos