{"id":1228,"date":"2023-10-29T00:09:00","date_gmt":"2023-10-29T00:09:00","guid":{"rendered":"https:\/\/labiol.xyz\/?p=1228"},"modified":"2024-01-19T05:42:02","modified_gmt":"2024-01-19T05:42:02","slug":"powershell-scripts-to-help-you-out-with-detailed-disk-storage-calculations","status":"publish","type":"post","link":"https:\/\/www.labiol.xyz\/index.php\/2023\/10\/29\/powershell-scripts-to-help-you-out-with-detailed-disk-storage-calculations\/","title":{"rendered":"PowerShell scripts to help you out with detailed disk\/storage calculations."},"content":{"rendered":"\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">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.<\/mark><\/p>\n\n\n\n<p>Scirpt to calculate disk number:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$vmsall = get-vm | where {$_.powerstate -like &quot;PoweredOn&quot;}. ## for all VMs defined here\n$vmdisks = @()\nforeach ($vv in $vmsall){\n  $diskforvm = New-Object -TypeName psobject\n  $diskforvm | Add-Member -MemberType NoteProperty -name &quot;name&quot;-value $vv.name\n  $diskforvm | Add-Member -MemberType NoteProperty -name &quot;disk_number&quot; -value (($vv | get-view).layoutex.disk.key).count\n  $vmdisks += $diskforvm\n}\n$vmdisks | Sort-Object -Property disk_number<\/code><\/pre><\/div>\n\n\n\n<p>Script to check various disk parameters:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#vms contains the list of VMs that we want to work on \n\nforeach ($vm in $vms){\n  Write-Host &quot;checking:&quot; + $vm\n  if (-not (get-vm $vm)){\n    $vm | out-file -Append desktop\\disk-size-scope-21062023-notfound.txt}\n  else{\n    get-vm $vm | select name, @{n=&quot;UsedSpaceGB&quot;; e={[Math]::Ceiling($_.UsedSpaceGB)}},@{n=&quot;ProvisionedSpaceGB&quot;; e={[Math]::Ceiling($_.ProvisionedSpaceGB)}}, @{n=&quot;DiskSize&quot;; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}, @{n=&quot;RealUsage&quot;; 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=&quot;ClusterName&quot;; e={(Get-Cluster -vm $_).name}}, @{n=&quot;CPU&quot;; e={cpunum}} | Export-Csv -Append -Path desktop\\disk-size-scope-21062023.csv }\n }<\/code><\/pre><\/div>\n\n\n\n<p>Script to check Physical or Virtual (Bus Sharing Mode) &#8211; indicating RDM:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#vms contains the list of VMs that we want to work on \n\n$array = @()\nforeach ($vm in $vms){\n     $vm = get-vm $vm\n     $disks = $vm | Get-ScsiController | Where-Object {$_.BusSharingMode -eq &#39;Physical&#39;  -or $_.BusSharingMode -eq &#39;Virtual&#39;}\n     foreach ($disk in $disks){\n         $REPORT = New-Object -TypeName PSObject\n         $REPORT | Add-Member -type NoteProperty -name Name -Value $vm.Name\n         $REPORT | Add-Member -type NoteProperty -name VMHost -Value $vm.Host\n         $REPORT | Add-Member -type NoteProperty -name Mode -Value $disk.BusSharingMode\n         $REPORT | Add-Member -type NoteProperty -name Type -Value &#39;BusSharing&#39;\n         $REPORT | Add-Member -type NoteProperty -name UsedSpacegB -Value [Math]::Ceiling($vm.UsedSpaceGB)\n         $array += $REPORT\n      }\n     }<\/code><\/pre><\/div>\n\n\n\n<p>Similar script but returning more information:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$array = @()\nforeach ($vmka in $vms){\n  $vmka = get-vm -name $vmka\n  $REPORT = New-Object -TypeName PSObject\n  $REPORT | Add-Member -type NoteProperty -name Name -Value $vmka.name\n  $REPORT | Add-Member -type NoteProperty -name noSharingControlerNum -Value ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &quot;NoSharing&quot;}).count\n  $REPORT | Add-Member -type NoteProperty -name SharedControlerNum -Value ($vmka |Get-ScsiController | where {$_.BusSharingMode -eq &#39;Physical&#39;  -or $_.BusSharingMode -eq &#39;Virtual&#39;}).count\n  #hack with -join, otherwise it is not possible to export to csv\n  $noShareDisks = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &quot;NoSharing&quot;}).key}).Filename -join &quot;, &quot;\n  $REPORT | Add-Member -type NoteProperty -name noSharedDisks -Value $noShareDisks\n  $sharedDisks = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &#39;Physical&#39;  -or $_.BusSharingMode -like &#39;Virtual&#39;}).key}).Filename -join &quot;, &quot;\n  $REPORT | Add-Member -type NoteProperty -name SharedDisks -Value $sharedDisks\n  \n  $noShareDisksNum = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &quot;NoSharing&quot;}).key}).count\n  $REPORT | Add-Member -type NoteProperty -name noSharedDisksNum -Value $noShareDisksNum\n  $sharedDisksNum = (Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &#39;Physical&#39;  -or $_.BusSharingMode -like &#39;Virtual&#39;}).key}).count\n  $REPORT | Add-Member -type NoteProperty -name SharedDisksNum -Value $sharedDisksNum\n\n  $REPORT | Add-Member -type NoteProperty -name noSharedDiskSizeGB -Value ((Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -like &quot;NoSharing&quot;}).key}).CapacityGB | Measure-Object -Sum).sum\n  $REPORT | Add-Member -type NoteProperty -name SharedDisksSizeGB -Value ((Get-HardDisk -VM $vmka | where {$_.extensiondata.controllerkey -in ($vmka |Get-ScsiController | where {$_.BusSharingMode -eq &#39;Physical&#39;  -or $_.BusSharingMode -eq &#39;Virtual&#39;}).key}).CapacityGB | Measure-Object -Sum).sum\n  $array += $REPORT\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Script to check various disk parameters: Script to check Physical or &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1228","post","type-post","status-publish","format-standard","hentry","category-vmware"],"_links":{"self":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1228"}],"version-history":[{"count":4,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1228\/revisions"}],"predecessor-version":[{"id":1232,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1228\/revisions\/1232"}],"wp:attachment":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}