Skip to content

Just another User's blog Site

PowerShell script to encrypt storage accounts with a Keyvault Key

Posted byksanks 5 November 20225 November 2022


try {Set-AzContext -Subscription $env:SUBSCRIPTION
}
catch {}

$Subscription = (Get-AzSubscription | Where-Object Name -eq $env:SUBSCRIPTION).Id

$KV = Get-AzKeyVault | Where-Object {($_.VaultName -notlike "*EXO*" -and $_.VaultName -notlike "*M365*" -and $_.VaultName -notlike "*SPO*") -and $_.VaultName -like "*01"}
$KV = Get-AzKeyVault -VaultName $KV.VaultName

$SAs = Get-AzStorageAccount | Where-Object {$_.Encryption.KeySource -eq 'Microsoft.Storage'}

foreach ($SA in $SAs) {

    $Key = Add-AzKeyVaultKey -VaultName $KV.VaultName `
    -Name ($SA.StorageAccountName.ToUpper() + '-encryption') `
    -Destination 'Software'

    $SA = Set-AzStorageAccount -ResourceGroupName $SA.ResourceGroupName `
    -Name $SA.StorageAccountName `
    -AssignIdentity

    $principalId = $SA.Identity.PrincipalId

    Set-AzKeyVaultAccessPolicy `
    -VaultName $KV.VaultName `
    -ObjectId $principalId `
    -PermissionsToKeys wrapkey,unwrapkey,get `
    -BypassObjectIdValidation

    Set-AzStorageAccount `
    -ResourceGroupName $SA.ResourceGroupName `
    -Name $SA.StorageAccountName `
    -KeyvaultEncryption `
    -KeyName $Key.Name `
    -KeyVaultUri $KV.VaultUri

}
Posted byksanks5 November 20225 November 2022Posted inTechnical Knowledge BaseTags: Azure, PowerShell Script, Storage Account, StorageAccountEncryption

Post navigation

Previous post Previous post:
PowerShell script used to Delete resourcegroup level locks in all your Azure Subscriptions
Next post Next post:
Powershell script to set Azure key vault key auto rotation policy

Recent Posts

  • Python Script to list Azure Inventory for all subscriptions.
  • Powershell script to list the .Net core version in an Azure Virtual Machine using Invoke-AzVmRunCommand
  • Powershell to list Azure Server Backup Report from the Azure Recovery Service Vaults
  • Powershell script to set Azure key vault key auto rotation policy
  • PowerShell script to encrypt storage accounts with a Keyvault Key

Recent Comments

No comments to show.

Archives

  • October 2025
  • February 2023
  • November 2022
  • July 2021
  • June 2021
  • May 2021
  • August 2020
  • May 2020
  • January 2020
  • February 2018
  • January 2018
  • June 2017
  • April 2017
  • December 2016
  • November 2016

Categories

  • Certifications
  • Cook's Corner
  • General View
  • Spiritist
  • Technical Knowledge Base

Recent Posts

  • Python Script to list Azure Inventory for all subscriptions.
  • Powershell script to list the .Net core version in an Azure Virtual Machine using Invoke-AzVmRunCommand
  • Powershell to list Azure Server Backup Report from the Azure Recovery Service Vaults
  • Powershell script to set Azure key vault key auto rotation policy
  • PowerShell script to encrypt storage accounts with a Keyvault Key

Recent Comments

    Proudly powered by WordPress.
    • Home
    • Contact