-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
240 lines (203 loc) · 11.5 KB
/
build.ps1
File metadata and controls
240 lines (203 loc) · 11.5 KB
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
# Import the functions
$ModuleFile = "$ENV:TEMP\GetToTheCloud.psm1"
$Module = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/buzzict/GetToTheCloud-TestLab/main/functions.psm1" -UseBasicParsing).Content | Out-File $ModuleFile
Import-Module $ModuleFile
Connect-AzAccount
# Get the public IP Address from where the script is run
$remoteAddress = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
# checking azure location close by and if eligible
## current connection
# $regions = Get-AZLocation
# $locations = $regions | Select-Object displayName,latitude,longitude | Sort-Object displayName
# $request = (Invoke-WebRequest -Uri https://ipapi.co/$remoteAddress/json).Content | ConvertFrom-Json
# $latitude = $request.lat
# $longitude = $request.lon
# $locations = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/buzzict/GetToTheCloud-TestLab/main/locations.json" -UseBasicParsing).Content | ConvertFrom-Json
# $hash = [ordered]@{
# latitude="$latitude";
# longitude="$longitude";
# locations=@($locations)
# }
# $body = $hash | ConvertTo-Json -Depth 100
# $uri = 'https://azureregion.azurewebsites.net/api/nearestRegionFromIp'
# Invoke-RestMethod -Method Put -Uri $uri -Body $body
# Stop displaying warning messages from the Az module
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
# Import JSON configuration file
$jsonPrompt = Read-Host -Prompt 'Enter the URL of the configuration file'
try {
$Json = Invoke-WebRequest $jsonPrompt -UseBasicParsing |Out-file "$ENV:TEMP\input.json"
$json = Get-Content "$ENV:TEMP\input.json" | ConvertFrom-Json
}
catch {
Write-Host "Configuration file could not be found. Please enter the correct full path"
Exit
}
$username = $json.UserName
$Cred = $json.Password | ConvertTo-SecureString -Force -AsPlainText
$Credential = New-Object -TypeName PSCredential -ArgumentList ($Username, $Cred)
$DomainName = $json.DomainName
$Domain = $DomainName.Split(".")[0]
$DomainUser = $domain + "\" + $Username
$DomainCredential = New-Object -TypeName PSCredential -ArgumentList ($DomainUser, $Cred)
# Check if the resource group is already present. If not, create a new resource group.
try {
Get-AzResourceGroup -Name $json.resourceGroupName -ErrorAction Stop
Write-Host "[WARNING] - Resource group already exists ($($json.resourceGroupName))"
}
catch {
New-AzResourceGroup -Name $json.resourceGroupName -Location $json.locationName | Out-Null
Write-Host "[SUCCESS] - Resource group is created ($($json.resourceGroupName))"
}
# Create a new subnet and Azure Virtual Network when it does not exist
$vnet = Get-AzVirtualNetwork -Name $json.vnetName
if ($vnet) {
Write-Host "[WARNING] - Virtual network already exists ($($json.vnetName))"
}
else {
# Process each subnet that has been defined in the JSON file and add it to the config for the new virtual network
foreach ($subnet in $json.subnets) {
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name $subnet.subnetName -AddressPrefix $subnet.subnetPrefix
Write-Output "[INFO] - Subnet is added to the virtual network configuration ($($subnet.subnetName))"
}
# Create a new virtual network and include the configured subnets from the previous cmdlet
$vnet = New-AzVirtualNetwork -Name $json.vnetName -ResourceGroupName $json.resourceGroupName -Location $json.locationName -AddressPrefix $json.vnetAddressPrefix -Subnet $subnetConfig
Write-Host "[SUCCES] - Virtual network is created ($($json.vnetName))"
}
# Process every machine that is found in the JSON file to speed up the virtual machine deployment
foreach ($machine in $json.machines) {
$vmPublicIP = $machine.vmName + '-ip'
$vmNicName = $machine.vmName + '-nic'
$pip = New-AzPublicIpAddress -Name $vmPublicIP -ResourceGroupName $json.resourceGroupName -Location $json.locationName -AllocationMethod Dynamic
$nic = New-AzNetworkInterface -Name $vmNicName -ResourceGroupName $json.resourceGroupName -Location $json.locationName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
# Create a public IP address, a network interface card and a Network Security Group
switch ($machine.type) {
"DomainController" { $nsg = New-TestLabNSG -LocationName $json.locationName -RemoteAddress $remoteAddress -Type 'DomainController' }
"ExchangeServer" { $nsg = New-TestLabNSG -LocationName $json.locationName -RemoteAddress $remoteAddress -Type 'ExchangeServer' }
"Client" { $nsg = New-TestLabNSG -LocationName $json.locationName -RemoteAddress $remoteAddress -Type 'Client' }
Default {}
}
# Assign Network Security Group to the network interface card
$nic.NetworkSecurityGroup = $nsg
$nic | Set-AzNetworkInterface
# Create a new virtual machine in Microsoft Azure
New-TestLabVM -Credential $credential -VMName $machine.vmName -VMSize $machine.vmSize -NICID $nic.Id -Skus $machine.skus -Offer $machine.offer -ResourceGroupName $json.resourceGroupName -LocationName $json.locationName -PublisherName $machine.publisherName
}
foreach ($machine in $json.machines) {
# Based on the type of the virtual machine, execute a different script extension
switch ($machine.type) {
"DomainController" {
# Add a new script extension to the VM
$fileUri = $json.UrlPSRemote
$script = 'Set-PowerShellRemoting.ps1'
Add-ScriptExtension -FileUri $fileUri -Script $script -ResourceGroupName $json.resourceGroupName -VMName $machine.vmName -LocationName $json.locationName
}
"ExchangeServer" {
# Add a new script extension to the VM
$fileUri = $json.UrlPSRemote
$script = 'Set-PowerShellRemoting.ps1'
Add-ScriptExtension -FileUri $fileUri -Script $script -ResourceGroupName $json.resourceGroupName -VMName $machine.vmName -LocationName $json.locationName
}
"Client" { }
Default {}
}
}
foreach ($machine in $json.machines) {
# Based on the type of the virtual machine, execute a ps1 script via WINRM
switch ($machine.type) {
"DomainController" {
# Add a new script extension to the VM
$VMName = $json.vmname
$Pip = $VMName + "PublicIP"
$fileUri = "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/01-DC-SetupDomainController.ps1"
$IP = (Get-AZPublicIPAddress -Name $Pip).IpAddress
Write-Host "[INFO] Connecting to $($VMName) with IP $IP for installing Domain Controller"
Invoke-Command -Computername $IP -ScriptBlock {
Param ($fileuri)
$OutputFolder = "C:\Temp"
if (Test-Path -path $OutputFolder) {
#do nothing
}
else {
$Location = $OutputFolder.Split("\")
New-Item -Path "$($Location[0])\" -Name $Location[1] -ItemType Directory
}
$Script = (Invoke-WebRequest -Uri $fileUri -UseBasicParsing).Content
$Script | Out-File C:\Temp\script.ps1
powershell C:\temp\script.ps1
Remove-Item C:\Temp\script.ps1 -force
} -Credential $Credential -ArgumentList $fileUri
$FileUri = "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/03-DC-ConfigureActiveDirectory.ps1"
Write-Host "[INFO] Connecting to $($VMName) with IP $IP for Creating Domain structure"
Invoke-Command -Computername $IP -ScriptBlock {
Param ($fileuri)
$OutputFolder = "C:\Temp"
if (Test-Path -path $OutputFolder) {
#do nothing
}
else {
$Location = $OutputFolder.Split("\")
New-Item -Path "$($Location[0])\" -Name $Location[1] -ItemType Directory
}
$Script = (Invoke-WebRequest -Uri $fileUri -UseBasicParsing).Content
$Script | Out-File C:\Temp\script.ps1
powershell C:\temp\script.ps1
Remove-Item C:\Temp\script.ps1 -force
} -Credential $DomainCredential -ArgumentList $fileUri
}
"ExchangeServer" {
# Add a new script extension to the VM
$VMName = $json.Vname
$Pip = $VMName + "PublicIP"
$fileUri = "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/02-EXC-DownloadExchange.ps1"
$IP = (Get-AZPublicIPAddress -Name $Pip).IpAddress
Write-Host "[INFO] Connecting to $($VMName) with IP $IP for downloading Exchange software"
Invoke-Command -Computername $IP -ScriptBlock {
Param ($fileuri)
$OutputFolder = "C:\Temp"
if (Test-Path -path $OutputFolder) {
#do nothing
}
else {
$Location = $OutputFolder.Split("\")
New-Item -Path "$($Location[0])\" -Name $Location[1] -ItemType Directory
}
$Script = (Invoke-WebRequest -Uri $fileUri -UseBasicParsing).Content
$Script | Out-File C:\Temp\script.ps1
powershell C:\temp\script.ps1
Remove-Item C:\Temp\script.ps1 -force
$Download = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/04-EXC-ConfigureExchange.ps1" -UseBasicParsing).Content
$Download | Out-File C:\ExchangeDownload\04-EXC-ConfigureExchange.ps1
$Download = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/Scripts/Replace-OAuthCertificate.ps1" -UseBasicParsing).Content
$Download | Out-File C:\ExchangeDownload\Replace-OAuthCertificate.ps1
$Download = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/Scripts/Run-HybridConfigWizard.ps1" -UseBasicParsing).Content
$Download | Out-File C:\ExchangeDownload\Run-HybridConfigWizard.ps1
$Download = (Invoke-WebRequest -uri "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/Scripts/GetToTheCloudFunctions.psm1" -UseBasicParsing).Content
$Download | Out-File C:\ExchangeDownload\GetToTheCloudFunctions.psm1
} -Credential $Credential -ArgumentList $fileUri
$fileUri = "https://raw.githubusercontent.com/GetToThe-Cloud/GetToTheCloud-Lab/main/011-EXC-NetworkSettings.ps1"
$EXIP = $IP
Write-Host "[INFO] Connecting to $($VMName) with IP $IP for setting Network Settings Exchange server"
Invoke-Command -Computername $IP -ScriptBlock {
Param ($fileuri)
$Script = ""
$OutputFolder = "C:\Temp"
if (Test-Path -path $OutputFolder) {
#do nothing
}
else {
$Location = $OutputFolder.Split("\")
New-Item -Path "$($Location[0])\" -Name $Location[1] -ItemType Directory
}
$Script = (Invoke-WebRequest -Uri $fileUri -UseBasicParsing).Content
$Script | Out-File C:\Temp\script.ps1
powershell C:\temp\script.ps1
Remove-Item C:\Temp\script.ps1 -force
} -Credential $Credential -ArgumentList $fileUri
Write-Host "[INFO] Restarting $($Vmname) now"
Restart-AZVM -ResourceGroupName $ResourceGroupName -Name $VMName
}
"Client" { }
Default {}
}
}