-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathCreate_encrypt_Linux_VM.ps1
49 lines (36 loc) · 1.51 KB
/
Create_encrypt_Linux_VM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Prefix for resources
$prefix = "tw"
#Some variables
$Location = "westeurope"
$id = Get-Random -Minimum 1000 -Maximum 9999
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzContext
Get-AzSubscription
Get-AzSubscription -SubscriptionName "Pay-As-You-Go" | Select-AzSubscription
#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
#Clean up resources
Remove-AzResourceGroup -Name "myResourceGroup" -Force