Private/AzStackHci.MemoryDump.Helpers.ps1

# ///////////////////////////////////////////////////////////////////
# SetDedicatedDumpFileSize Function
# Used to calculate size of dedicated dump file, based on the
# physical node memory size.
# ///////////////////////////////////////////////////////////////////
Function SetDedicatedDumpFileSize {
    <#
    .SYNOPSIS
 
    Sets the size of the dedicated dump file
 
    .DESCRIPTION
 
    Sets the size of the dedicated dump file, based on the physical node memory size.
 
    #>


    begin {
        # Requires administrator permissions to set dedicated dump file size and check system memory
        if (-not (Test-Elevation)) { throw "This script must be run as an Administrator." }

        # Set default minimum dedicated dump file size, for systems with less than 768 GiB of memory
        [uint32]$SetDedicatedDumpFileSize = 65536 # 64GB
    }

    process {
        try
        {
            $totalPhysicalMemory = (Get-CimInstance -ClassName Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum
            Write-Verbose "System memory size == $([math]::round($totalPhysicalMemory/1Gb,2)) GiB" -Verbose
            if ($totalPhysicalMemory -ge 768GB)
            {
                # large memory systems, require 128 GiB dedicated dump file size
                $SetDedicatedDumpFileSize = 131072 # 128 GiB
            }
        
        } catch {

            # If we fail to get total physical memory, just use the default size.
            Write-Verbose "Failed getting total physical memory. Error: $($_.Exception.Message)" -Verbose
            Throw "Failed getting total physical memory. Error: $($_.Exception.Message)"
        }
        Write-Verbose "Dedicated Dump File Size required: '$SetDedicatedDumpFileSize' MiB" -Verbose

    } # End of process block

    end {
        Write-Debug "Completed SetDedicatedDumpFileSize function"
        return $SetDedicatedDumpFileSize
    }

} # End of SetDedicatedDumpFileSize


# ///////////////////////////////////////////////////////////////////
# SetDedicatedDumpFilePath Function
# Used to define the path for the dedicated dump file, using
# the 'DedicatedDumpFileDriveLetter' input parameter
# ///////////////////////////////////////////////////////////////////
Function SetDedicatedDumpFilePath {
    <#
    .SYNOPSIS
 
    Sets the path for the dedicated dump file
 
    .DESCRIPTION
 
    Sets the path for the dedicated dump file, using the 'DedicatedDumpFileDriveLetter' input parameter.
    Checks free disks space on the target drive, and ensures there is sufficient space for the dedicated dump file.
 
    #>


    [CmdletBinding()]
    param (
        # Path to drive letter for DedicatedDumpFile
        [Parameter(Mandatory=$true,Position=1)]
        [ValidateScript({Test-Path $_})]
        [String]
        [ValidatePattern('^[C-Zc-z]:\\?$')]$DedicatedDumpFileDriveLetter, # Drive letter for the dedicated dump file, must be a valid drive letter (C: through Z:)

        # Optional switch, used to ignore disk space check for the dedicated dump file
        [Parameter(Mandatory=$false,Position=2)]
        [Switch]
        $IgnoreDiskSpaceCheck
    )

    begin {
        # Requires administrator permissions to set dedicated dump file path and check disk space
        if (-not (Test-Elevation)) { throw "This script must be run as an Administrator." }
    }

    process {
        # if the drive letter is only 2 characters (C:), add a backslash to the end (C:\)
        if($DedicatedDumpFileDriveLetter.Length -eq 2){
            $DedicatedDumpFileDriveLetter = $($DedicatedDumpFileDriveLetter + "\")
        }

        # Create the path for the dedicated dump file using the drive letter
        [string]$DedicatedDumpFilePath = "$($DedicatedDumpFileDriveLetter)DedicatedDumpFile.sys"
        try
        {
            # Check path and disk space
            $Disk = Get-PSDrive -ErrorAction Stop | Where-Object { $PSItem.Root -eq $DedicatedDumpFileDriveLetter }
            Write-Verbose "Current Disk Free Size = $([math]::round($Disk.Free / 1GB,2)) GiB" -Verbose
            Write-Verbose "Dedicated Dump File size required for node = $($script:DedicatedDumpFileSize*1Mb/1Gb) GiB" -Verbose
            # Add the minimum disk space required for the dedicated dump file, plus 10% of the total disk space
            $script:MinimumRequiredDiskSpace = [math]::round($script:DedicatedDumpFileSize*1Mb/1Gb + ((($disk.Free + $disk.Used)/1Gb) * 0.1),2)
            Write-Verbose "Minimum Required Disk Space, including 10% additional free disk space = $script:MinimumRequiredDiskSpace GiB" -Verbose
            if (($Disk.Free/1Gb -lt $script:MinimumRequiredDiskSpace) -and (-not($IgnoreDiskSpaceCheck.IsPresent)))
            {
                Write-Verbose "Node physical disk free space is below minimum size for large host dump settings + 10% reserve, so settings can not be applied." -Verbose
                throw "Node physical disk free space is below minimum size for large host dump settings + 10% reserve, so settings can not be applied."
            } else {
                if($IgnoreDiskSpaceCheck.IsPresent){
                    Write-Verbose "*** Free disk space checks have been ignored, settings can be applied.... ***" -Verbose
                } else { 
                    Write-Verbose "Node physical disk free space is above minimum required for dedicated dump settings + 10% reserve. Settings can be applied." -Verbose
                    Write-Verbose "Sufficient disk space on $DedicatedDumpFileDriveLetter for a dedicated dump file of $([math]::round($DedicatedDumpFileSize*1MB/1Gb)) GiB." -Verbose
                }
            }
        } catch{
            # If we fail to get free disk space on target drive, error
            Write-Verbose "Failed getting free disk space on $DedicatedDumpFileDriveLetter. Error: $($_.Exception.Message)" -Verbose
            Throw "Failed getting free disk space on $DedicatedDumpFileDriveLetter. Error: $($_.Exception.Message)"
        }
            Write-Verbose "Dedicate Dump File Path = '$DedicatedDumpFilePath' and there is sufficient free space on the target drive."

    } # End of process block

    end {
        Write-Debug "Completed SetDedicatedDumpFilePath function"

        return $DedicatedDumpFilePath
    }
} # End of SetDedicatedDumpFilePath