-
Notifications
You must be signed in to change notification settings - Fork 3
/
Set-Network.ps1
90 lines (69 loc) · 2.76 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
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 VM Configuration objects (by default it downloads them)")]
$VmSettings = ""
)
$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"
}
$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-AzureRmResource -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 = @{}
$VmSettings | ForEach-Object {
$vmName = $_.imageName
$dnsServer = $_.dnsServer
# Find the VM
$vm = $vms | Where-Object {$_.Name -eq $vmName}
if(-not $vm) {
throw "Can't find VM named $vmName in lab $DevTestLabName in RG $ResourceGroupName"
}
# DANGER: this is implementation specific. Might change if DTL Changes how it stores compute info.
$computeVm = Get-AzureRmResource -ResourceId $vm.Properties.computeId
$computeGroup = $computeVm.ResourceGroupName
$nic = Get-AzureRmNetworkInterface -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 ..."
$ip = $nic.IpConfigurations | ForEach-Object {$_.PrivateIpAddress}
$nicsHash.add($vmName, @{'nic' = $nic; 'dnsServer' = $dnsServer;'ip' = $ip})
} | Out-Null
if($nicsHash.count -eq 0) {
throw "Found no NICS??"
}
# Act on each NIC depending if it's a dns server or not
$nicsHash.Keys | 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(-not $thisServer) {
throw "The DNS server '$dnsName' is not in the lab, hence cannot be set as DNS server for '$_'"
}
$dnsIp = $thisServer.ip
$nic.DnsSettings.DnsServers.Add($dnsIp)
$nic | Set-AzureRmNetworkInterface | Out-Null
Write-Host "$_`t-> $dnsName`t$dnsIp"
}
} | Out-Null
Write-Output "Network setted correctly for $DevTestLabName"