VMware Cloud Community
vmhyperv
Contributor
Contributor
Jump to solution

VM Inventory script for VM Name with location,Cluster Name and Data store total size and Free space left in Datastore.

Hi,
i am looking for slight modification in script. I want the Output for Data store  in GB rather than MB.Any help on this will appreciated.

$vc = connect-viserver "eg 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

thanks

vmguys

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Use the PowerShell builtin constant 1KB

$vc = connect-viserver "eg 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 GB";E={$_.CapacityMB/1KB}},         @{N="DS Free GB";E={$_.FreeSpaceMB/1KB}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

And if you want control the format of the numbers a bit, use the Round method

$vc = connect-viserver "eg 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 GB";E={[Math]::Round($_.CapacityMB/1KB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

View solution in original post

0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

Use the PowerShell builtin constant 1KB

$vc = connect-viserver "eg 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 GB";E={$_.CapacityMB/1KB}},         @{N="DS Free GB";E={$_.FreeSpaceMB/1KB}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

And if you want control the format of the numbers a bit, use the Round method

$vc = connect-viserver "eg 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 GB";E={[Math]::Round($_.CapacityMB/1KB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,
    I want to add one more column in this.% of free space left in Data store.Is it Possible.

Thanks
vmguys

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, you can use the -format operator with the percent format string.

$vc = connect-viserver "eg 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 GB";E={[Math]::Round($_.CapacityMB/1KB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}},         @{N="DS Free Space %";E={"{0:p1}" -f ($_.FreeSpaceMB/$_.CapacityMB)}}
} $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  It worked perfectly.Thanks  you are awsome.

thanks

vmguys

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  one more thing i would like to add in this script.As i have more than 3000 vms and i want to pull out for specific 500 vms from csv. i.e import from csv.

Is it possible

thanks

vmguy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, suppose your CSV file looks like this

Name

vm1

vm2

vm3

Then you can do something like this

$vc = connect-viserver "eg VC name" 
$names
= Import-Csv "C:\vmnames.csv" -UseCulture | %{$_.Name} $report = @() foreach($vm in (Get-VM -Name $names)){     $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 GB";E={[Math]::Round($_.CapacityMB/1KB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}},         @{N="DS Free Space %";E={"{0:p1}" -f ($_.FreeSpaceMB/$_.CapacityMB)}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv


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

vmhyperv
Contributor
Contributor
Jump to solution

Thanks LucD,

I will check and let you know.

vmguy

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

i executed the Script  with FQDN abd without FQDN  but its throws error.

Get-VM : 10/31/2011 5:22:23 PM    Get-VM        VM with name 'AATLKTYCOTSR001.aam.XXXXXXXXXXX.com' was not found, using the specified filter(s).
At E:\script\VM-DS-Cluster.ps1:8 char:23
+ foreach($vm in (Get-VM <<<<  -Name $names)){
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

thanks

vmguys

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So you are not using the VM name but the hostname of the OS running in the VM ?

If you have the VMware Tools installed on all your VMs, then you could do the following to select the VMs based on their hostname.

$vc = connect-viserver "eg VC name" 
$names
= Import-Csv "C:\vmnames.csv" -UseCulture | %{$_.Name} $report = @() foreach($vm in (Get-VM | where {$names -contains $_.Guest.Hostname})){     $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 GB";E={[Math]::Round($_.CapacityMB/1KB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}},         @{N="DS Free Space %";E={"{0:p1}" -f ($_.FreeSpaceMB/$_.CapacityMB)}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

But then your CSV should look something like this

Name

AATLKTYCOTSR001.aam.XXXXXXXXXXX.com

...


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  After executing the script its generating blank out in csv format after a couple of hours.

thanks

vmguy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure you are actually specifying hostnames or DNSnames in the input CSV file.

Are these the names returned by in the DNSname column ?

Get-VM | Select Name,@{N="DNSname";E={$_.Guest.HostName}}


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  i am using the DNS name.Here is the output for above command

PowerCLI C:\> Get-VM UASTPA3TKSWO193 | Select Name,@{N="DNSname";E={$_.Guest.HostName}}

Name                                                                       DNSname
----                                                                       -------
UASTPA3TKSWO193                                                            UASTPA3TKSWO193.us.aam.ad.abcinternal.com

so where we need to make change in the script.

thanks

vmguy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then the script should work.

The other reason could be that you do not have the VMware Tools installed on one or more of the VMs.

In that case the HostName property could be $null and hence the test in the script would fail.

You can check with

Get-VM | Select Name,@{N="Tools Status";E={$_.ExtensionData.Guest.ToolsStatus}}

It should say something like "toolsOK".


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

   Thanks its worked once you suggested for DNS name but its take too much for pulling out report for 5  vms.what about if i

am going to pull info for 500 vms. One more request is it possible to  pull out  for  the vm total space provisioned for each disk  and free space left of each disk.E.g C: 30 gb free 10 gb ,D :50gb)

thanks

vmguys

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, try the fast version.

$vc = connect-viserver "eg VC name" 

$report = @()
Import-Csv "C:\vmnames.csv" -UseCulture | %{
    $vm = Get-View -ViewType VirtualMachine -Property Name,Guest.HostName,Runtime.Host,Datastore -Filter @{"Guest.HostName"=$_.Name}
    $esx = Get-View $vm.Runtime.Host -Property Name,Parent
    $cluster = Get-View $esx.Parent -Property Name
    $report +=
Get-View $vm.Datastore -Property Name,Summary |         Select @{N="Name";E={$vm.Name}},         @{N="Cluster";E={$cluster.Name}},         @{N="ESX Host";E={$esx.Name}},         @{N="Datastore";E={$_.Name}},         @{N="DS Capacity GB";E={[Math]::Round($_.Summary.Capacity/1GB,2)}},         @{N="DS Free GB";E={[Math]::Round($_.Summary.FreeSpace/1GB,2)}},         @{N="DS Free Space %";E={"{0:p1}" -f ($_.Summary.FreeSpace/$_.Summary.Capacity)}} } $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

Mapping the vDisk to the vPartitions is not obvious.

See the discussion in the List only virtual disks and their provisioned space thread.


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

   yes the script is executing  fast but the output is generating on console only rather than in csv  and csv output is blank with 0 bytes.

thanks

vmguy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That was a typo, sorry about that.

The script above has been corrected.


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  Now different errors throws

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 E:\script\VM-DS-Cluster.ps1:7 char:20
+     $esx = Get-View <<<<  $vm.Runtime.Host -Property Name,Parent
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

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 E:\script\VM-DS-Cluster.ps1:9 char:24
+     $report += Get-View <<<<  $vm.Datastore -Property Name,Summary |
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

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 E:\script\VM-DS-Cluster.ps1:7 char:20
+     $esx = Get-View <<<<  $vm.Runtime.Host -Property Name,Parent
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

thanks

vmguys

0 Kudos