-
Notifications
You must be signed in to change notification settings - Fork 19
/
Create_VNet_Peering.ps1
74 lines (58 loc) · 2.13 KB
/
Create_VNet_Peering.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
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzContext
Get-AzSubscription
Get-AzSubscription -SubscriptionName "Visual Studio Enterprise-Abonnement" | Select-AzSubscription
#Create a resource group
New-AzResourceGroup -ResourceGroupName myResourceGroup -Location WestEurope
#Create a virtual network
$virtualNetwork1 = New-AzVirtualNetwork `
-ResourceGroupName myResourceGroup `
-Location WestEurope `
-Name myVirtualNetwork1 `
-AddressPrefix 10.0.0.0/16
#Create a subnet configuration
$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
-Name Subnet1 `
-AddressPrefix 10.0.0.0/24 `
-VirtualNetwork $virtualNetwork1
#Write the subnet configuration to the virtual network
$virtualNetwork1 | Set-AzVirtualNetwork
#Create a virtual network with a 10.1.0.0/16
$virtualNetwork2 = New-AzVirtualNetwork `
-ResourceGroupName myResourceGroup `
-Location WestEurope `
-Name myVirtualNetwork2 `
-AddressPrefix 10.1.0.0/16
#Create the subnet configuration.
$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
-Name Subnet1 `
-AddressPrefix 10.1.0.0/24 `
-VirtualNetwork $virtualNetwork2
#Write the subnet configuration to the virtual network.
$virtualNetwork2 | Set-AzVirtualNetwork
#Create a peering, the following example peers myVirtualNetwork1 to myVirtualNetwork2
Add-AzVirtualNetworkPeering `
-Name myVirtualNetwork1-myVirtualNetwork2 `
-VirtualNetwork $virtualNetwork1 `
-RemoteVirtualNetworkId $virtualNetwork2.Id
=> PeeringState is Initiated
Add-AzVirtualNetworkPeering `
-Name myVirtualNetwork2-myVirtualNetwork1 `
-VirtualNetwork $virtualNetwork2 `
-RemoteVirtualNetworkId $virtualNetwork1.Id
#Confirm that the peering state
Get-AzVirtualNetworkPeering `
-ResourceGroupName myResourceGroup `
-VirtualNetworkName myVirtualNetwork1 `
| Select PeeringState
Get-AzVirtualNetworkPeering `
-ResourceGroupName myResourceGroup `
-VirtualNetworkName myVirtualNetwork2 `
| Select PeeringState
#Clean Up
Remove-AzResourceGroup -Name myResourceGroup -Force