Search code examples
azurepowershellazure-cliazure-cli2

Is there any way do create a vm in azure without temporary storage drive?


I used this command to spin up a virtual machine :

az vm create -n $virtualMachine -g $resourceGroup --size Standard_D2_v3 --image win2016datacenter --data-disk-sizes-gb 40
--location $location --admin-username $admin --admin-password $password

Every time it creates a Drive D labeled "Temporary Storage", Is there any way I could get rid of this drive?

P.S I know what it dose and I do not need it.


Solution

  • I have came up with this solution:

    $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'
    

    As @Charles Xu said you can not delete it but you can ignore it and make it offline. it took me a wile to figure it out , yet it might save some time from others.

    If you just need the powershell this is it:

    $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