@@ -1932,6 +1932,97 @@ function Test-AzureDiskEncryptionExtension
1932
1932
}
1933
1933
}
1934
1934
1935
+ <#
1936
+ . SYNOPSIS
1937
+ Test the Set-AzVMDiskEncryptionExtension dual pass to single pass migration scenario
1938
+ #>
1939
+ function Test-AzureDiskEncryptionExtensionDualPassToSinglePassMigration
1940
+ {
1941
+ # This test should be run in Live mode only not in Playback mode
1942
+ # pre-requisites to be filled in before running this test. The AAD app should belong to the directory as the user running the test.
1943
+ $resourceGroupName = Get-ComputeTestResourceName
1944
+ try
1945
+ {
1946
+ # Check if AAD app was already created
1947
+ $aadAppName = " detestapp"
1948
+ $SvcPrincipals = Get-AzADServicePrincipal - SearchString $aadAppName ;
1949
+ if (-not $SvcPrincipals )
1950
+ {
1951
+ # Create a new AD application if not created before
1952
+ $identifierUri = [string ]::Format(" http://localhost:8080/{0}" , $rgname );
1953
+ $defaultHomePage = ' http://contoso.com' ;
1954
+ $now = [System.DateTime ]::Now;
1955
+ $oneYearFromNow = $now.AddYears (1 );
1956
+ $aadClientSecret = Get-ResourceName ;
1957
+ $ADApp = New-AzADApplication - DisplayName $aadAppName - HomePage $defaultHomePage - IdentifierUris $identifierUri - StartDate $now - EndDate $oneYearFromNow - Password $aadClientSecret ;
1958
+ Assert-NotNull $ADApp ;
1959
+ $servicePrincipal = New-AzADServicePrincipal - ApplicationId $ADApp.ApplicationId ;
1960
+ $SvcPrincipals = (Get-AzADServicePrincipal - SearchString $aadAppName );
1961
+ # Was AAD app created?
1962
+ Assert-NotNull $SvcPrincipals ;
1963
+ $aadClientID = $servicePrincipal.ApplicationId ;
1964
+ }
1965
+ else
1966
+ {
1967
+ # Was AAD app already created?
1968
+ Assert-NotNull $aadClientSecret ;
1969
+ $aadClientID = $SvcPrincipals [0 ].ApplicationId;
1970
+ Write-Verbose " Got SPN client ID.."
1971
+ }
1972
+
1973
+ # create virtual machine and key vault prerequisites
1974
+ $vm = Create- VirtualMachine $resourceGroupName
1975
+
1976
+ # Create new KeyVault
1977
+ $vaultName = " detestvault" ;
1978
+ $keyVault = New-AzKeyVault - VaultName $vaultName - ResourceGroupName $resourceGroupName - Location $loc - Sku standard;
1979
+ $keyVault = Get-AzKeyVault - VaultName $vaultName - ResourceGroupName $resourceGroupName
1980
+ # set enabledForDiskEncryption
1981
+ Set-AzKeyVaultAccessPolicy - VaultName $vaultName - ResourceGroupName $resourceGroupName - EnabledForDiskEncryption;
1982
+ # set permissions to AAD app to write secrets and keys
1983
+ Set-AzKeyVaultAccessPolicy - VaultName $vaultName - ServicePrincipalName $aadClientID - PermissionsToKeys all - PermissionsToSecrets all
1984
+ $diskEncryptionKeyVaultUrl = $keyVault.VaultUri ;
1985
+ $keyVaultResourceId = $keyVault.ResourceId ;
1986
+
1987
+ # Enable encryption on the VM
1988
+ Set-AzVMDiskEncryptionExtension - ResourceGroupName $rgname - VMName $vmName - AadClientID $aadClientID - AadClientSecret $aadClientSecret - DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl - DiskEncryptionKeyVaultId $keyVaultResourceId - Force;
1989
+ # verify encryption state
1990
+ $status = Get-AzVmDiskEncryptionStatus - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name
1991
+ Assert-NotNull $status
1992
+ Assert-AreEqual $status.OsVolumeEncrypted Encrypted
1993
+
1994
+ # verify encryption settings
1995
+ $settings = $status.OsVolumeEncryptionSettings
1996
+ Assert-NotNull $settings
1997
+ Assert-NotNull $settings.DiskEncryptionKey.SecretUrl
1998
+ Assert-AreEqual $settings.DiskEncryptionKey.SourceVault.Id $keyVaultResourceId
1999
+
2000
+ # migrate VM to single pass
2001
+ Write-Verbose " Migrate VM to 1pass"
2002
+ Set-AzVMDiskEncryptionExtension - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name - Migrate - Force;
2003
+ Write-Verbose " Migration complete"
2004
+
2005
+ # verify encryption state
2006
+ $status = Get-AzVmDiskEncryptionStatus - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name
2007
+ Assert-NotNull $status
2008
+ Assert-AreEqual $status.OsVolumeEncrypted Encrypted
2009
+
2010
+ # verify encryption settings
2011
+ $settings = $status.OsVolumeEncryptionSettings
2012
+ Assert-NotNull $settings
2013
+ Assert-NotNull $settings.DiskEncryptionKey.SecretUrl
2014
+ Assert-AreEqual $settings.DiskEncryptionKey.SourceVault.Id $keyVaultResourceId
2015
+
2016
+ # verify VM Model is null after Migration
2017
+ $vmModel = Get-AzVM - ResourceGroupName $vm.ResourceGroupName - Name $vm.Name
2018
+ Assert-Null $vmModel.StorageProfile.OSDisk.EncryptionSettings
2019
+ }
2020
+ finally
2021
+ {
2022
+ Clean - ResourceGroup($resourceGroupName )
2023
+ }
2024
+ }
2025
+
1935
2026
<#
1936
2027
. SYNOPSIS
1937
2028
Test Virtual Machine BGInfo Extensions
0 commit comments