Memutar kunci akses akun penyimpanan dengan PowerShell

Skrip ini membuat akun Azure Storage, menampilkan kunci akses utama akun penyimpanan baru, lalu memperbarui (memutar) kunci.

Sampel ini memerlukan Azure PowerShell. Jalankan Get-Module -ListAvailable Az untuk menemukan versinya. Jika Anda perlu menginstal atau meningkatkan, lihat Menginstal modul Azure PowerShell.

Jalankan cmdlet Connect-AzAccount untuk tersambung ke Azure.

Jika Anda tidak memiliki langganan Azure, buat akun gratis Azure sebelum memulai.

Skrip sampel

# this script will show how to rotate one of the access keys for a storage account

# get list of locations and pick one
Get-AzLocation | select Location

# save the location you want to use  
$location = "eastus"

# create a resource group
$resourceGroup = "rotatekeystestrg"
New-AzResourceGroup -Name $resourceGroup -Location $location 

# create a standard general-purpose storage account 
$storageAccountName = "contosotestkeys"
New-AzStorageAccount -ResourceGroupName $resourceGroup `
  -Name $storageAccountName `
  -Location $location `
  -SkuName Standard_LRS `

# retrieve the first storage account key and display it 
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]

Write-Host "storage account key 1 = " $storageAccountKey

# re-generate the key
New-AzStorageAccountKey -ResourceGroupName $resourceGroup `
    -Name $storageAccountName `
    -KeyName key1

# retrieve it again and display it 
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
Write-Host "storage account key 1 = " $storageAccountKey

Membersihkan penyebaran

Jalankan perintah berikut untuk menghapus grup sumber daya, akun penyimpanan, dan semua sumber daya terkait.

Remove-AzResourceGroup -Name rotatekeystestrg

Penjelasan skrip

Skrip ini menggunakan perintah berikut untuk membuat akun penyimpanan dan mengambil dan memutar salah satu kunci aksesnya. Setiap item dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
Get-AzLocation Mendapatkan semua lokasi dan penyedia sumber daya yang didukung untuk setiap lokasi.
New-AzResourceGroup Membuat grup sumber daya Azure.
New-AzStorageAccount Membuat Akun penyimpanan.
Get-AzStorageAccountKey Mendapatkan kunci akses untuk akun Azure Storage.
New-AzStorageAccountKey Membuat ulang kunci akses untuk akun Azure Storage.

Langkah berikutnya

Untuk informasi selengkapnya tentang modul Azure PowerShell, lihat dokumentasi Azure PowerShell.

Sampel skrip PowerShell penyimpanan tambahan dapat ditemukan dalam sampel PowerShell untuk penyimpanan Azure Blob.