-
Notifications
You must be signed in to change notification settings - Fork 61
/
Secure_webserver_VM_Azure_TLS_SSL.ps1
95 lines (76 loc) · 3.04 KB
/
Secure_webserver_VM_Azure_TLS_SSL.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
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Basic variables
$resourceGroup = "myResourceGroupSecureWeb"
$prefix = "tw"
$location = "westeurope"
$id = Get-Random -Minimum 1000 -Maximum 9999
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzSubscription -SubscriptionName "MSDN Platforms" | Select-AzSubscription
#Create a resource group
New-AzResourceGroup -ResourceGroupName $resourceGroup -Location $location
#Create an Azure Key Vault
$keyvaultName="$($prefix)keyvault$id"
New-AzKeyVault -VaultName $keyvaultName `
-ResourceGroup $resourceGroup `
-Location $location `
-EnabledForDeployment
#Generate a certificate and store in Key Vault
$policy = New-AzKeyVaultCertificatePolicy `
-SubjectName "CN=www.tomwechsler.ch" `
-SecretContentType "application/x-pkcs12" `
-IssuerName Self `
-ValidityInMonths 12
Add-AzKeyVaultCertificate `
-VaultName $keyvaultName `
-Name "mycert" `
-CertificatePolicy $policy
#Set an administrator username and password for the VM
$cred = Get-Credential
# Create a VM
New-AzVm `
-ResourceGroupName $resourceGroup `
-Name "myVM" `
-Location $location `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-SecurityGroupName "myNetworkSecurityGroup" `
-PublicIpAddressName "myPublicIpAddress" `
-Credential $cred `
-OpenPorts 443
# Use the Custom Script Extension to install IIS
Set-AzVMExtension -ResourceGroupName $resourceGroup `
-ExtensionName "IIS" `
-VMName "myVM" `
-Location $location `
-Publisher "Microsoft.Compute" `
-ExtensionType "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-SettingString '{"commandToExecute":"powershell Install-WindowsFeature Web-Server -IncludeManagementTools"}'
#Add a certificate to VM from Key Vault
$certURL=(Get-AzKeyVaultSecret -VaultName $keyvaultName -Name "mycert").id
$vm=Get-AzVM -ResourceGroupName $resourceGroup -Name "myVM"
$vaultId=(Get-AzKeyVault -ResourceGroupName $resourceGroup -VaultName $keyVaultName).ResourceId
$vm = Add-AzVMSecret -VM $vm -SourceVaultId $vaultId -CertificateStore "My" -CertificateUrl $certURL
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
#Configure IIS to use the certificate
$PublicSettings = '{
"fileUris":["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/secure-iis.ps1"],
"commandToExecute":"powershell -ExecutionPolicy Unrestricted -File secure-iis.ps1"
}'
Set-AzVMExtension -ResourceGroupName $resourceGroup `
-ExtensionName "IIS" `
-VMName "myVM" `
-Location $location `
-Publisher "Microsoft.Compute" `
-ExtensionType "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-SettingString $publicSettings
#Test the secure web app
Get-AzPublicIPAddress -ResourceGroupName $resourceGroup -Name "myPublicIPAddress" | select "IpAddress"
#Now you can open a web browser and enter https://<myPublicIP>
#Clean Up
Remove-AzResourceGroup -Name "myResourceGroupSecureWeb" -Force