PowerShell scripts to help you out with detailed disk/storage calculations.

Please use all scripts published by me with caution. The scripts have been tested in my environment and are functional, but I do not take responsibility for how they may behave in your environment.

Scirpt to calculate disk number:

$vmsall = get-vm | where {$_.powerstate -like "PoweredOn"}. ## for all VMs defined here
$vmdisks = @()
foreach ($vv in $vmsall){
  $diskforvm = New-Object -TypeName psobject
  $diskforvm | Add-Member -MemberType NoteProperty -name "name"-value $vv.name
  $diskforvm | Add-Member -MemberType NoteProperty -name "disk_number" -value (($vv | get-view).layoutex.disk.key).count
  $vmdisks += $diskforvm
}
$vmdisks | Sort-Object -Property disk_number

Script to check various disk parameters:

#vms contains the list of VMs that we want to work on 

foreach ($vm in $vms){
  Write-Host "checking:" + $vm
  if (-not (get-vm $vm)){
    $vm | out-file -Append desktop\disk-size-scope-21062023-notfound.txt}
  else{
    get-vm $vm | select name, @{n="UsedSpaceGB"; e={[Math]::Ceiling($_.UsedSpaceGB)}},@{n="ProvisionedSpaceGB"; e={[Math]::Ceiling($_.ProvisionedSpaceGB)}}, @{n="DiskSize"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}, @{n="RealUsage"; e={[Math]::Ceiling(((($_ | get-view).guest.disk.Capacity | Measure-Object -Sum).sum / 1GB) - ((($_ | get-view).guest.disk.freespace | measure-object -sum).sum / 1GB))}}, memorygb, @{n="ClusterName"; e={(Get-Cluster -vm $_).name}}, @{n="CPU"; e={cpunum}} | Export-Csv -Append -Path desktop\disk-size-scope-21062023.csv }
 }

Script to check Physical or Virtual (Bus Sharing Mode) – indicating RDM:

#vms contains the list of VMs that we want to work on 

$array = @()
foreach ($vm in $vms){
     $vm = get-vm $vm
     $disks = $vm | Get-ScsiController | Where-Object {$_.BusSharingMode -eq 'Physical'  -or $_.BusSharingMode -eq 'Virtual'}
     foreach ($disk in $disks){
         $REPORT = New-Object -TypeName PSObject
         $REPORT | Add-Member -type NoteProperty -name Name -Value $vm.Name
         $REPORT | Add-Member -type NoteProperty -name VMHost -Value $vm.Host
         $REPORT | Add-Member -type NoteProperty -name Mode -Value $disk.BusSharingMode
         $REPORT | Add-Member -type NoteProperty -name Type -Value 'BusSharing'
         $REPORT | Add-Member -type NoteProperty -name UsedSpacegB -Value [Math]::Ceiling($vm.UsedSpaceGB)
         $array += $REPORT
      }
     }

Similar script but returning more information:

$array = @()
foreach ($vmka in $vms){
  $vmka = get-vm -name $vmka
  $REPORT = New-Object -TypeName PSObject
  $REPORT | Add-Member -type NoteProperty -name Name -Value $vmka.name
  $REPORT | Add-Member -type NoteProperty -name noSharingControlerNum -Value ($vmka |Get-ScsiController | where {$_.BusSharingMode -like "NoSharing"}).count
  $REPORT | Add-Member -type NoteProperty -name SharedControlerNum -Value ($vmka |Get-ScsiController | where {$_.BusSharingMode -eq 'Physical'  -or $_.BusSharingMode -eq 'Virtual'}).count
  #hack with -join, otherwise it is not possible to export to csv
  $noShareDisks = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like "NoSharing"}).key}).Filename -join ", "
  $REPORT | Add-Member -type NoteProperty -name noSharedDisks -Value $noShareDisks
  $sharedDisks = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like 'Physical'  -or $_.BusSharingMode -like 'Virtual'}).key}).Filename -join ", "
  $REPORT | Add-Member -type NoteProperty -name SharedDisks -Value $sharedDisks
  
  $noShareDisksNum = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like "NoSharing"}).key}).count
  $REPORT | Add-Member -type NoteProperty -name noSharedDisksNum -Value $noShareDisksNum
  $sharedDisksNum = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like 'Physical'  -or $_.BusSharingMode -like 'Virtual'}).key}).count
  $REPORT | Add-Member -type NoteProperty -name SharedDisksNum -Value $sharedDisksNum

  $REPORT | Add-Member -type NoteProperty -name noSharedDiskSizeGB -Value ((Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like "NoSharing"}).key}).CapacityGB | Measure-Object -Sum).sum
  $REPORT | Add-Member -type NoteProperty -name SharedDisksSizeGB -Value ((Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -eq 'Physical'  -or $_.BusSharingMode -eq 'Virtual'}).key}).CapacityGB | Measure-Object -Sum).sum
  $array += $REPORT
}

Most likely, you would need to modify the scripts to better suit your specific task. Nevertheless, I hope the ones presented above will be helpful.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free(BSD)
Search for duplicated files

This will be short (but hopefully I will find more time to show entire process to search duplicated files together with some examples). In case you are searching for duplicated files I can recommend two software which actually rocks in openSource world

Azure
NFS issue, cannot be mounted or is not visible

The same kind of issue I have encountered numerous times while working across different environments and with various customers. The problem with NFS mounts connected from remote locations is so common. This issue extends beyond communication solely over WAN and also include connections between datacenters (DC) where we lack control …

Azure
Why Firefox is important and people should use this browser in 2024, my thoughts.

Can you remember the times when everyone was using Internet Explorer? Back in the ’90s and the early part of this century, Internet Explorer dominated the browser market. Software Incompatibility with Other Browsers Incompatibility issues with software and other browsers have been a persistent problem. Even in 2022, this remains …