-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-AzureCredentialVault.ps1
132 lines (120 loc) · 5.77 KB
/
New-AzureCredentialVault.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
function New-AzureCredentialVault {
<#
.SYNOPSIS
Creates a new Azure Credential Vault
.DESCRIPTION
Creates a new Azure Credential Vault.
.PARAMETER SubscriptionID
The Subscription ID of the subscription for your vault. You can find the subscription ID by running Get-AzureSubscription
.PARAMETER Credential
You need to supply valid credentials for the subscription. This is the only credentials you ever need to remember
.PARAMETER ResourceGroupName
Name of the resource group you keep the Key Vault in
.PARAMETER Location
The location for your resource group
.PARAMETER StorageAccountName
Name of the Storage Account for the Key Vault
.PARAMETER VaultName
The name of your Key Vault
.EXAMPLE
New-AzureCredentialVault -SubscriptionID 3ec04d29-9a0a-49ef-bdec-646f4575650e -Credential $credential -ResourceGroupName MyKeyVault -Location westeurope -StorageAccountName mykeyvaults -VaultName MyKeysForWork -Verbose
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0,HelpMessage="Supply SubscriptionID")]
[guid]$SubscriptionID,
[Parameter(Mandatory=$true, Position=1,HelpMessage="Supply valid credentials for the subscription")]
[PSCredential]$Credential,
[Parameter(Mandatory=$true, Position=2,HelpMessage="Supply the resource group name to use")]
$ResourceGroupName,
[Parameter(Mandatory=$true, Position=3,HelpMessage="Supply a location for the resource group")]
$Location,
[Parameter(Mandatory=$true, Position=4,HelpMessage="Supply the storage account name to use")]
$StorageAccountName,
[Parameter(Mandatory=$true, Position=5,HelpMessage="Supply the vault name")]
$VaultName,
$SkuName = 'Standard_LRS',
$TableName = 'keylinks',
$PartitionKey = 'Private'
)
begin {
$VPreference = $VerbosePreference
try {
Write-Verbose "Logging in as $($Credential.UserName) to $SubscriptionID"
Login-AzureRmAccount -Credential $Credential -SubscriptionId $SubscriptionID -ErrorAction stop | Out-Null
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
$CurrentSubscription = Get-AzureRmContext
Write-Verbose "Current Subscription ID: $($CurrentSubscription.Subscription.SubscriptionId)"
}
process {
try {
# Next command loads the Azure module. Pausing Verbose
$ResourceGroup = Get-AzureRmResourceGroup $ResourceGroupName -ErrorAction stop -Verbose SilentlyContinue
Write-Verbose "Found Resource Group: $($ResourceGroup.ResourceGroupName)"
}
catch {
try {
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location | Out-Null
Write-Verbose -Message "$ResourceGroupName has been created"
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
}
try {
$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction Stop
Write-Verbose "Found Storage Account: $($StorageAccount.StorageAccountName)"
}
catch {
try {
$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -SkuName $SkuName -Location $Location -ErrorAction Stop
Write-Verbose "$StorageAccountName has been created in $ResourceGroupName"
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
}
try {
$KeyVault = Get-AzureRmKeyVault -ResourceGroupName $ResourceGroupName -VaultName $VaultName -ErrorAction Stop
if ($KeyVault -match $null){throw [System.IO.FileNotFoundException] "$VaultName not found."}
Write-Verbose "Found Key Vault: $($KeyVault.VaultName)"
}
catch {
try {
$isRegistered = Get-AzureRmResourceProvider -ProviderNamespace 'Microsoft.KeyVault'
if ($isRegistered.RegistrationState -contains 'Unregistered') {
Write-Verbose "Register Key Vault for your Subscription"
Register-AzureRmResourceProvider -ProviderNamespace "Microsoft.KeyVault" | Out-Null
}
New-AzureRmKeyVault -ResourceGroupName $ResourceGroupName -VaultName $VaultName -Location $Location -ErrorAction Stop | Out-Null
Write-Verbose "$VaultName has been created in $ResourceGroupName"
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
}
try {
$Table = Get-AzureStorageTable -Name $TableName -Context $StorageAccount.Context -ErrorAction Stop
Write-Verbose -Message "Found table: $($Table.Name)"
}
catch {
try {
New-AzureStorageTable -Name $TableName -Context $StorageAccount.Context -ErrorAction Stop | Out-Null
Write-Verbose -Message "$TableName has been created in $StorageAccountName"
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
}
}
end {
}
}