-
Notifications
You must be signed in to change notification settings - Fork 47
/
PrepareDevnetTfsDeployment.ps1
103 lines (79 loc) · 2.88 KB
/
PrepareDevnetTfsDeployment.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
[CmdletBinding(DefaultParameterSetName="nossl")]
param(
[Parameter(Mandatory)]
[String]$DomainName,
[Parameter(Mandatory)]
[String]$AdminUsername,
[Parameter(Mandatory)]
[SecureString]$AdminPassword,
[Parameter(Mandatory)]
[String]$KeyVaultResourceGroupName,
[Parameter(Mandatory)]
[String]$KeyVaultName,
[Parameter(Mandatory=$false,ParameterSetName="ssl")]
[String]$CertificatePath,
[Parameter(Mandatory,ParameterSetName="ssl")]
[SecureString]$CertificatePassword,
[Parameter(Mandatory)]
[String]$Location,
[Parameter(Mandatory=$false)]
[String]$OutFile = ".\azuredeploy.parameters.json"
)
if (Test-Path $OutFile) {
throw "Output file already exists. Please delete or rename"
}
#Check if the user is administrator
if (-not [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) {
throw "You must have administrator priveleges to run this script."
}
$azcontext = Get-AzureRmContext
if ([string]::IsNullOrEmpty($azcontext.Account)) {
throw "User not logged into Azure."
}
#Some settings
$DomainAdminPasswordSecretName = "DomainAdminPassword"
$SslCertificateSecretName = "SslCert"
$kvrg = New-AzureRmResourceGroup -Name $KeyVaultResourceGroupName -Location $Location
$kv = New-AzureRmKeyVault -VaultName $KeyVaultName -Location $kvrg.Location -ResourceGroupName $KeyVaultResourceGroupName
Set-AzureRmKeyVaultAccessPolicy -VaultName $KeyVaultName -EnabledForDiskEncryption -EnabledForDeployment -EnabledForTemplateDeployment
#Store domain password in keyvault.
$passwdsecret = Set-AzureKeyVaultSecret -VaultName $KeyVaultName -Name $DomainAdminPasswordSecretName -SecretValue $AdminPassword
$secrets = @()
if (-not [String]::IsNullOrEmpty($CertificatePath)) {
#Upload SSL cert to keyvault
$cer = Import-AzureKeyVaultCertificate -VaultName $KeyVaultName -Name $SslCertificateSecretName -FilePath $CertificatePath -Password $CertificatePassword
$secret = @{
"sourceVault" = @{
"id" = $kv.ResourceId
}
"vaultCertificates" = @(
@{
"certificateUrl" = $cer.SecretId
"certificateStore" = "My"
}
)
}
$secrets = @( $secret )
}
$templateParameters = @{
"domainName" = @{
"value" = $DomainName
}
"adminUsername" = @{
"value" = $AdminUsername
}
"adminPassword" = @{
"reference" = @{
"keyvault" = @{
"id" = $kv.ResourceId
}
"secretName" = $DomainAdminPasswordSecretName
}
}
}
if (-not [String]::IsNullOrEmpty($CertificatePath)) {
$templateParameters.Add("secrets", @{ "value" = $secrets})
$templateParameters.Add("sslThumbprint", @{ "value" = $cer.Thumbprint})
}
$templateParameters | ConvertTo-Json -Depth 10 | Out-File $OutFile
Write-Host "Parameters written to $OutFile."