Set up full disk encryption for a Linux VM with PowerShell in Azure!



Hi Azure friends,


I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):


#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp


#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose


#Log into Azure


#Select the correct subscription



Get-AzSubscription -SubscriptionName "your subscription name" | Select-AzSubscription


#Prefix for resources
$prefix = "tw"

#Some variables
$Location = "westeurope"
$id = Get-Random -Minimum 1000 -Maximum 9999
#Create a resource group
New-AzResourceGroup -Name "myResourceGroup" -Location $Location

#Create a virtual machine
$cred = Get-Credential
New-AzVM -Name MyVm -Credential $cred -ResourceGroupName MyResourceGroup -Image Canonical:UbuntuServer:18.04-LTS:latest -Size Standard_D2S_V3

#Create a Key Vault configured for encryption keys
$keyVaultParameters = @{
    Name = "$prefix-key-vault-$id"
    ResourceGroupName = "MyResourceGroup"
    Location = $location
    EnabledForDiskEncryption = $true
    EnabledForDeployment = $true
    Sku = "Standard"

$keyVault = New-AzKeyVault @keyVaultParameters

#Encrypt the virtual machine
$KeyVault = Get-AzKeyVault -VaultName "$prefix-key-vault-$id" -ResourceGroupName "MyResourceGroup"

Set-AzVMDiskEncryptionExtension -ResourceGroupName MyResourceGroup -VMName "MyVM" -DiskEncryptionKeyVaultUrl $KeyVault.VaultUri -DiskEncryptionKeyVaultId $KeyVault.ResourceId -SkipVmBackup -VolumeType All

#You can verify the encryption process
Get-AzVmDiskEncryptionStatus -VMName MyVM -ResourceGroupName MyResourceGroup
Now you have used the PowerShell to create a Linux VM with full disk encryption! Congratulations!
#Clean up (when you no longer need the resources)
Remove-AzResourceGroup -Name "myResourceGroup" -Force

I hope this article was useful. Best regards, Tom Wechsler


P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github!

0 Replies