forked from robotechredmond/Azure-PowerShell-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AureRM - Azure Key Vault Snippets.ps1
245 lines (181 loc) · 5.78 KB
/
AureRM - Azure Key Vault Snippets.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Sign-in with Azure account credentials
Login-AzureRmAccount
# Select Azure Subscription
$subscriptionId =
(Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru).SubscriptionId
Select-AzureRmSubscription `
-SubscriptionId $subscriptionId
# Select Azure Resource Group
$rgName =
(Get-AzureRmResourceGroup |
Out-GridView `
-Title "Select an Azure Resource Group ..." `
-PassThru).ResourceGroupName
$rg =
Get-AzureRmResourceGroup `
-Name $rgName
# Create New Key Vault
# To create via ARM template - https://github.com/Azure/azure-quickstart-templates/tree/master/101-key-vault-create
$vaultName = 'MyDemoVault01'
$vaultSKU = 'Premium'
New-AzureRmKeyVault `
-VaultName $vaultName `
-ResourceGroupName $rgName `
-Location $rg.Location `
-SKU $vaultSKU
# Get Key Vault
$vault =
Get-AzureRmKeyVault `
-VaultName $vaultName `
-ResourceGroupName $rgName
# Show current properties of Key Vault
$vault | Format-List
# Set Azure Key Vault Access Policy for ARM Template Deployments
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-EnabledForTemplateDeployment
# Set Azure Key Vault Access Policy for ARM Compute xRP Deployments
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-EnabledForDeployment
# Set Azure Key Vault Access Policy for SQL TDE
# For details - https://msdn.microsoft.com/en-us/library/dn198405(v=sql.120).aspx
# Video walk-through - https://channel9.msdn.com/Shows/TechNet+Radio/TechNet-Radio-Part-23-Building-Your-Hybrid-Cloud-Azure-Key-Vault
$spName1 = '<aad-client-id1>' # Service Principal for SQL Server Admin
$spName2 = '<aad-client-id2>' # Service Principal for SQL Server DB Engine
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-ServicePrincipalName $spName1 `
-PermissionsToKeys create,get,list,wrapKey,unwrapKey
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-ServicePrincipalName $spName2 `
-PermissionsToKeys get,list,wrapKey,unwrapKey
# Store password credentials in Key Vault
# Sample template - https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-secure-password
$secret =
Read-Host `
-Prompt 'Enter password' `
-AsSecureString
$secretName = 'ITSecret'
Set-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName `
-SecretValue $secret
Get-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName
# Store certificate in Key Vault
# Sample template - https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-winrm-keyvault-windows
# Details - http://blogs.technet.com/b/kv/archive/2015/07/14/vm_2d00_certificates.aspx
$certFilename = '.\SampleCert.pfx'
$certPassword =
Read-Host `
-Prompt 'Enter password'
$fileContentBytes =
get-content $certFileName -Encoding Byte
$fileContentEncoded =
[System.Convert]::ToBase64String($fileContentBytes)
$jsonObject = @"
{
"data": "$filecontentencoded",
"dataType" :"pfx",
"password": "$certPassword"
}
"@
$jsonObjectBytes =
[System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$jsonEncoded =
[System.Convert]::ToBase64String($jsonObjectBytes)
$secret =
ConvertTo-SecureString `
-String $jsonEncoded `
-AsPlainText `
-Force
$secretName = 'SampleCert'
Set-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName `
-SecretValue $secret
Get-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName
# Enable Disk Encryption for IaaS VMs
# Sample Template - https://github.com/Azure/azure-quickstart-templates/tree/master/201-encrypt-create-new-vm-gallery-image/
# Details - http://blogs.msdn.com/b/azuresecurity/archive/2015/11/17/explore-azure-disk-encryption-with-azure-powershell.aspx
$aadClientID =
'<aad-client-id>'
$aadClientSecret =
'<aad-client-secret>'
$vaultUri =
$vault.VaultUri
$vaultResourceId =
$vault.ResourceId
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-ServicePrincipalName $aadClientID `
-PermissionsToKeys all `
-PermissionsToSecrets all
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $vaultName `
-EnabledForDiskEncryption
$rgName =
(Get-AzureRmResourceGroup |
Out-GridView `
-Title "Select an Azure Resource Group ..." `
-PassThru).ResourceGroupName
$rg =
Get-AzureRmResourceGroup `
-Name $rgName
$vmName =
(Get-AzureRmVm `
-ResourceGroupName $rgName).Name |
Out-GridView `
-Title "Select an Azure VM ..." `
-PassThru
$vm =
Get-AzureRmVm `
-ResourceGroupName $rgName `
-Name $vmName
$vm | Start-AzureRmVm
$vmStatus =
(Get-AzureRmVm `
-ResourceGroupName $rgName `
-Name $vmName `
-Status).Statuses
$vmStatus[-1]
Set-AzureRmVMDiskEncryptionExtension `
-ResourceGroupName $rgName `
-VMName $vmName `
-AadClientID $aadClientID `
-AadClientSecret $aadClientSecret `
-DiskEncryptionKeyVaultUrl $vaultUri `
-DiskEncryptionKeyVaultId $vaultResourceId `
-VolumeType All `
-Force
Get-AzureRmVMDiskEncryptionStatus `
-ResourceGroupName $rgName `
-VMName $vmName
# Enable Logging for Key Vault
$saName = $vaultName.ToLower() + "logs"
$sa =
New-AzureRmStorageAccount `
-ResourceGroupName $rgName `
-Name $saName `
-Type Standard_LRS `
-Location $rg.Location
Set-AzureRmDiagnosticSetting `
-ResourceId $vault.ResourceId `
-StorageAccountId $sa.Id `
-Enabled $true `
-Categories AuditEvent
$container =
'insights-logs-auditevent'
$logs =
Get-AzureStorageBlob `
-Container $container `
-Context $sa.Context
$logs | Get-AzureStorageBlobContent -Destination '.' -Force