I got inspired by a session from Ryan Mangan, who told me about the importance of the page file for an Azure Virtual Desktop VM. So, after that, I started VS Code and created automation to set the PageFile via a CustomScriptExtension and Bicep.

So, this blog is about setting the PageFile via CustomScripExtension for your AVD VM. I’ll show you how to do this for a single VM. You can integrate it into your own bicep template and automation.

Prerequisites

You need Azure CLI on your machine to run the bicep file. You can download it here.

Install bicep tools, link.

After that, you are ready to start this blog!

Configuration

The configuration is quite simple. We push a custom Powershell script to the AVD machines via a custom script extension for Azure Virtual Machines (AVD Machines). This custom Powershell script is available via my GitHub.

This is the code:

#Get LogicDisks for machine with Temporary Disks
$LogicalDisks = Get-WmiObject Win32_LogicalDisk | Where-Object VolumeName -EQ 'Temporary Storage'

#Set Pagefile on Temp disk if exists
If ($LogicalDisks){
    
    Write-Host "Setting PageFile on $($LogicalDisks.DeviceID)"
    $pagefileset = Get-WmiObject win32_pagefilesetting | Where-Object{$_.caption -like "$($LogicalDisks.DeviceID)*"}
    $pagefileset.InitialSize = 6144
    $pagefileset.MaximumSize = 24576
    $pagefileset.Put() | Out-Null

}

The script sets a page file on the temp disk on the AVD machine. If you want to change the size of the page file edit these values:

  • $pagefileset.InitialSize
  • $pagefileset.MaximumSize

I mostly use machines with 8 cores and 28 GB ram since they scale quickly and correctly.

Next is the bicep file, this is the syntax:

param fileurl string = 'https://raw.githubusercontent.com/Ruthhl3ss/public/main/DSC/SetPagefileonTempStorage.ps1'
param vmname string = 'YOUR VM NAME'
param location string = 'westeurope'

resource customscriptextension 'Microsoft.Compute/virtualMachines/extensions@2022-03-01' = {
  name: '${vmname}/SetPageFile'
  location: location
  properties: {
    publisher: 'Microsoft.Compute'
    type: 'CustomScriptExtension'
    typeHandlerVersion: '1.10'
    autoUpgradeMinorVersion: true
    settings: {
      fileUris: [
        fileurl
      ]
      commandToExecute: 'powershell -ExecutionPolicy Bypass -File SetPagefileonTempStorage.ps1'
    }
  }
}

Please edit the parameters at the top of the Bicep template.

  • fileurl –> This is the URL where the script for setting the page file resides. Please copy/fork the script to your own repository. It now sits in my Github repo.
  • vmname –> Use the VM name on which you want to deploy the template.
  • location –> Pick your location.

In addition, with a little imagination, you can integrate this into your sessionhost deployment. 🙂

Running the template

Firstly, we need to log on to Azure. We use Azure CLI to do so. Use this command:

az login

After that, if you have multiple subscriptions, set the appropriate subscription with this command:

az account set --subscription <subscriptionid>

Lastly, run the bicep template with the following code:

az deployment group create --resource-group RESOURCEGROUPNAME --template-file PATH\CustomScriptExtension.bicep

As a result, the script will be applied to your AVD VM:

set the PageFile via CustomScriptExtension and Bicep - custom script extension

And the page file is set to the following:

And that is how you set the PageFile via CustomScriptExtension and Bicep!

References

Inspired by Ryan Mangan

Other posts:
Bicep for New Image version, Azure Compute Gallery

One thought on “Set PageFile via CustomScriptExtension and Bicep”

Leave a Reply

Your email address will not be published.