-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRotate_storage_account_access_keys.ps1
48 lines (35 loc) · 1.41 KB
/
Rotate_storage_account_access_keys.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
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Prefix for resources
$prefix = "tw"
#Basic variables
$id = Get-Random -Minimum 1000 -Maximum 9999
#Log into Azure
Connect-AzAccount
#get list of locations and pick one
Get-AzLocation | select Location
$location = "westeurope"
#Select the correct subscription
Get-AzSubscription -SubscriptionName "MSDN Platforms" | Select-AzSubscription
#create a resource group
$resourceGroup = "$prefix-rg-$id"
New-AzResourceGroup -Name $resourceGroup -Location $location
#create a standard general-purpose storage account
$storageAccountName = "$($prefix)sa$id"
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
#Clean Up
Remove-AzResourceGroup -Name $resourceGroup -Force