VMware Cloud Community
aryan14in
Enthusiast
Enthusiast

powercli script to get cluster name, datastore name, total size & free space on DS for a VM

Hi All,

need help to get  cluster name, datastore name, total size and available space on DS for a VM

I have referred below links

http://ict-freak.nl/2009/11/17/powercli-one-liner-to-get-vms-clusters-esx-hosts-and-datastores/comme...

+++++++++++++++++

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}} | `
Export-Csv -NoTypeInformation C:\Scripts\VM_CLuster_Host_Datastore.csv

+++++++++++++++++

I have modified above script as per requirement:

$vmname = Import-csv "C:\vcp-vm.csv"

foreach ($vm.name in $vmname)
{
Get-VM $vm.name | Select Name, @{N="Cluster";E={Get-Cluster -VM $vm.name}}, `
@{N="ESX Host";E={Get-VMHost -VM $vm.name}}, `
@{N="Datastore";E={Get-Datastore -VM $vm.name}} | `
Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv
}

could you plese suggest on adding datastore information

Thanks!

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership

Try this one

$vmnames = Import-csv "C:\vcp-vm.csv"
$vmnames | %{
    $vm = Get-VM $_.Name
    $ds = Get-Datastore -VM $vm
   
$vm | Select Name, @{N="Cluster";E={Get-Cluster -VM $vm.name}},
        @{N="ESX Host";E={(Get-VMHost -VM $vm.name).Name}},
      @{N="Datastore";E={$ds.Name}},         @{N="DS Capacity";E={$ds.CapacityMB}},         @{N="DS Free";E={$ds.FreeSpaceMB}} } | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

Note that the script doesn't take into account when a VM is using multiple datastores!


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

Reply
0 Kudos
aryan14in
Enthusiast
Enthusiast

Thanks! Luc. This is working as it was asked for..

As you mentioned in your note that the script doesn't take into account when a VM is using multiple datastores. Is it possible to include those details in script.

Reply
0 Kudos
LucD
Leadership
Leadership

The script is nearly the same except that it takes the $ds variable as input for the Select

$vmnames = Import-csv "C:\vcp-vm.csv" $vmnames | %{
    $vm = Get-VM $_.Name
    $ds = Get-Datastore -VM $vm    
    $ds
| Select @{N="Name";E={$vm.Name}},         @{N="Cluster";E={Get-Cluster -VM $vm.name}},         @{N="ESX Host";E={(Get-VMHost -VM $vm.name).Name}},         @{N="Datastore";E={$_.Name}},         @{N="DS Capacity";E={$_.CapacityMB}},         @{N="DS Free";E={$_.FreeSpaceMB}} } | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

Reply
0 Kudos
aryan14in
Enthusiast
Enthusiast

Superb! you are the best!

just one more thing, I tried modifying the same script to run on entire VC. I modified the script as below (even more), but having issues with it. could you please suggest on same as well.

    $vc = connect-viserver "VC name" 
    $vm = Get-VM
    $ds = Get-Datastore -VM $vm    
    $ds | Select @{N="Name";E={$vm}},         @{N="Cluster";E={Get-Cluster -VM $vm}},         @{N="ESX Host";E={(Get-VMHost -VM $vm).Name}},         @{N="Datastore";E={$_.Name}},         @{N="DS Capacity";E={$_.CapacityMB}},         @{N="DS Free";E={$_.FreeSpaceMB}}| Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

Easiest is to use a loop over all objects returned by Get-VM.

The script stores the entries in an array, $report, and at the end exports the array to the CSV file.

$vc = connect-viserver "VC name" 
$report = @()
foreach($vm in Get-VM){
    $ds = Get-Datastore -VM $vm 
    $report += $ds | Select @{N="Name";E={$vm}},
        @{N="Cluster";E={Get-Cluster -VM $vm}},
        @{N="ESX Host";E={(Get-VMHost -VM $vm).Name}},
        @{N="Datastore";E={$_.Name}},
        @{N="DS Capacity";E={$_.CapacityMB}},
        @{N="DS Free";E={$_.FreeSpaceMB}}
}
$report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv 


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

Reply
0 Kudos
aryan14in
Enthusiast
Enthusiast

Perfect! again, Thanks a lot..

Reply
0 Kudos
leaderone2
Contributor
Contributor

I found the script run very slow, I have 40 ESX host, and about 1000 vms. I run this script for about 40 minutes, Could it run more quickly? If I donot care the Performance consume on the vCenter or the client I run the scripts. Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

You can try switching to the Get-View cmdlet.

That is normally faster than the regular cmdlets.

$vmnames = Import-csv "C:\vcp-vm.csv" | %{"^$($_.Name)$"}

$vmnames = $vmnames -join '|'

$report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{

    New-Object PSObject -Property @{

        Name = $_.Name

        Cluster = &{

            $obj = Get-View -Id $_.Parent -Property Name,Parent

            while ($obj -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

                $obj = Get-View -Id $obj.Parent -property Name,Parent

            }

            $obj.Name

        }

        'ESX Host' = Get-View -Id $_.Runtime.Host -Property Name | %{$_.Name}

        Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'

        'DS Capacity MB' = $script:ds.Summary.Capacity/1MB

        'DS Free MB' = $script:ds.Summary.FreeSpace/1MB

    }

}

$report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

Reply
0 Kudos
leaderone2
Contributor
Contributor

Yeah! Very Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

You're welcome.

Out of curiosity, how long did this version run ?

Update: I changed the $ds variable to $script:ds


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

Reply
0 Kudos
leaderone2
Contributor
Contributor

Actually I need the script to get vmname,vmid,ESXHost,ESXIP,Cluster,Datastore in one line.

I re-test  the script today (last time I did not wait until it run to end, I just estimate the time). The scripts taks about 94minutes with the old script below:( our environment has about 40 ESX host, and about 1000 vms)

Get-VM | Select Name,id,@{N="ESXHost";E={(Get-VMHost -VM $_).name}}, `

@{N="ESXIp";E={(Get-VMHost | Get-VMHostNetwork | Select Hostname, ConsoleGateway, DNSAddress -ExpandProperty ConsoleNic | Select  IP).ip}}, `

@{N="Cluster";E={Get-Cluster -VM $_}}, `

@{N="Datastore";E={Get-Datastore -VM $_}},`

Time | Export-Csv -NoTypeInformation c:\VMInfo\"Export67VMinfo$(Get-Date -Format 'yyyyMMdd').csv"


"

Your  new script diid not the same column with above, and the cluster sames get the folder info, so I modify like below (I am a younger with PowerCLI),but it still cannot get the same column to my need, I will trying to modify it tomorrow  . No matter how, the below script is quick than above :

$vmnames = get-vm | %{"^$($_.Name)$"}

$vmnames = $vmnames -join '|'

$report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{

    New-Object PSObject -Property @{

        Name = $_.Name

        Cluster = get-cluster -vm $_.name

        'ESX Host' = Get-View -Id $_.Runtime.Host -Property Name | %{$_.Name}

        Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'

   }

}

$rert | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

I will have a test to retrieve  vmname,vmid,ESXHost,ESXIP,Cluster,Datastore ,with your method, if I have result about this ,I will test how long it take , and put the result here!

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

Except for the IP address of the ESXi node, everything is there.

Or what exactly do you mean with "did not same column" ?


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

Reply
0 Kudos
leaderone2
Contributor
Contributor

I mean the script you provide donot have "vmid","esx IP" column, anyhow ,your script's run time is 45minutes, save half the time than before!

I have another question. Below is my script

Get-VM | Select Name,id, `

@{Name=’ESXIP’;Expression={%{$_.VMHost} | Select ($_.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}},`

vmhost, `

@{Name=’Cluster’;Expression={$_.VMHost.Parent}}, `

@{N="Storage";E={Get-HardDisk -VM $_ |Select @{N='HD';E={$_.Name}},@{N='Datastore';E={($_.Filename.Split(']')[0]).TrimStart('[')}}}},`

Time | Export-Csv -NoTypeInformation -encoding utf8 c:\VMInfo\"Export67VMinfo$(Get-Date -Format 'yyyyMMdd').csv"

I want to get the vm's HardDisk and Datastore information. In our Environment, some vms  have more than one HardDisks    and the vm's HardDisks maybe not have the same Datastore.

When I use the script to Export to csv file, Some vm's Storage data are missing ,These vms all have more than one HardDisk. I think maybe "storage" column is too long or maybe this column have comma symbol? But it is normal in powercli as show below!

小Q截图-20160106211427.png

I need to export this to csv file, because I want to use diff command to check line matches ,could you  help me on this .Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

Try this extended version.

I added the missing columns, and all Datastores where there is vDisk, will be in one string, separated by the '|' character

$vmnames = Import-csv "C:\vcp-vm.csv" | %{"^$($_.Name)$"}

$vmnames = $vmnames -join '|'

$report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{

    New-Object PSObject -Property @{

        Name = $_.Name

        VMId = $_.MoRef

        Cluster = &{

            $obj = Get-View -Id $_.Parent -Property Name,Parent

            while ($obj -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

                $obj = Get-View -Id $obj.Parent -property Name,Parent

            }

            $obj.Name

        }

        'ESX Host' = $script:esx = Get-View -Id $_.Runtime.Host -Property Name,Config; $esx.Name

        'ESX IP' = ($script.esx.Config.Network.Vnic | where{$_.Device -eq 'vmk0'}).Spec.Ip.IpAddress

        Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'

        'DS Capacity MB' = $script:ds.Summary.Capacity/1MB

        'DS Free MB' = $script:ds.Summary.FreeSpace/1MB

        HD = &{

            $ds = $_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk} | %{

                $_.Backing.Datastore

            }

            [string]::Join((Get-View -Id $ds -Property Name | select -ExpandProperty Name),'\')

                 }

    }

}

$report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

Reply
0 Kudos
leaderone2
Contributor
Contributor

When I run the script,it  cannot get the HardDisk information, May be the parameter of "-ExpandProperty" is wrong, ? Could you help me ? Very thanks!

小Q截图-20160107112634.png

Reply
0 Kudos