## Callum Stevenson 2020 [long[]]$TotalUsed = 0,0,0,0,0,0,0,0,0,0,0,0,0 [long[]]$TotalAlloc = 0,0,0,0,0,0,0,0,0,0,0,0,0 [int[]]$DriveLetterID = 0,0,0,0,0,0,0,0,0,0,0,0,0 [long]$ChainedVHDSize $DirtyDataUsed = 0 $DirtyDataAlloc = 0 $vm = 0 $diskCount = 1 $CheckpointWarning = "" $isOverallocated = 9 ## EMAIL STUFF $SendEmailTo = "callum@secomtech.com.au" # Sender Credentials $msg = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient($smtpServer) ## END EMAIL STUFF function Get-DriveLetterFromInt($givenInt){ [string]$foundLetter switch($givenInt){ 1 {$foundLetter = "A" } 2 {$foundLetter = "B" } 3 {$foundLetter = "C" } 4 {$foundLetter = "D" } 5 {$foundLetter = "E" } 6 {$foundLetter = "F" } 7 {$foundLetter = "G" } 8 {$foundLetter = "H" } 9 {$foundLetter = "I" } 10 {$foundLetter = "J" } 11 {$foundLetter = "K" } 12 {$foundLetter = "L" } 13 {$foundLetter = "M" } 14 {$foundLetter = "N" } 15 {$foundLetter = "O" } 16 {$foundLetter = "P" } 17 {$foundLetter = "Q" } 18 {$foundLetter = "R" } 19 {$foundLetter = "S" } 20 {$foundLetter = "T" } 21 {$foundLetter = "U" } 22 {$foundLetter = "V" } 23 {$foundLetter = "Q" } 24 {$foundLetter = "W" } 25 {$foundLetter = "Y" } 26 {$foundLetter = "Z" } } return $foundLetter; } function Get-DriveIntFromChar($givenChar){ [int]$foundInt switch($givenChar){ A {$foundInt = 1} B {$foundInt = 2} C {$foundInt = 3} D {$foundInt = 4} E {$foundInt = 5} F {$foundInt = 6} G {$foundInt = 7} H {$foundInt = 8} I {$foundInt = 9} J {$foundInt = 10} K {$foundInt = 11} L {$foundInt = 12} M {$foundInt = 13} N {$foundInt = 14} O {$foundInt = 15} P {$foundInt = 16} Q {$foundInt = 17} R {$foundInt = 18} S {$foundInt = 19} T {$foundInt = 20} U {$foundInt = 21} V {$foundInt = 22} W {$foundInt = 23} X {$foundInt = 24} Y {$foundInt = 25} Z {$foundInt = 26} } return $foundInt; } #cs Write-Output "C________STARTING_________" foreach ($vm in get-vm){ $vmname = $vm.VMName Write-Output "________PROCESSING_$vmname" ## Get Allocated Space $AllocSpaceArray = Get-VM -VMName $vmname | Select-Object vmid | Get-VHD | Select @{expression={$_.size }} $AllocLocationArray = Get-VM -VMName $vmname | Select-Object vmid | Get-VHD | Select @{expression={$_.Path }} $AllocSpaceArray = $AllocSpaceArray -replace "\D+", "" Write-host '# disks='$AllocSpaceArray.Count $diskCount = 0; foreach($location in $AllocLocationArray){ Write-host "----" $diskCount++ [string]$StringLocation = $location $StrippedLocation = $StringLocation.SubString(11,1) $VHDType = $StringLocation.Substring($StringLocation.Length - 6 , 5) $PathFrontStripped = $StringLocation.Substring(11, $StringLocation.Length - 12) Write-host "Disk" $diskCount $PathFrontStripped Write-host "Drive Letter = " $StrippedLocation Write-host "VHD Type = " $VHDType $Path = $PathFrontStripped while($Path = (Get-VHD -Path $Path).ParentPath) # THIS IS FOR CHECKING AVHDX PARENTS { Write-Host "parent path ; "$Path $ParentVhdSize = Get-VHD -Path $Path | Select @{expression={$_.filesize }} $ParentVhdSize = $ParentVhdSize -replace "\D+", "" [long]$ParentVhdLong = $ParentVhdSize #$ParentVhdLong = $ParentVhdLong / 1gb Write-Host "parent size check ; "$ParentVhdLong $ChainedVHDSize = $ChainedVHDSize + $ParentVhdLong } if($VHDType -eq "avhdx" -or $VHDType -eq ".avhd" ){ $CheckpointWarning = "Checkpoints/avhd(x) Found! Results may be inaccurate! check script logs for offending disk" } switch($StrippedLocation){ A { $DriveLetterID[$diskCount] = 1 } B { $DriveLetterID[$diskCount] = 2 } C { $DriveLetterID[$diskCount] = 3 } D { $DriveLetterID[$diskCount] = 4 } E { $DriveLetterID[$diskCount] = 5 } F { $DriveLetterID[$diskCount] = 6 } G { $DriveLetterID[$diskCount] = 7 } H { $DriveLetterID[$diskCount] = 8 } I { $DriveLetterID[$diskCount] = 9 } J { $DriveLetterID[$diskCount] = 10 } K { $DriveLetterID[$diskCount] = 11 } L { $DriveLetterID[$diskCount] = 12 } M { $DriveLetterID[$diskCount] = 13 } N { $DriveLetterID[$diskCount] = 14 } O { $DriveLetterID[$diskCount] = 15 } P { $DriveLetterID[$diskCount] = 16 } Q { $DriveLetterID[$diskCount] = 17 } R { $DriveLetterID[$diskCount] = 18 } S { $DriveLetterID[$diskCount] = 19 } T { $DriveLetterID[$diskCount] = 20 } U { $DriveLetterID[$diskCount] = 21 } V { $DriveLetterID[$diskCount] = 22 } W { $DriveLetterID[$diskCount] = 23 } X { $DriveLetterID[$diskCount] = 24 } Y { $DriveLetterID[$diskCount] = 25 } Z { $DriveLetterID[$diskCount] = 26 } } } $diskCount = 0; foreach ($AllocSpace in $AllocSpaceArray){ $diskCount++ #Write $TotalAlloc[$DriveLetterID[$diskCount]] += $AllocSpace Write-host " Disk" $diskCount "total Allocated = "($AllocSpace/1gb) } #### ## Get Used Space $UsedSpaceArray = Get-VM -VMName $vmname | Select-Object vmid | Get-VHD | Select @{expression={$_.filesize }} $UsedSpaceArray = $UsedSpaceArray -replace "\D+", "" $diskCount = 0; foreach ($UsedSpace in $UsedSpaceArray){ $diskCount++ $TotalUsed[$DriveLetterID[$diskCount]] += $UsedSpace Write-host " Disk" $diskCount "total Used = "($UsedSpace/1gb) } } Write-host " " $msg.body = $msg.body + "`n" + "_____________~~~~~~~ RESULTS ~~~~~~~_______________" $msg.body = $msg.body + "`n" + "--- For " + [System.Net.Dns]::GetHostName() for($i=1; $i -le 28; $i++){ if($TotalAlloc[$i] -gt 1){ $msg.body = $msg.body + "`n" + "" [string]$SelectedDisk = Get-DriveLetterFromInt($i) $msg.body = $msg.body + "`n" + "____~~~$SelectedDisk : ~~~______" $VHDAllocated = ([math]::Round(($TotalAlloc[$i]/1gb),2)) $VHDUsed = ([math]::Round( ($TotalUsed[$i]/1gb),2)) $msg.body = $msg.body + "`n" + "VHD Allocated (Gb) = " +"`t`t"+ $VHDAllocated # NEED TO GIVE ARRAY $msg.body = $msg.body + "`n" + "VHD Used (Gb) = " +"`t`t"+ $VHDUsed # NEED TO GIVE ARRAY $ChainVHDUsed = ([math]::Round( ($ChainedVHDSize/1gb),2)) $msg.body = $msg.body + "`n" + "Parent VHD Used (Gb) = " +"`t"+ $ChainVHDUsed # NEED TO GIVE ARRAY ## Get Stats of Drive $Diskused = (Get-PSDrive $SelectedDisk.SubString(1) | Select-Object Used) $Diskused = $Diskused -replace "\D+", "" $DiskFree = (Get-PSDrive $SelectedDisk.SubString(1) | Select-Object Free) $DiskFree = $DiskFree -replace "\D+", "" $LongDiskUsed = ([math]::Round(($Diskused/1gb),2)) $LongDiskFree = ([math]::Round(($DiskFree/1gb),2)) $DiskTotal = $LongDiskFree + $LongDiskUsed $nonVHDFileSize = ($LongDiskUsed - ($VHDUsed + $ChainVHDUsed)) ## Write Out $msg.body = $msg.body + "`n" + "Drive Used (Gb) = " +"`t`t"+ ($LongDiskUsed) # NEED TO GIVE ARRAY $msg.body = $msg.body + "`n" + "Drive Free (Gb) = " +"`t`t"+ ($LongDiskFree) # NEED TO GIVE ARRAY $msg.body = $msg.body + "`n" + "Drive Total (Gb) = " +"`t`t"+ ($DiskTotal) # NEED TO GIVE ARRAY $msg.body = $msg.body + "`n" + "Non VHD Space Used = " +"`t`t"+ $nonVHDFileSize $TotalTheorySize = $VHDAllocated + $nonVHDFileSize $UsableFreeSpace = $DiskTotal - $TotalTheorySize $msg.body = $msg.body + "`n" + "Total Theoretical space needed = " +"`t`t"+ $TotalTheorySize $msg.body = $msg.body + "`n" + "Usable Free Space = " +"`t`t"+ $UsableFreeSpace if($TotalTheorySize -le $DiskTotal){ $isOverallocated = 0 $msg.body = $msg.body + "`n" + " Drive OK!" $driveResults = "DRIVE OK!" } elseif ($TotalTheorySize -ge $DiskTotal) { $isOverallocated = 1 $msg.body = $msg.body + "`n" + " !!! !!! !!! DRIVE IS OVERALLOCATED !!! !!! !!! " $msg.body = $msg.body + "`n" + " !!! !!! !!! DRIVE IS OVERALLOCATED !!! !!! !!! " $OverallocationAmount = $TotalTheorySize - $DiskTotal $msg.body = $msg.body + "`n" + " Overallocated by : " +"`t`t"+ $OverallocationAmount $driveResults = 'DRIVE OVERALLOCATED BY + $OverallocationAmount' } else { $msg.body = $msg.body + "`n" + " BAD READ! " } } } $msg.subject = "##1- Automatic Script Results - Overallocated = $isOverallocated" $msg.body = $msg.body + "`n" + "" $msg.body = $msg.body + "`n" + $CheckpointWarning Write-host $msg.body ##$msg.body = $msg.body + "HELLO LOL" ##$smtp.Send($msg) ##$msg