我想出了这个解决方案:
$resourceGroup = "your resource group name"
$virtualMachine = "your virtual machine name"
# remove page file from drive d
az vm run-command invoke -g $resourceGroup -n $virtualMachine --command-id RunPowerShellScript --scripts '$CurrentPageFile = Get-WmiObject -Query "select * from Win32_PageFileSetting"; $CurrentPageFile.delete(); Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{name="c:\pagefile.sys";InitialSize = 0; MaximumSize = 0}; Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\cdrom -Name Start -Value 4 -Type DWord'
# restart server
az vm restart -g $resourceGroup -n $virtualMachine
# set drive d and it's related disk into offline mode
az vm run-command invoke -g $resourceGroup -n $virtualMachine --command-id RunPowerShellScript --scripts 'get-disk -Number (Get-Partition -DriveLetter D).disknumber | Set-Disk -IsOffline $true'
# get azure disks and initialize, format and label it (assign a drive letter as well)
az vm run-command invoke -g $resourceGroup -n $virtualMachine --command-id RunPowerShellScript --scripts 'Get-Disk | Where partitionstyle -eq "raw" | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false'
正如@Charles Xu 所说,您不能删除它,但可以忽略它并使其脱机。我花了很长时间才弄清楚,但它可能会节省其他人的时间。
如果您只需要powershell,就是这样:
$CurrentPageFile = Get-WmiObject -Query "select * from Win32_PageFileSetting";
$CurrentPageFile.delete();
Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{name="c:\pagefile.sys";InitialSize = 0; MaximumSize = 0};
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\cdrom -Name Start -Value 4 -Type DWord
# you need to restart at this stage
get-disk -Number (Get-Partition -DriveLetter D).disknumber | Set-Disk -IsOffline $true
Get-Disk | Where partitionstyle -eq "raw" | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false