vwmagic
Enthusiast
Enthusiast

Seeking POWECLI to get all VM's and used space inside a number of Datastores

Jump to solution

I have X number of Datastores, wanted to list all VM names along with every VM's used space inside each one of datastore. 

Can you please help  me out? Thanks in advance!

0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership

Sure

$dsName = Get-Content -Path .\ds.list

Get-VM -PipelineVariable vm |
ForEach-Object -Process {
  $vm.ExtensionData.Storage.PerDatastoreUsage |
  ForEach-Object -Process {
    $ds = Get-View -Id $_.Datastore -Property Name
    if ($dsnames -contains $ds.Name) {
      [PSCustomObject]@{
        Datastore = $ds.Name
        VM = $vm.Name
        StorageUsed = [math]::Round($_.Committed / 1GB, 1)
        StorageAssigned = [math]::Round($_.Uncommitted / 1GB, 1)
      }
    }
  }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
LucD
Leadership
Leadership

What is the content of that file?
Something like this and nothing else in there?

DS1
DS2
DS3

 


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership

Try something ike this

Get-VM -PipelineVariable vm |
ForEach-Object -Process {
  $vm.ExtensionData.Storage.PerDatastoreUsage |
  ForEach-Object -Process {
    [PSCustomObject]@{
      Datastore = (Get-View -Id $_.Datastore -Property Name).Name
      VM = $vm.Name
      StorageUsed = [math]::Round($_.Committed/1GB,1)
      StorageAssigned = [math]::Round($_.Uncommitted/1GB,1)
    }
  }
}

If you only want to report on a specific set of datastores you could do

$dsNames = 'DS1', 'DS2', 'DS3'

Get-VM -PipelineVariable vm |
ForEach-Object -Process {
  $vm.ExtensionData.Storage.PerDatastoreUsage |
  ForEach-Object -Process {
    $ds = Get-View -Id $_.Datastore -Property Name
    if ($dsnames -contains $ds.Name) {
      [PSCustomObject]@{
        Datastore = $ds.Name
        VM = $vm.Name
        StorageUsed = [math]::Round($_.Committed / 1GB, 1)
        StorageAssigned = [math]::Round($_.Uncommitted / 1GB, 1)
      }
    }
  }
}


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

Thanks!

Instead of using the following:

$dsNames = 'DS1', 'DS2', 'DS3'

 

I have a file "ds.list", containing all names like below:
ds1

ds1

ds3

...

ds50

Can you please use "ds.list" as the input file to this code?

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Is that a normal .txt file, with one datastorename per line?

If yes, use

$dsName = Get-Content -Path .\ds.list


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

one more last thing. 

 

Could you please help to put all outputs to an excel file instead of the screen?

 

Thank you!

0 Kudos
LucD
Leadership
Leadership

Sure

$dsName = Get-Content -Path .\ds.list

Get-VM -PipelineVariable vm |
ForEach-Object -Process {
  $vm.ExtensionData.Storage.PerDatastoreUsage |
  ForEach-Object -Process {
    $ds = Get-View -Id $_.Datastore -Property Name
    if ($dsnames -contains $ds.Name) {
      [PSCustomObject]@{
        Datastore = $ds.Name
        VM = $vm.Name
        StorageUsed = [math]::Round($_.Committed / 1GB, 1)
        StorageAssigned = [math]::Round($_.Uncommitted / 1GB, 1)
      }
    }
  }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

One more thing just found.

 

The output of "StorageAssigned" is quite different than those values I checked from vSphere client GUI, where as there is column called "Provisioned Space". "StorageUsed" matches "usedspace" there.

Are these two attributes are the same, or any way you can output "provisioned space"?

 

Thanks!

0 Kudos
LucD
Leadership
Leadership

Oops, seems I forgot to add the Unshared space

StorageAssigned = [math]::Round(($_.Uncommitted + $_.Unshared) / 1GB, 1)

 


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

I am looking for "provisioned space" as the one shown in vSphere client under the datastore. 

In your script, "storageassigned" (uncommitted) + "storageused"(committed) looks equivalent to "provisioned space", can you please add these two values together, and use the column name as "provisioned space"?

Thank you!

0 Kudos
LucD
Leadership
Leadership

If you look under Datastore and then the line for the VM, that is view from the VM.
In other words the Provisioned Space number is the total space used by that VM on all datastores.
Your original question was for the storage per datastore for each VM


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

I am sorry, but if I changed the input to a file instead of a variable, the code doesn't work as shown below. In the end, it will generate a empty C:\tmp\List_VMs.csv. 

$dsNames = Get-Content -Path C:\tmp\ds.txt

echo $dsNames                                    # it contains all DS's
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
$vm.ExtensionData.Storage.PerDatastoreUsage |
ForEach-Object -Process {
$ds = Get-View -Id $_.Datastore -Property Name
if ($dsNames -contains $ds.Name) {
[PSCustomObject]@{
Datastore = $ds.Name
VM = $vm.Name
StorageUsed = [math]::Round($_.Committed / 1GB, 1)
StorageAssigned = [math]::Round($_.Uncommitted / 1GB, 1)
}
}
}
} | Export-Csv -Path "C:\tmp\List_VMs.csv" -NoTypeInformation -UseCulture

 

If I changed the first line to $dsNames = 'DS1', 'DS2', 'DS3', then it worked. 

 

Any idea?

0 Kudos
LucD
Leadership
Leadership

What is the content of that file?
Something like this and nothing else in there?

DS1
DS2
DS3

 


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

I am all good now. It was due to trailing characters in the end of each line. 

Thank you!

0 Kudos