VMware Cloud Community
lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

vSAN policy in different datastores

I have just identified a problem in a large environment and I need to know the best way to address it.

I identified virtual machines on different datastore, vSAN and datastore on storage bays.

The problem is that someone applied the "vSAN Default Storage Policy" to all virtual machines.

I made this little script to help me find the details of the VMDK of each virtual machine and I would like to add the following information:

  • VM Storage Policies
  • VM Storage Policy Compliance
  • DataStore Type

Is that possible, or do I have to do it differently?

Script:

$report = @()
foreach ($vm in Get-VM){
$view = Get-View $vm
if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true){
$row = '' | select Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin
$row.Name = $vm.Name
$row.Provisioned = [math]::round($vm.ProvisionedSpaceGB , 2)
$row.Total = [math]::round(($view.config.hardware.Device | Measure-Object CapacityInKB -Sum).sum/1048576 , 2)
$row.Used = [math]::round($vm.UsedSpaceGB , 2)
$row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
$row.VMDKsize = $view.config.hardware.Device | where {$_.GetType().name -eq 'VirtualDisk'} | ForEach-Object {($_.capacityinKB)/1048576} | Out-String
$row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object {[math]::round( ($_.Capacity - $_.FreeSpace)/1048576/1024, 2 )} | Out-String
$row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
$report += $row
}}
$report | Sort Name | Export-Csv -Path "D:Thin_Disks.csv"

Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with

    $row.StoragePolicy = (Get-Harddisk -VM $vm | Get-SpbmEntityConfiguration).StoragePolicy.Name | %{if($_ -eq $null){'Datastore Default'}else{$_}} | Out-String

Don't forget to add StoragePolicy on the line where you define $row.

    $row = '' | Select-Object Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin, StoragePolicy


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

View solution in original post

LucD
Leadership
Leadership
Jump to solution

That should be like this

$report = @()
foreach ($vm in Get-VM) {
  $view = Get-View $vm
  if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true) {
    $row = '' | Select-Object Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin, StoragePolicy
    $row.Name = $vm.Name
    $row.Provisioned = [math]::round($vm.ProvisionedSpaceGB , 2)
    $row.Total = [math]::round(($view.config.hardware.Device | Measure-Object CapacityInKB -Sum).sum / 1048576 , 2)
    $row.Used = "{0:N2}GB" -f $vm.UsedSpaceGB
    $row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
    $row.VMDKsize = $view.config.hardware.Device | Where-Object { $_.GetType().name -eq 'VirtualDisk' } | ForEach-Object { "{0:N2}GB" -f (($_.capacityinKB) / 1MB) } | Out-String
    $row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object { "{0:N2}GB" -f (($_.Capacity - $_.FreeSpace) / 1GB) } | Out-String
    $row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
    $row.StoragePolicy = (Get-Harddisk -VM $vm | Get-SpbmEntityConfiguration).StoragePolicy.Name | %{if($_ -eq $null){'Datastore Default'}else{$_}} | Out-String
    $report += $row
  }
}
$report | Sort-Object Name | Export-Csv -Path "D:Thin_Disks.csv"


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Try with

    $row.StoragePolicy = (Get-Harddisk -VM $vm | Get-SpbmEntityConfiguration).StoragePolicy.Name | %{if($_ -eq $null){'Datastore Default'}else{$_}} | Out-String

Don't forget to add StoragePolicy on the line where you define $row.

    $row = '' | Select-Object Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin, StoragePolicy


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

lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

@LucD  Thank you it's working fine 🙂

 

would you please help me with sources or some example in order to show output in GB or Go Like this :

20 Go or GB
30 Go or GB

As for now the output is like this

lElOUCHE_79_0-1675184909578.png

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$row.VMDKsize = $view.config.hardware.Device | where {$_.GetType().name -eq 'VirtualDisk'} | ForEach-Object {"{0:N2}GB" -f (($_.capacityinKB)/1MB)} | Out-String

Similar for Used and DiskUsed


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

Reply
0 Kudos
lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

Like this I'm getting error

$row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object {[math]::round( "{0:N2} GB" -f ($_.Capacity - $_.FreeSpace)/1048576/1024, 2 )} | Out-String
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be like this

$report = @()
foreach ($vm in Get-VM) {
  $view = Get-View $vm
  if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true) {
    $row = '' | Select-Object Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin, StoragePolicy
    $row.Name = $vm.Name
    $row.Provisioned = [math]::round($vm.ProvisionedSpaceGB , 2)
    $row.Total = [math]::round(($view.config.hardware.Device | Measure-Object CapacityInKB -Sum).sum / 1048576 , 2)
    $row.Used = "{0:N2}GB" -f $vm.UsedSpaceGB
    $row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
    $row.VMDKsize = $view.config.hardware.Device | Where-Object { $_.GetType().name -eq 'VirtualDisk' } | ForEach-Object { "{0:N2}GB" -f (($_.capacityinKB) / 1MB) } | Out-String
    $row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object { "{0:N2}GB" -f (($_.Capacity - $_.FreeSpace) / 1GB) } | Out-String
    $row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
    $row.StoragePolicy = (Get-Harddisk -VM $vm | Get-SpbmEntityConfiguration).StoragePolicy.Name | %{if($_ -eq $null){'Datastore Default'}else{$_}} | Out-String
    $report += $row
  }
}
$report | Sort-Object Name | Export-Csv -Path "D:Thin_Disks.csv"


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