Pagefile For Exchange 2013 / 2016 – Single Server

Memory Management and the newest versions of Exchange. Any Exchange Server needs to have an appropriately sized Pagefile in order for the server to perform optimally. Microsoft has a series of Best Practices around the configuration of Windows Server for Exchange. Let’s start with the basics of the Pagefile on an Exchange server:

  • The Pagefile should not be ‘System Managed’
  • The Pagefile Initial and Maximum sizes should be the same
  • The Pagefile should be sized as RAM + 10MB
  • The Maximum size of the Pagefile should be 32778 MB (32GB +10MB)

For anyone familiar with Exchange, most if all of these guidelines should be relatively straight forward. What I want to focus on for this article, is how do we configure this reliably on an Exchange Servers?

Make sure to read this article on how to properly configure Exchange for the best performance, including configuring the PageFile properly:

Ask The Perf Guy: Sizing Guidance Updates For Exchange 2013 SP1

PowerShell Script

Like most of the needs for Exchange Server, a good PowerShell script can provide a valid solution. This one will be a bit more complex because when working with Pagefile settings on a remote computer will require a few steps to complete the process. We need to be able to change the Pagefile from a System Managed (default) to a custom size file. Doing so requires a removal and creation of a new Pagefile.sys file on the affected server.

Code for changing the PageFile size (only)

# Variables
$Computername = $env:computername

# Get RAM and set ideal PageFileSize
try {
    [int32]$RamInMb = (Get-CIMInstance -computername $name -Classname win32_physicalmemory -ErrorAction Stop | measure-object -property capacity -sum).sum/1MB
} catch {
    write-host "Cannot acquire the amount of RAM in the server." -ForegroundColor Red
    $stop = $true
}
[int32]$Size = $RAMinMb + 10

# Set Maximum Pagefile Size
If ($Size -gt 32778) {
    $Size = 32778
}

Write-Host " "
Write-Host "The Pagefile will be set to a Maximum size of $Size MB and a Minimum size of $Size MB." -ForegroundColor Yellow
Write-Host " "

# Set the Automatic Pagefile to False
Set-CimInstance -Query “Select * from win32_computersystem” -Property @{automaticmanagedpagefile=”False”}  -ErrorAction STOP

# Remove the old Pagefile
$PageFile = Get-CimInstance -ClassName Win32_PageFileSetting
$PageFile | Remove-CimInstance

# Set the Pagefile to the new size
New-CimInstance -ClassName Win32_PageFileSetting -Property  @{Name= "$("C"):\pagefile.sys"}
Get-CimInstance -ClassName Win32_PageFileSetting | Set-CimInstance -Property @{InitialSize = $size; MaximumSize = $size}

# End Result
$pagefile = Get-CimInstance win32_PageFileSetting -Property * | select-object Name,initialsize,maximumsize
$name = $pagefile.name;$max = $pagefile.maximumsize;$min = $pagefile.initialsize
write-host " "
write-host "The page file of $name is now configured for an initial size of " -ForegroundColor white -NoNewline
write-host "$min " -ForegroundColor green -NoNewline
write-host "and a maximum size of " -ForegroundColor white -NoNewline
write-host "$max." -ForegroundColor Green
write-host " "
Write-Host "DON'T FORGET TO REBOOT THE SERVER!!!" -ForegroundColor Red

Sample Run Through
On a server that is configured with a Managed Pagefile, the script should work flawlessly and produce output like this:

pagefileresult-1


Troubleshooting Errors

When testing the script initially, the Pagefile was set correctly every time. However, when preparing for this blog post, I ran the script on a different set of lab servers and lo and behold the Pagefile would not set correctly. The script instead generated the below error:

pagefileresult-2

The error message “Value Out Of Range” is a bit strange and a bit non-descriptive as well. In order to troubleshoot this I used the $Size variable, the variable used as the value for setting a Minimum and Maximum Pagefile size. For a server with 24 GB or RAM, the $Size variable would be 24586. I experimented with many values and was only able to set the $Size variable to about 20000 before the Pagefile could be set properly. Anything above 20000 would generate the above error. After a bit of poking around, it seemed to be related to the amount of disk space available on the drive. In order to prevent the script from failing to configure this correctly, a disk space check needs to be placed at the beginning of the script:

$Disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Computername -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$MB = $Disk.FreeSpace/1mb
$FreeSpace = [Math]::Round($MB)
if ($FreeSpace -lt $Size) {
    Write-Host "There isn't enough diskspace available for configuring the Pagefile to the correct size." -ForegroundColor Yellow
    Write-Host "The Pagefile needs to be $Size MB and the amount of freespace is $FreeSpace MB." -ForegroundColor White
} Else {
    (Code from above)
}

After putting this in place the script did indeed generate the error message coded above:

pagefileresult-3
Final Code
Adding the disk space code from the above section, we get the below completed script code:

# Variable
$Computername = $Env:ComputerName

# Get RAM and set ideal PageFileSize
try {
    [int32]$RamInMb = (Get-CIMInstance -computername $Name -Classname win32_physicalmemory -ErrorAction Stop | measure-object -property capacity -sum).sum/1MB
} catch {
    Write-Host "Cannot acquire the amount of RAM in the server." -ForegroundColor Red
    $Stop = $True
}
[int32]$Size = $RAMinMb + 10

# Set Maximum Pagefile Size
If ($Size -gt 32778) {
    $Size = 32778
}

# Disk Space Check
$Disk = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$MB = $Disk.FreeSpace/1mb
$FreeSpace = [Math]::Round($MB)

if ($FreeSpace -lt $Size) {
    Write-Host "There isn't enough diskspace available for configuring the Pagefile to the correct size." -ForegroundColor Yellow
    Write-Host "The Pagefile needs to be $Size MB and the amount of freespace is $FreeSpace MB." -ForegroundColor White
} Else {

    Write-Host " "
    Write-Host "The Pagefile will be set to a Maximum size of $Size MB and a Minimum size of $Size MB." -ForegroundColor Yellow
    Write-Host " "

    # Set the Automatic Pagefile to False
    Set-CimInstance -Query “Select * from Win32_ComputerSystem” -Property @{AutomaticManagedPagefile=”False”}  -ErrorAction STOP

    # Remove the old Pagefile
    $PageFile = Get-CimInstance -ClassName Win32_PageFileSetting
    $PageFile | Remove-CimInstance

    # Set the Pagefile to the new size
    New-CimInstance -ClassName Win32_PageFileSetting -Property  @{Name= "$("C"):\pagefile.sys"}
    Get-CimInstance -ClassName Win32_PageFileSetting | Set-CimInstance -Property @{InitialSize = $Size; MaximumSize = $Size}

    # End Result
    $Pagefile = Get-CimInstance Win32_PageFileSetting -Property * | Select-Object Name,InitialSize,MaximumSize
    $Name = $Pagefile.name;$Max = $Pagefile.maximumsize;$Min = $Pagefile.initialsize
    Write-Host " "
    Write-Host "The page file of $Name is now configured for an initial size of " -ForegroundColor White -NoNewline
    Write-Host "$Min " -ForegroundColor Green -NoNewline
    Write-Host "and a maximum size of " -ForegroundColor White -NoNewline
    Write-Host "$Max." -ForegroundColor Green
    Write-Host " "
    Write-Host "DON'T FORGET TO REBOOT THE SERVER!!!" -ForegroundColor Red
}



Summary
This code is valid for the current server and should be executed only locally. Remember to reboot the server after the changes are made. In my next blog post, I will show how you can take the above code block and apply it to multiple servers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s