|
1 | 1 | <# .SYNOPSIS
|
2 | 2 | EFLOW Windows Server provisioning Script
|
3 | 3 | .DESCRIPTION
|
4 |
| - THIS SCRIPT IS NOT RECOMMENDED TO BE USED AS IS, BUT SHOULD BE SEEN AS A COLLECTION OF STEPS TO BE RUN MANUALLY |
5 |
| - TO GET EFLOW UP AND RUNNING. |
6 |
| - This script automatically install Edge For Linux On Windows (EFLOW) on a new Windows Server VM |
7 |
| - The script might trigger a restart and might need to be rerun after a restart |
| 4 | + This script automatically installs Edge For Linux On Windows (EFLOW) on a new Windows Server VM. |
| 5 | + This script assumes that the Windows VM has Hyper-V already installed. |
| 6 | + The script accepts the following parameters: |
| 7 | + - iotEdgeDeviceConnectionString: (required) the iotedge device connection string. |
| 8 | + - switchName: (optional) the switch name. |
| 9 | + - startEflowIpRange: (optional) start of the IP range of eflow. |
| 10 | + - internalPort: (optional) the internal port of the Net Nat Static Mapping. |
| 11 | + - externalPort: (optional) the external port of the Net Nat Static Mapping. |
8 | 12 | .NOTES
|
9 | 13 | Author :
|
10 |
| - Mikhail Chatillon - chmikhai@microft.com |
| 14 | + Mikhail Chatillon - chmikhai@microsoft.com |
11 | 15 | Daniele Antonio Maggio - daniele.maggio@microsoft.com
|
| 16 | + Nora Abi Akar - noraabiakar@microsoft.com |
12 | 17 | #>
|
13 | 18 |
|
14 | 19 | param
|
15 | 20 | (
|
16 |
| - [string]$switchName="EFLOW Switch", |
17 |
| - [string]$startEflowIpRange=100, |
18 |
| - [string]$endEflowIpRange=200, |
19 | 21 | [string]$iotEdgeDeviceConnectionString,
|
| 22 | + [string]$switchName, |
| 23 | + [string]$startEflowIpRange=100, |
20 | 24 | [int]$internalPort=5000,
|
21 | 25 | [int]$externalPort=5000
|
22 | 26 | )
|
23 | 27 |
|
24 |
| -# Enable Windows Features |
25 |
| -if ((Get-WindowsFeature -Name "DHCP").Installed -eq $false) |
26 |
| -{ |
27 |
| - Install-WindowsFeature -Name DHCP -IncludeManagementTools |
28 |
| -} |
29 |
| -if ((Get-WindowsFeature -Name "Hyper-V").Installed -eq $false) |
| 28 | +# Verify dependencies |
| 29 | +if (!$iotEdgeDeviceConnectionString) |
30 | 30 | {
|
31 |
| - Install-WindowsFeature -Name Hyper-V |
32 |
| - Install-WindowsFeature -Name Hyper-V-PowerShell |
33 |
| - Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -All -NoRestart |
34 |
| - Write-Host "Restarting the computer in 30 seconds to finish Hyper-V installation" |
35 |
| - Start-Sleep -Seconds 30 |
36 |
| - Restart-Computer |
| 31 | + throw "IoT Edge Device Connection String not provided." |
37 | 32 | }
|
38 |
| -if ((Get-WindowsFeature -Name "DHCP").Installed -eq $false) |
| 33 | + |
| 34 | +if (!$switchName) |
39 | 35 | {
|
40 |
| - throw "DHCP not correctly installed." |
| 36 | + throw "Switch Name not provided." |
41 | 37 | }
|
| 38 | + |
42 | 39 | if ((Get-WindowsFeature -Name "Hyper-V").Installed -eq $false)
|
43 | 40 | {
|
44 | 41 | throw "Hyper-V not correctly installed."
|
45 | 42 | }
|
46 | 43 |
|
47 |
| -# Create the networking |
48 |
| -if(Get-VMSwitch -Name $switchName -ErrorAction SilentlyContinue) |
| 44 | +if(!(Get-VMSwitch -Name $switchName -ErrorAction SilentlyContinue)) |
49 | 45 | {
|
50 |
| - throw "Switch already existing"; |
| 46 | + throw "Switch $switchName not found"; |
51 | 47 | }
|
52 | 48 |
|
53 |
| -New-VMSwitch -Name $switchName -SwitchType internal |
54 |
| -Write-Host "Sleeping for 30sec before continuing for propagating the VMSwitch" |
55 |
| -Start-Sleep -Seconds 30 |
56 |
| -Write-Host "Finished waiting for propagating the VMSwitch" |
| 49 | +# Create Nat |
| 50 | +Write-Host "Creating Nat" |
| 51 | + |
57 | 52 | $netAdapterIfIndex=(Get-NetAdapter -Name "*$switchName*").ifIndex
|
58 | 53 | $netAdapterIpAddress=Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $netAdapterIfIndex
|
59 | 54 | $netAdapterIp=$netAdapterIpAddress.IPAddress
|
60 | 55 | $ipAddressFamily=$netAdapterIp.Substring(0, $netAdapterIp.lastIndexOf('.')+1)
|
61 |
| -$gwIpCounter=1 |
62 |
| -$gwIp=$ipAddressFamily+$gwIpCounter |
63 |
| - |
64 |
| -while(!(get-NetIPAddress -IpAddress $gwIp -ErrorAction SilentlyContinue)) |
65 |
| -{ |
66 |
| - $gwIpCounter++ |
67 |
| - if($gwIpCounter>9) |
68 |
| - { |
69 |
| - throw "All the IPs in the subnet range 1-9 on $ipAddressFamily were already taken"; |
70 |
| - } |
71 |
| - |
72 |
| - $gwIp=$ipAddressFamily+$gwIpCounter |
73 |
| -} |
74 |
| - |
| 56 | +$gwIp=$ipAddressFamily+1 |
75 | 57 | $natIp=$ipAddressFamily+0
|
76 |
| - |
77 |
| -New-NetIPAddress -IPAddress $gwIp -PrefixLength 24 -InterfaceIndex $netAdapterIfIndex |
78 |
| - |
79 |
| -if(Get-NetNat -Name "$switchName" -ErrorAction SilentlyContinue) |
80 |
| -{ |
81 |
| - throw "Net Nat with name $switchName already existing"; |
82 |
| -} |
| 58 | +$startIp=$ipAddressFamily+$startEflowIpRange |
83 | 59 |
|
84 | 60 | New-NetNat -Name "$switchName" -InternalIPInterfaceAddressPrefix "$natIp/24"
|
85 | 61 |
|
86 |
| -#Install DHCP |
87 |
| -netsh dhcp add securitygroups |
88 |
| -Restart-Service dhcpserver |
89 |
| -$startIp=$ipAddressFamily+$startEflowIpRange |
90 |
| -$endIp=$ipAddressFamily+$endEflowIpRange |
91 |
| -Add-DhcpServerV4Scope -Name "AzureIoTEdgeScope" -StartRange $startIp -EndRange $endIp -SubnetMask 255.255.255.0 -State Active |
92 |
| -Set-DhcpServerV4OptionValue -ScopeID $natIp -Router $gwIp |
93 |
| -Restart-Service dhcpserver |
| 62 | +# Install Eflow |
| 63 | +Write-Host "Installing Eflow" |
94 | 64 |
|
95 |
| -# install Eflow |
96 | 65 | Set-ExecutionPolicy -ExecutionPolicy AllSigned -Force
|
97 | 66 | $msiPath = $([io.Path]::Combine($env:TEMP, 'AzureIoTEdge.msi'))
|
98 | 67 | $ProgressPreference = 'SilentlyContinue'
|
99 | 68 | Invoke-WebRequest "https://aka.ms/AzEflowMSI" -OutFile $msiPath
|
| 69 | + |
100 | 70 | Start-Process -Wait msiexec -ArgumentList "/i","$([io.Path]::Combine($env:TEMP, 'AzureIoTEdge.msi'))","/qn"
|
101 |
| -Deploy-Eflow -acceptEula 'yes' -acceptOptionalTelemetry 'yes' -vswitchName $switchName -ip4Address $startIp -ip4GatewayAddress $gwIp -vswitchType 'Internal' |
102 |
| -if($iotEdgeDeviceConnectionString){ |
103 |
| - Provision-EflowVm -provisioningType ManualConnectionString -devConnString "$iotEdgeDeviceConnectionString" |
104 |
| -} |
| 71 | +Deploy-Eflow -acceptEula 'yes' -acceptOptionalTelemetry 'yes' -vswitchName $switchName -ip4Address $startIp -ip4GatewayAddress $gwIp -vswitchType 'Internal' -ip4PrefixLength 24 |
| 72 | +Provision-EflowVm -provisioningType ManualConnectionString -devConnString "$iotEdgeDeviceConnectionString" |
| 73 | + |
| 74 | +# Add static mapping |
| 75 | +Write-Host "Creating static mapping" |
105 | 76 |
|
106 | 77 | if(!(Get-NetNatStaticMapping -NatName "$switchName" -ErrorAction SilentlyContinue))
|
107 | 78 | {
|
108 | 79 | Add-NetNatStaticMapping -ExternalIPAddress "0.0.0.0/0" -ExternalPort $externalPort -Protocol TCP -InternalIPAddress "$startip" -InternalPort $internalPort -NatName $switchName
|
109 | 80 | }
|
| 81 | + |
| 82 | +# Set DNS server |
| 83 | +Write-Host "Set DNS server" |
| 84 | + |
| 85 | +Set-EflowVmDNSServers -dnsServers 168.63.129.16 |
0 commit comments