Udostępnij za pośrednictwem


Move the Azure temporary disk to a different drive letter on Windows Server

On occasion you may have a need to move the Azure temporary drive to a different drive letter. Azure by default is set to use the D drive. This drive letter configuration may conflict with existing scripts or company OS installation standards. I’ve created an ARM template that uses PowerShell DSC to allow you to move the drive letter. It performs the following steps:

1) Disables the Windows Page File and reboots the VM
2) Changes the drive letter from the D drive to a drive letter you specify in the ARM template parameters file
3) Re-enables the Windows page file and reboots the VM

This project is in GitHub here: https://github.com/perktime/MoveAzureTempDrive. To use it, modify the azuredeploy.parameters.json file with your vmName and your desired tempDriveLetter:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": {
"value": "<put_your_existing_vm_name_here>"
},
"assetLocation": {
"value": "https://petedscutil.blob.core.windows.net/scripts"
    },
"tempDriveLetter": {
"value": "Z"
}
}
}

Optionally, you can copy the MoveAzureTempDrive.ps1.zip DSC file to your own Azure storage account and modify the assetLocation parameter as well. Also, if you have an existing DSC extension you will have to remove it before deploying this.

If you are interested in how this works, here is the explanation (note: assuming you understand how PowerShell DSC works):

To disable the Windows page file, we use “gwmi win32_pagefilesetting” which uses WMI to first check if the page file is enabled or not. If it is, we use this script to delete it and restart the VM:

gwmi win32_pagefilesetting
$pf=gwmi win32_pagefilesetting
$pf.Delete()
Restart-Computer –Force

Once the VM restarts, the PowerShell DSC module will then change the drive letter to your desired drive and then re-enable the page file and reboot:

Get-Partition -DriveLetter "D"| Set-Partition -NewDriveLetter $TempDriveLetter
$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

Comments

  • Anonymous
    January 03, 2017
    Good stuff. Thanks for taking the time to post this.
    • Anonymous
      January 06, 2017
      Thanks Bill!
  • Anonymous
    January 08, 2017
    Thank you kindly !
  • Anonymous
    April 07, 2017
    Trying to run this from an arm template while the machine is being provisioned results in the following error: WinRM firewall exception will not work since one of the network connection types on this machine is set to Public. Change the network connection type to either Domain or Private and try again.Any idea?
  • Anonymous
    June 06, 2017
    Would be cool if this worked on Server 2008R2.
  • Anonymous
    October 31, 2017
    I have a script that builds a VM in Azure. It already has extension added to do a domain join. I'll like to know if I can add this extension in below format. I can't seem to get it right. It keeps getting errors. Any help would be appreciated. #========================Change Temporary Drive Letter===========================#$PublicSettings = '{"ModulesURL":"https://github.com/perktime/MoveAzureTempDrive/blob/master/Scripts/MoveAzureTempDrive.ps1.zip", "configurationFunction": "MoveAzureTempDrive.ps1\MoveAzureTempDrive", "Properties": {"MachineName": $VMname} }'Set-AzureRmVMExtension -ExtensionName "DSC" -ResourceGroupName myVMResourceGroup -VMName $vmName -Publisher "Microsoft.Powershell" -ExtensionType "DSC" -TypeHandlerVersion 2.19 -SettingString $PublicSettings -Location $VMResourceGroupLocation