0

I am able to run the below script within the VM without any error

gwmi win32_pagefilesetting
$pf=gwmi win32_pagefilesetting
$pf.Delete()
Restart-Computer –Force
Get-Partition -DriveLetter "D" | Set-Partition -NewDriveLetter "T"
$TempDriveLetter = "T"
$TempDriveLetter = $TempDriveLetter + ":"
$drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
#re-enable page file on new Drive
$drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{ Name = "$TempDriveLetter\pagefile.sys"; MaximumSize = 0; }
Restart-Computer -Force

and when I am trying to run the script via a custom script extension, I am getting the following error: Error screenshot

Script reference: https://stackoverflow.com/questions/58015765/drive-letter-override-in-azure-vm-creation

mysanj
  • 1
  • 1

1 Answers1

0

You can achieve this using the PowerShell DSC extension. The following DSC code will assign the T: letter to the first data disk, set the page file and reboot the server.

Configuration SetPageFile {

Import-DscResource -ModuleName ComputerManagementDsc
Import-DscResource -ModuleName StorageDSC

Node localhost {

    LocalConfigurationManager {
        RebootNodeIfNeeded = $true
    }

    WaitforDisk Disk2
    {
        DiskId           = 2
        RetryIntervalSec = 30
        RetryCount       = 3
    }

    Disk Disk2 {
        DiskId      = 2
        DriveLetter = 'T'
        DependsOn   = '[WaitForDisk]Disk2'
    }

    VirtualMemory PagingSettings
    {
        Type        = 'CustomSize'
        Drive       = 'T'
        InitialSize = '2048'
        MaximumSize = '2048'
        DependsOn   = '[Disk]Disk2'
    }
}

}

Add the extension to the VM specifying the following settings

enter image description here

You can find the zip file in my GitHub repo. Upload it to a storage account and select it when you click on "Browse" in the VM extension configuration.