Prepare and deploy an Azure SQL Server for Always Encrypted

%3CLINGO-SUB%20id%3D%22lingo-sub-1960146%22%20slang%3D%22en-US%22%3EPrepare%20and%20deploy%20an%20Azure%20SQL%20Server%20for%20Always%20Encrypted%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1960146%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Azure%20friends%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%26nbsp%3BPlease%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23So%20that%20you%20can%20carry%20out%20the%20configuration%2C%20you%20need%20the%20necessary%20cmdlets%2C%20these%20are%20contained%20in%20the%20module%20Az%20(is%20the%20higher-level%20module%20from%20a%20number%20of%20submodules)%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall-Module%20-Name%20Az%20-Force%20-AllowClobber%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23For%20this%20example%20I%20am%20using%20passwords%20in%20the%20script.%20This%20is%20usually%20not%20a%20good%20idea%2C%20but%20it's%20fine%20for%20the%20demo.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%23Prefix%26nbsp%3Bfor%26nbsp%3Bresources%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24prefix%26nbsp%3B%3D%26nbsp%3B%22tw%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Log%26nbsp%3Binto%26nbsp%3BAzure%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CP%3E%23Select%20the%20correct%20subscription%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzContext%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%20-SubscriptionName%20%22your%20subscription%20name%22%20%7C%20Select-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Let's%26nbsp%3Bcreate%26nbsp%3Ba%26nbsp%3BSQL%26nbsp%3BServer%26nbsp%3Bthat%26nbsp%3Bwe%26nbsp%3Bwill%26nbsp%3Buse%26nbsp%3BAlways%26nbsp%3BEncrypted%26nbsp%3Bwith%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24Location%26nbsp%3B%3D%26nbsp%3B%22westeurope%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24id%26nbsp%3B%3D%26nbsp%3BGet-Random%26nbsp%3B-Minimum%26nbsp%3B1000%26nbsp%3B-Maximum%26nbsp%3B9999%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24ResourceGroupName%26nbsp%3B%3D%26nbsp%3B%22%24prefix-sql-ae-%24id%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24SQLServerName%26nbsp%3B%3D%26nbsp%3B%22%24prefix-sql-%24id%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24SQLDatabaseName%26nbsp%3B%3D%26nbsp%3B%22Hospital%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24SQLAdmin%26nbsp%3B%3D%26nbsp%3B%22sqladmin%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24SQLAdminPassword%26nbsp%3B%3D%26nbsp%3BConvertTo-SecureString%26nbsp%3B-String%26nbsp%3B'P%40ssw0rd999!'%26nbsp%3B-AsPlainText%26nbsp%3B-Force%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24SQLAdminCredentials%26nbsp%3B%3D%26nbsp%3BNew-Object%26nbsp%3B-TypeName%26nbsp%3BSystem.Management.Automation.PSCredential%26nbsp%3B-ArgumentList%26nbsp%3B%24SQLAdmin%2C%24SQLAdminPassword%3C%2FSTRONG%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%23We%26nbsp%3Bneed%26nbsp%3Bour%26nbsp%3Bpublic%26nbsp%3BIP%20(SQL%20Firewall%20configuration)%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24MyIPAddress%26nbsp%3B%3D%26nbsp%3BInvoke-RestMethod%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fipinfo.io%2Fjson%26nbsp%3B%7C%26nbsp%3BSelect-Object%26nbsp%3B-ExpandProperty%26nbsp%3Bip%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fipinfo.io%2Fjson%26nbsp%3B%7C%26nbsp%3BSelect-Object%26nbsp%3B-ExpandProperty%26nbsp%3Bip%3C%2FA%3E%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Ba%26nbsp%3Bresource%26nbsp%3Bgroup%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3BSQL%26nbsp%3BServer%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24sqlAeRG%26nbsp%3B%3D%26nbsp%3BNew-AzResourceGroup%26nbsp%3B-Name%26nbsp%3B%24ResourceGroupName%26nbsp%3B-Location%26nbsp%3B%24Location%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Now%26nbsp%3Bwe%26nbsp%3Bneed%26nbsp%3Bto%26nbsp%3Bcreate%26nbsp%3Ba%26nbsp%3BKey%26nbsp%3BVault%26nbsp%3Bto%26nbsp%3Buse%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3BSQL%26nbsp%3BServer%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24keyVaultParameters%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BName%26nbsp%3B%3D%26nbsp%3B%22%24prefix-key-vault-%24id%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BResourceGroupName%26nbsp%3B%3D%26nbsp%3B%24sqlAeRG.ResourceGroupName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BLocation%26nbsp%3B%3D%26nbsp%3B%24location%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BEnabledForDiskEncryption%26nbsp%3B%3D%26nbsp%3B%24true%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BEnabledForDeployment%26nbsp%3B%3D%26nbsp%3B%24true%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BSku%26nbsp%3B%3D%26nbsp%3B%22Standard%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24keyVault%26nbsp%3B%3D%26nbsp%3BNew-AzKeyVault%26nbsp%3B%40keyVaultParameters%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Now%26nbsp%3Bcreate%26nbsp%3Ban%26nbsp%3BAAD%26nbsp%3BSPN%26nbsp%3Bto%26nbsp%3Buse%26nbsp%3Bwith%26nbsp%3BConsole%26nbsp%3BApp%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EImport-Module%26nbsp%3BAz.Resources%26nbsp%3B%23%26nbsp%3BImports%26nbsp%3Bthe%26nbsp%3BPSADPasswordCredential%26nbsp%3Bobject%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24credProps%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BStartDate%26nbsp%3B%3D%26nbsp%3BGet-Date%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BEndDate%26nbsp%3B%3D%26nbsp%3B(Get-Date%26nbsp%3B-Year%26nbsp%3B2024)%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BPassword%26nbsp%3B%3D%26nbsp%3B'9MPG7j2MAH3fEveE58vxxg0ghjo9sEutitv9jBeyjfqTLpb9sGBhXQSY9yn2'%26nbsp%3B%23Or%26nbsp%3Bgenerate%26nbsp%3Byour%26nbsp%3Bown%2C%26nbsp%3Bavoid%26nbsp%3Bspecial%26nbsp%3Bcharacters%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24credentials%26nbsp%3B%3D%26nbsp%3BNew-Object%26nbsp%3BMicrosoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential%26nbsp%3B-Property%26nbsp%3B%24credProps%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24sp%26nbsp%3B%3D%26nbsp%3BNew-AzAdServicePrincipal%26nbsp%3B-DisplayName%26nbsp%3B%24SQLServerName%26nbsp%3B-PasswordCredential%26nbsp%3B%24credentials%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Grant%26nbsp%3Bthe%26nbsp%3BSPN%26nbsp%3Baccess%26nbsp%3Bto%26nbsp%3Bthe%26nbsp%3BKey%26nbsp%3BVault%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24accessPolicy%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BVaultName%26nbsp%3B%3D%26nbsp%3B%24keyVault.VaultName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BServicePrincipalName%26nbsp%3B%3D%26nbsp%3B%24sp.ApplicationId%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BPermissionsToKeys%26nbsp%3B%3D%26nbsp%3B%40(%22wrapKey%22%2C%22unwrapKey%22%2C%22get%22%2C%22recover%22%2C%22sign%22%2C%22verify%22%2C%22list%22)%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ESet-AzKeyVaultAccessPolicy%26nbsp%3B%40accessPolicy%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Grant%26nbsp%3Byourself%26nbsp%3Bproper%26nbsp%3Baccess%26nbsp%3Bto%26nbsp%3Bthe%26nbsp%3BKey%26nbsp%3BVault%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24accessPolicyUPN%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BVaultName%26nbsp%3B%3D%26nbsp%3B%24keyVault.VaultName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BUserPrincipalName%26nbsp%3B%3D%26nbsp%3B%22tom%40tomwechsler.ch%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BPermissionsToKeys%26nbsp%3B%3D%26nbsp%3B%40(%22create%22%2C%22wrapKey%22%2C%22unwrapKey%22%2C%22get%22%2C%22recover%22%2C%22sign%22%2C%22verify%22%2C%22list%22)%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSTRONG%3ESet-AzKeyVaultAccessPolicy%26nbsp%3B%40accessPolicyUPN%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Ban%26nbsp%3BAzure%26nbsp%3BSQL%26nbsp%3BServer%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24sqlServerParameters%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BResourceGroupName%26nbsp%3B%3D%26nbsp%3B%24sqlAeRG.ResourceGroupName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BLocation%26nbsp%3B%3D%26nbsp%3B%24Location%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BServerName%26nbsp%3B%3D%26nbsp%3B%24SQLServerName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BSqlAdministratorCredentials%26nbsp%3B%3D%26nbsp%3B%24SQLAdminCredentials%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSTRONG%3E%24sqlServer%26nbsp%3B%3D%26nbsp%3BNew-AzSqlServer%26nbsp%3B%40sqlServerParameters%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Ba%26nbsp%3BFirewall%26nbsp%3Brule%26nbsp%3Ballowing%26nbsp%3Byou%26nbsp%3Bto%26nbsp%3Bconnect%26nbsp%3Bwith%26nbsp%3BSSMS%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24sqlFirewallParameters%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BResourceGroupName%26nbsp%3B%3D%26nbsp%3B%24sqlAeRG.ResourceGroupName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BServerName%26nbsp%3B%3D%26nbsp%3B%24sqlServer.ServerName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BFirewallRuleName%26nbsp%3B%3D%26nbsp%3B%22MyIPAddress%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BStartIpAddress%26nbsp%3B%3D%26nbsp%3B%24MyIPAddress%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BEndIpAddress%26nbsp%3B%3D%26nbsp%3B%24MyIPAddress%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSTRONG%3E%24sqlFirewall%26nbsp%3B%3D%26nbsp%3BNew-AzSqlServerFirewallRule%26nbsp%3B%40sqlFirewallParameters%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Ba%26nbsp%3Bdatabase%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Bapplication%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24databaseParameters%26nbsp%3B%3D%26nbsp%3B%40%7B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BResourceGroupName%26nbsp%3B%3D%26nbsp%3B%24sqlAeRG.ResourceGroupName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BServerName%26nbsp%3B%3D%26nbsp%3B%24sqlServer.ServerName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BDatabaseName%26nbsp%3B%3D%26nbsp%3B%24SQLDatabaseName%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BRequestedServiceObjectiveName%26nbsp%3B%3D%26nbsp%3B%22S0%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BSampleName%26nbsp%3B%3D%26nbsp%3B%22AdventureWorksLT%22%26nbsp%3B%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSTRONG%3E%24database%26nbsp%3B%3D%26nbsp%3BNew-AzSqlDatabase%26nbsp%3B%40databaseParameters%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Now%26nbsp%3Bconnect%26nbsp%3Bto%26nbsp%3BSQL%26nbsp%3BDB%26nbsp%3Bwith%26nbsp%3BSQL%26nbsp%3BServer%26nbsp%3BManagement%26nbsp%3BStudio%26nbsp%3B(SSMS)%26nbsp%3Band%26nbsp%3Bconfigure%26nbsp%3BAlways%26nbsp%3BEncrypted%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3ENow%20you%20have%20used%20the%20PowerShell%20to%20create%20Azure%20SQL%20Server%20(Which%20can%20now%20be%20configured%20with%20Always%20Encrypted)!%20Congratulations!%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%23Clean%26nbsp%3Bup%20(when%20you%20no%20longer%20need%20the%20resources)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%3CSTRONG%3ERemove-AzResourceGroup%26nbsp%3B-Name%26nbsp%3B%3C%2FSTRONG%3E%24ResourceGroupName%3C%2FSTRONG%3E%26nbsp%3B%3CSTRONG%3E-Force%3C%2FSTRONG%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EP.S.%26nbsp%3BAll%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3CBR%20%2F%3E%3C%2FA%3E%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1960146%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
MVP

 

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
Clear-Host

 

#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

 

#For this example I am using passwords in the script. This is usually not a good idea, but it's fine for the demo.

 

#Prefix for resources
$prefix = "tw"

#Log into Azure
Connect-AzAccount

#Select the correct subscription

Get-AzContext

Get-AzSubscription

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


#Let's create a SQL Server that we will use Always Encrypted with
$Location = "westeurope"
$id = Get-Random -Minimum 1000 -Maximum 9999
$ResourceGroupName = "$prefix-sql-ae-$id"
$SQLServerName = "$prefix-sql-$id"
$SQLDatabaseName = "Hospital"
$SQLAdmin = "sqladmin"
$SQLAdminPassword = ConvertTo-SecureString -String 'P@ssw0rd999!' -AsPlainText -Force
$SQLAdminCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SQLAdmin,$SQLAdminPassword
 
#We need our public IP (SQL Firewall configuration)

#Create a resource group for the SQL Server
$sqlAeRG = New-AzResourceGroup -Name $ResourceGroupName -Location $Location

#Now we need to create a Key Vault to use with the SQL Server
$keyVaultParameters = @{
    Name = "$prefix-key-vault-$id"
    ResourceGroupName = $sqlAeRG.ResourceGroupName
    Location = $location
    EnabledForDiskEncryption = $true
    EnabledForDeployment = $true
    Sku = "Standard"
}
$keyVault = New-AzKeyVault @keyVaultParameters

#Now create an AAD SPN to use with Console App
Import-Module Az.Resources # Imports the PSADPasswordCredential object
$credProps = @{
    StartDate = Get-Date
    EndDate = (Get-Date -Year 2024)
    Password = '9MPG7j2MAH3fEveE58vxxg0ghjo9sEutitv9jBeyjfqTLpb9sGBhXQSY9yn2' #Or generate your own, avoid special characters
}
$credentials = New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property $credProps
$sp = New-AzAdServicePrincipal -DisplayName $SQLServerName -PasswordCredential $credentials

#Grant the SPN access to the Key Vault
$accessPolicy = @{
    VaultName = $keyVault.VaultName
    ServicePrincipalName = $sp.ApplicationId
    PermissionsToKeys = @("wrapKey","unwrapKey","get","recover","sign","verify","list")
}
 
Set-AzKeyVaultAccessPolicy @accessPolicy

#Grant yourself proper access to the Key Vault
$accessPolicyUPN = @{
    VaultName = $keyVault.VaultName
    UserPrincipalName = "tom@tomwechsler.ch"
    PermissionsToKeys = @("create","wrapKey","unwrapKey","get","recover","sign","verify","list")
}

Set-AzKeyVaultAccessPolicy @accessPolicyUPN

#Create an Azure SQL Server
$sqlServerParameters = @{
    ResourceGroupName = $sqlAeRG.ResourceGroupName
    Location = $Location
    ServerName = $SQLServerName
    SqlAdministratorCredentials = $SQLAdminCredentials
}

$sqlServer = New-AzSqlServer @sqlServerParameters

#Create a Firewall rule allowing you to connect with SSMS
$sqlFirewallParameters = @{
    ResourceGroupName = $sqlAeRG.ResourceGroupName
    ServerName = $sqlServer.ServerName
    FirewallRuleName = "MyIPAddress"
    StartIpAddress = $MyIPAddress
    EndIpAddress = $MyIPAddress
}

$sqlFirewall = New-AzSqlServerFirewallRule @sqlFirewallParameters

#Create a database for the application
$databaseParameters = @{
    ResourceGroupName = $sqlAeRG.ResourceGroupName
    ServerName = $sqlServer.ServerName
    DatabaseName = $SQLDatabaseName
    RequestedServiceObjectiveName = "S0"
    SampleName = "AdventureWorksLT" 
}

$database = New-AzSqlDatabase @databaseParameters

#Now connect to SQL DB with SQL Server Management Studio (SSMS) and configure Always Encrypted
 
Now you have used the PowerShell to create Azure SQL Server (Which can now be configured with Always Encrypted)! Congratulations!
 
#Clean up (when you no longer need the resources)
Remove-AzResourceGroup -Name $ResourceGroupName -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! https://github.com/tomwechsler

0 Replies