You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/virtual-network/tutorial-connect-virtual-networks-powershell.md
+97-70Lines changed: 97 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,24 @@ author: asudbring
6
6
ms.service: virtual-network
7
7
ms.topic: how-to
8
8
ms.tgt_pltfrm: virtual-network
9
-
ms.date: 03/13/2018
9
+
ms.date: 04/15/2024
10
10
ms.author: allensu
11
11
ms.custom: devx-track-azurepowershell
12
12
# Customer intent: I want to connect two virtual networks so that virtual machines in one virtual network can communicate with virtual machines in the other virtual network.
13
13
---
14
14
15
15
# Connect virtual networks with virtual network peering using PowerShell
16
16
17
-
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network. In this article, you learn how to:
17
+
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network.
18
+
19
+
In this article, you learn how to:
18
20
19
21
* Create two virtual networks
22
+
20
23
* Connect two virtual networks with a virtual network peering
24
+
21
25
* Deploy a virtual machine (VM) into each virtual network
26
+
22
27
* Communicate between VMs
23
28
24
29
If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
@@ -29,29 +34,37 @@ If you choose to install and use PowerShell locally, this article requires the A
29
34
30
35
## Create virtual networks
31
36
32
-
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup). The following example creates a resource group named *myResourceGroup* in the *eastus* location.
37
+
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup). The following example creates a resource group named **test-rg** in the **eastus** location.
Create a virtual network with [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). The following example creates a virtual network named *myVirtualNetwork1* with the address prefix *10.0.0.0/16*.
47
+
Create a virtual network with [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). The following example creates a virtual network named **vnet-1** with the address prefix **10.0.0.0/16**.
39
48
40
49
```azurepowershell-interactive
41
-
$virtualNetwork1 = New-AzVirtualNetwork `
42
-
-ResourceGroupName myResourceGroup `
43
-
-Location EastUS `
44
-
-Name myVirtualNetwork1 `
45
-
-AddressPrefix 10.0.0.0/16
50
+
$vnet1 = @{
51
+
ResourceGroupName = "test-rg"
52
+
Location = "EastUS"
53
+
Name = "vnet-1"
54
+
AddressPrefix = "10.0.0.0/16"
55
+
}
56
+
$virtualNetwork1 = New-AzVirtualNetwork @vnet1
46
57
```
47
58
48
-
Create a subnet configuration with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). The following example creates a subnet configuration with a 10.0.0.0/24 address prefix:
59
+
Create a subnet configuration with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). The following example creates a subnet configuration with a **10.0.0.0/24** address prefix:
Write the subnet configuration to the virtual network with [Set-AzVirtualNetwork](/powershell/module/az.network/Set-azVirtualNetwork), which creates the subnet:
@@ -60,138 +73,152 @@ Write the subnet configuration to the virtual network with [Set-AzVirtualNetwork
60
73
$virtualNetwork1 | Set-AzVirtualNetwork
61
74
```
62
75
63
-
Create a virtual network with a 10.1.0.0/16 address prefix and one subnet:
76
+
Create a virtual network with a **10.1.0.0/16** address prefix and one subnet:
# Write the subnet configuration to the virtual network.
80
97
$virtualNetwork2 | Set-AzVirtualNetwork
81
98
```
82
99
83
100
## Peer virtual networks
84
101
85
-
Create a peering with [Add-AzVirtualNetworkPeering](/powershell/module/az.network/add-azvirtualnetworkpeering). The following example peers *myVirtualNetwork1* to *myVirtualNetwork2*.
102
+
Create a peering with [Add-AzVirtualNetworkPeering](/powershell/module/az.network/add-azvirtualnetworkpeering). The following example peers **vnet-1** to **vnet-2**.
86
103
87
104
```azurepowershell-interactive
88
-
Add-AzVirtualNetworkPeering `
89
-
-Name myVirtualNetwork1-myVirtualNetwork2 `
90
-
-VirtualNetwork $virtualNetwork1 `
91
-
-RemoteVirtualNetworkId $virtualNetwork2.Id
105
+
$peerConfig1 = @{
106
+
Name = "vnet-1-to-vnet-2"
107
+
VirtualNetwork = $virtualNetwork1
108
+
RemoteVirtualNetworkId = $virtualNetwork2.Id
109
+
}
110
+
Add-AzVirtualNetworkPeering @peerConfig1
92
111
```
93
112
94
-
In the output returned after the previous command executes, you see that the **PeeringState** is *Initiated*. The peering remains in the *Initiated* state until you create the peering from *myVirtualNetwork2* to *myVirtualNetwork1*. Create a peering from *myVirtualNetwork2* to *myVirtualNetwork1*.
113
+
In the output returned after the previous command executes, you see that the **PeeringState** is **Initiated**. The peering remains in the **Initiated** state until you create the peering from **vnet-2** to **vnet-1**. Create a peering from **vnet-2** to **vnet-1**.
95
114
96
115
```azurepowershell-interactive
97
-
Add-AzVirtualNetworkPeering `
98
-
-Name myVirtualNetwork2-myVirtualNetwork1 `
99
-
-VirtualNetwork $virtualNetwork2 `
100
-
-RemoteVirtualNetworkId $virtualNetwork1.Id
116
+
$peerConfig2 = @{
117
+
Name = "vnet-2-to-vnet-1"
118
+
VirtualNetwork = $virtualNetwork2
119
+
RemoteVirtualNetworkId = $virtualNetwork1.Id
120
+
}
121
+
Add-AzVirtualNetworkPeering @peerConfig2
101
122
```
102
123
103
-
In the output returned after the previous command executes, you see that the **PeeringState** is *Connected*. Azure also changed the peering state of the *myVirtualNetwork1-myVirtualNetwork2* peering to *Connected*. Confirm that the peering state for the *myVirtualNetwork1-myVirtualNetwork2* peering changed to *Connected* with [Get-AzVirtualNetworkPeering](/powershell/module/az.network/get-azvirtualnetworkpeering).
124
+
In the output returned after the previous command executes, you see that the **PeeringState** is **Connected**. Azure also changed the peering state of the **vnet-1-to-vnet-2** peering to **Connected**. Confirm that the peering state for the **vnet-1-to-vnet-2** peering changed to **Connected** with [Get-AzVirtualNetworkPeering](/powershell/module/az.network/get-azvirtualnetworkpeering).
Resources in one virtual network cannot communicate with resources in the other virtual network until the **PeeringState** for the peerings in both virtual networks is *Connected*.
134
+
Resources in one virtual network cannot communicate with resources in the other virtual network until the **PeeringState** for the peerings in both virtual networks is **Connected**.
113
135
114
136
## Create virtual machines
115
137
116
138
Create a VM in each virtual network so that you can communicate between them in a later step.
117
139
118
140
### Create the first VM
119
141
120
-
Create a VM with [New-AzVM](/powershell/module/az.compute/new-azvm). The following example creates a VM named *myVm1* in the *myVirtualNetwork1* virtual network. The `-AsJob` option creates the VM in the background, so you can continue to the next step. When prompted, enter the user name and password you want to log in to the VM with.
142
+
Create a VM with [New-AzVM](/powershell/module/az.compute/new-azvm). The following example creates a VM named **vm-1** in the **vnet-1** virtual network. The `-AsJob` option creates the VM in the background, so you can continue to the next step. When prompted, enter the user name and password for the virtual machine.
121
143
122
144
```azurepowershell-interactive
123
-
New-AzVm `
124
-
-ResourceGroupName "myResourceGroup" `
125
-
-Location "East US" `
126
-
-VirtualNetworkName "myVirtualNetwork1" `
127
-
-SubnetName "Subnet1" `
128
-
-ImageName "Win2016Datacenter" `
129
-
-Name "myVm1" `
130
-
-AsJob
145
+
$vm1 = @{
146
+
ResourceGroupName = "test-rg"
147
+
Location = "EastUS"
148
+
VirtualNetworkName = "vnet-1"
149
+
SubnetName = "subnet-1"
150
+
ImageName = "Win2019Datacenter"
151
+
Name = "vm-1"
152
+
}
153
+
New-AzVm @vm1 -AsJob
131
154
```
132
155
133
156
### Create the second VM
134
157
135
158
```azurepowershell-interactive
136
-
New-AzVm `
137
-
-ResourceGroupName "myResourceGroup" `
138
-
-Location "East US" `
139
-
-VirtualNetworkName "myVirtualNetwork2" `
140
-
-SubnetName "Subnet1" `
141
-
-ImageName "Win2016Datacenter" `
142
-
-Name "myVm2"
159
+
$vm2 = @{
160
+
ResourceGroupName = "test-rg"
161
+
Location = "EastUS"
162
+
VirtualNetworkName = "vnet-2"
163
+
SubnetName = "subnet-1"
164
+
ImageName = "Win2019Datacenter"
165
+
Name = "vm-2"
166
+
}
167
+
New-AzVm @vm2
143
168
```
144
169
145
-
The VM takes a few minutes to create. Do not continue with later steps until Azure creates the VM and returns output to PowerShell.
170
+
The VM takes a few minutes to create. Don't continue with the later steps until Azure creates **vm-2** and returns output to PowerShell.
You can connect to a VM's public IP address from the internet. Use [Get-AzPublicIpAddress](/powershell/module/az.network/get-azpublicipaddress) to return the public IP address of a VM. The following example returns the public IP address of the *myVm1* VM:
176
+
You can connect to a VM's public IP address from the internet. Use [Get-AzPublicIpAddress](/powershell/module/az.network/get-azpublicipaddress) to return the public IP address of a VM. The following example returns the public IP address of the **vm-1** VM:
Use the following command to create a remote desktop session with the *myVm1* VM from your local computer. Replace `<publicIpAddress>` with the IP address returned from the previous command.
186
+
Use the following command to create a remote desktop session with the **vm-1** VM from your local computer. Replace `<publicIpAddress>` with the IP address returned from the previous command.
160
187
161
188
```
162
189
mstsc /v:<publicIpAddress>
163
190
```
164
191
165
-
A Remote Desktop Protocol (.rdp) file is created, downloaded to your computer, and opened. Enter the user name and password (you may need to select **More choices**, then **Use a different account**, to specify the credentials you entered when you created the VM), and then click **OK**. You may receive a certificate warning during the sign-in process. Click **Yes** or **Continue** to proceed with the connection.
192
+
A Remote Desktop Protocol (.rdp) file is created and opened. Enter the user name and password (you may need to select **More choices**, then **Use a different account**, to specify the credentials you entered when you created the VM), and then click **OK**. You may receive a certificate warning during the sign-in process. Click **Yes** or **Continue** to proceed with the connection.
166
193
167
-
On the *myVm1* VM, enable the Internet Control Message Protocol (ICMP) through the Windows firewall so you can ping this VM from *myVm2* in a later step, using PowerShell:
194
+
On **vm-1**, enable the Internet Control Message Protocol (ICMP) through the Windows Firewall so you can ping this VM from **vm-2** in a later step, using PowerShell:
Though ping is used to communicate between VMs in this article, allowing ICMP through the Windows Firewall for production deployments is not recommended.
200
+
**Though ping is used to communicate between VMs in this article, allowing ICMP through the Windows Firewall for production deployments is not recommended.**
174
201
175
-
To connect to the *myVm2* VM, enter the following command from a command prompt on the *myVm1* VM:
202
+
To connect to **vm-2**, enter the following command from a command prompt on **vm-1**:
176
203
177
204
```
178
205
mstsc /v:10.1.0.4
179
206
```
180
207
181
-
Since you enabled ping on *myVm1*, you can now ping it by IP address from a command prompt on the *myVm2* VM:
208
+
You enabled ping on **vm-1**. You can now ping **vm-1** by IP address from a command prompt on **vm-2**.
182
209
183
210
```
184
211
ping 10.0.0.4
185
212
```
186
213
187
-
You receive four replies. Disconnect your RDP sessions to both *myVm1* and *myVm2*.
214
+
You receive four replies. Disconnect your RDP sessions to both **vm-1** and **vm-2**.
188
215
189
216
## Clean up resources
190
217
191
218
When no longer needed, use [Remove-AzResourcegroup](/powershell/module/az.resources/remove-azresourcegroup) to remove the resource group and all of the resources it contains.
0 commit comments