forked from lucabol/DTLCustomImagesLab
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSet-Network.ps1
154 lines (116 loc) · 5.27 KB
/
Set-Network.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
param
(
[Parameter(Mandatory=$true, HelpMessage="The Name of the new DevTest Lab")]
[string] $DevTestLabName,
[Parameter(Mandatory=$true, HelpMessage="The Name of the resource group to create the lab in")]
[string] $ResourceGroupName,
[Parameter(Mandatory=$false, HelpMessage="The VMs we're adjusting the network settings")]
$VMsToConfigure = "",
[Parameter(Mandatory=$false, HelpMessage="The VM Configuration objects (by default it downloads them)")]
$VmSettings = "",
[parameter(Mandatory=$false, HelpMessage="Public=separate IP Address, Shared=load balancers optimizes IP Addresses, Private=No public IP address.")]
[string] $LabIpConfig = "Public"
)
$ErrorActionPreference = 'Stop'
. "./Utils.ps1"
if(-not $VmSettings) {
$VmSettings = & "./Import-VmSetting" -StorageAccountName $StorageAccountName -StorageContainerName $StorageContainerName -StorageAccountKey $StorageAccountKey
}
if(-not $VmSettings) {
throw "VmSettings can't be null or empty"
}
if (-not $VMsToConfigure) {
# If $VMsToConfigure is empty, we assume we're updating all VMs
$VMsToConfigure = $VmSettings
}
$VMsToConfigureNames = $VMsToConfigure | Select -ExpandProperty imagename
$scriptFolder = $PWD
Write-Host "Starting DNS setting ..."
if(-not $scriptFolder) {
throw "Script folder is null"
}
# Get all VMs in lab expanding properties to get to compute VM
$vms = Get-AzResource -ResourceType "Microsoft.DevTestLab/labs/virtualMachines" -ResourceGroupName $ResourceGroupName -ExpandProperties -Name "$DevTestLabName/"
# Needs to run first through all the vms to get the private ip for the dns servers
$nicsHash = @{}
# Also track, for the Shared config, the VM size & largest IP
$nicsHashBySize = @{}
$VmSettings | ForEach-Object {
$vmName = $_.imageName
$dnsServer = $_.dnsServer
# Find the VM
$vm = $vms | Where-Object {$_.Name -eq $vmName}
# Only proceed if we have the VM
if($vm) {
# DANGER: this is implementation specific. Might change if DTL Changes how it stores compute info.
$computeVm = Get-AzResource -ResourceId $vm.Properties.computeId
$computeGroup = $computeVm.ResourceGroupName
$nic = Get-AzNetworkInterface -Name $vmName -ResourceGroupName $computeGroup
if(-not $nic) {
throw "Can't find the NIC named $vmName in the compute group $computeGroup"
}
Write-Host "Found the NIC for $vmName ..."
# Clear any existing DNS settings, in case this is the 2nd+ time through this script
# since we reset them all anyway, for all the VMs in the lab
if ($nic.DnsSettings.DnsServers.Count -gt 0) {
$nic.DnsSettings.DnsServers.Clear()
}
$ip = $nic.IpConfigurations | ForEach-Object {$_.PrivateIpAddress}
# Add the network details for all the VMs into the list
$nicsHash.add($vmName, @{'nic' = $nic; 'dnsServer' = $dnsServer;'ip' = $ip})
# Handle Shared IPs based on VM Size since that's how DTL groups them into availability sets
if ($LabIpConfig -eq "Shared") {
if ($nicsHashBySize.Keys | Where-Object {$vm.Properties.size -eq $_}) {
$nicsHashBySize[$vm.Properties.size].Add(@{'nic' = $nic; 'dnsServer' = $dnsServer;'ip' = $ip})
}
else {
$list = New-Object System.Collections.ArrayList
$list.Add(@{'nic' = $nic; 'dnsServer' = $dnsServer;'ip' = $ip})
$nicsHashBySize.Add($vm.Properties.size, $list)
}
}
}
} | Out-Null
if($nicsHash.count -eq 0) {
throw "Found no network cards in the lab? Aborting, this is a critical error"
}
# Act on each NIC depending if it's a dns server or not
$nicsHash.Keys | Where-Object {$VMsToConfigureNames -contains $_} | ForEach-Object {
$value = $nicsHash[$_]
$isDns = -not $value.dnsServer
$nic = $value.nic
if($isDns) {
$nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
Write-Host "$_`t-> static allocation"
} else {
$dnsName = $value.dnsServer
$thisServer = $nicsHash[$dnsName]
if($thisServer) {
$dnsIp = $thisServer.ip
$nic.DnsSettings.DnsServers.Add($dnsIp)
Write-Host "$_`t-> $dnsName`t$dnsIp"
}
else {
Write-Host "The DNS server '$dnsName' is not in the lab, hence cannot be set as DNS server for '$_'" -ForegroundColor Yellow
}
}
if ($LabIpConfig -ne "Shared") {
# Add the general DNS server to enable windows update - 168.63.129.16
$nic.DnsSettings.DnsServers.Add("168.63.129.16")
}
$nic | Set-AzNetworkInterface | Out-Null
} | Out-Null
# At the end, if we are using Shared IPs we need to find
# Each VM with the highest private IP address within each group
# of sizes and add the DNS server for Windows Update only once
if ($LabIpConfig -eq "Shared") {
foreach ($groupName in $nicsHashBySize.Keys) {
Write-Host "Adding 168.63.129.16 to availability set: $groupName"
# Get the net card with the highest IP
$nic = ($nicsHashBySize[$groupName] | Sort-Object -Property @{Expression={[int] ($_.ip.Split('.') | Select -Last 1)}; Descending=$true} | Select -First 1).nic
# Add the general DNS server to enable windows update - 168.63.129.16
$nic.DnsSettings.DnsServers.Add("168.63.129.16")
$nic | Set-AzNetworkInterface | Out-Null
}
}
Write-Output "Network configuration completed for '$DevTestLabName' DevTest Lab"