Skip to content

Commit e255b30

Browse files
authored
Merge pull request #272253 from asudbring/vnet-freshness-1
Freshness update of vnet peering PowerShell article
2 parents fc2fda3 + 02d4d70 commit e255b30

File tree

1 file changed

+97
-70
lines changed

1 file changed

+97
-70
lines changed

articles/virtual-network/tutorial-connect-virtual-networks-powershell.md

Lines changed: 97 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ author: asudbring
66
ms.service: virtual-network
77
ms.topic: how-to
88
ms.tgt_pltfrm: virtual-network
9-
ms.date: 03/13/2018
9+
ms.date: 04/15/2024
1010
ms.author: allensu
1111
ms.custom: devx-track-azurepowershell
1212
# 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.
1313
---
1414

1515
# Connect virtual networks with virtual network peering using PowerShell
1616

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:
1820

1921
* Create two virtual networks
22+
2023
* Connect two virtual networks with a virtual network peering
24+
2125
* Deploy a virtual machine (VM) into each virtual network
26+
2227
* Communicate between VMs
2328

2429
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
2934

3035
## Create virtual networks
3136

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.
3338

3439
```azurepowershell-interactive
35-
New-AzResourceGroup -ResourceGroupName myResourceGroup -Location EastUS
40+
$resourceGroup = @{
41+
Name = "test-rg"
42+
Location = "EastUS"
43+
}
44+
New-AzResourceGroup @resourceGroup
3645
```
3746

38-
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**.
3948

4049
```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
4657
```
4758

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:
4960

5061
```azurepowershell-interactive
51-
$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
52-
-Name Subnet1 `
53-
-AddressPrefix 10.0.0.0/24 `
54-
-VirtualNetwork $virtualNetwork1
62+
$subConfig = @{
63+
Name = "subnet-1"
64+
AddressPrefix = "10.0.0.0/24"
65+
VirtualNetwork = $virtualNetwork1
66+
}
67+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subConfig
5568
```
5669

5770
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
6073
$virtualNetwork1 | Set-AzVirtualNetwork
6174
```
6275

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:
6477

6578
```azurepowershell-interactive
6679
# Create the virtual network.
67-
$virtualNetwork2 = New-AzVirtualNetwork `
68-
-ResourceGroupName myResourceGroup `
69-
-Location EastUS `
70-
-Name myVirtualNetwork2 `
71-
-AddressPrefix 10.1.0.0/16
80+
$vnet2 = @{
81+
ResourceGroupName = "test-rg"
82+
Location = "EastUS"
83+
Name = "vnet-2"
84+
AddressPrefix = "10.1.0.0/16"
85+
}
86+
$virtualNetwork2 = New-AzVirtualNetwork @vnet2
7287
7388
# Create the subnet configuration.
74-
$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
75-
-Name Subnet1 `
76-
-AddressPrefix 10.1.0.0/24 `
77-
-VirtualNetwork $virtualNetwork2
89+
$subConfig = @{
90+
Name = "subnet-1"
91+
AddressPrefix = "10.1.0.0/24"
92+
VirtualNetwork = $virtualNetwork2
93+
}
94+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subConfig
7895
7996
# Write the subnet configuration to the virtual network.
8097
$virtualNetwork2 | Set-AzVirtualNetwork
8198
```
8299

83100
## Peer virtual networks
84101

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**.
86103

87104
```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
92111
```
93112

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**.
95114

96115
```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
101122
```
102123

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).
104125

105126
```azurepowershell-interactive
106-
Get-AzVirtualNetworkPeering `
107-
-ResourceGroupName myResourceGroup `
108-
-VirtualNetworkName myVirtualNetwork1 `
109-
| Select PeeringState
127+
$peeringState = @{
128+
ResourceGroupName = "test-rg"
129+
VirtualNetworkName = "vnet-1"
130+
}
131+
Get-AzVirtualNetworkPeering @peeringState | Select PeeringState
110132
```
111133

112-
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**.
113135

114136
## Create virtual machines
115137

116138
Create a VM in each virtual network so that you can communicate between them in a later step.
117139

118140
### Create the first VM
119141

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.
121143

122144
```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
131154
```
132155

133156
### Create the second VM
134157

135158
```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
143168
```
144169

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.
146171

147172
[!INCLUDE [ephemeral-ip-note.md](../../includes/ephemeral-ip-note.md)]
148173

149174
## Communicate between VMs
150175

151-
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:
152177

153178
```azurepowershell-interactive
154-
Get-AzPublicIpAddress `
155-
-Name myVm1 `
156-
-ResourceGroupName myResourceGroup | Select IpAddress
179+
$ipAddress = @{
180+
ResourceGroupName = "test-rg"
181+
Name = "vm-1"
182+
}
183+
Get-AzPublicIpAddress @ipAddress | Select IpAddress
157184
```
158185

159-
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.
160187

161188
```
162189
mstsc /v:<publicIpAddress>
163190
```
164191

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.
166193

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:
168195

169196
```powershell
170197
New-NetFirewallRule –DisplayName "Allow ICMPv4-In" –Protocol ICMPv4
171198
```
172199

173-
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.**
174201

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**:
176203

177204
```
178205
mstsc /v:10.1.0.4
179206
```
180207

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**.
182209

183210
```
184211
ping 10.0.0.4
185212
```
186213

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**.
188215

189216
## Clean up resources
190217

191218
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.
192219

193220
```azurepowershell-interactive
194-
Remove-AzResourceGroup -Name myResourceGroup -Force
221+
Remove-AzResourceGroup -Name test-rg -Force
195222
```
196223

197224
## Next steps

0 commit comments

Comments
 (0)