Skip to content

Commit 26d4b3f

Browse files
Merge pull request #289153 from mbender-ms/patch-625840
virtual network manager - Major Update create-virtual-network-manager…
2 parents 4e6e82a + 18aaadf commit 26d4b3f

File tree

1 file changed

+58
-187
lines changed

1 file changed

+58
-187
lines changed

articles/virtual-network-manager/create-virtual-network-manager-powershell.md

Lines changed: 58 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: mbender-ms
55
ms.author: mbender
66
ms.service: azure-virtual-network-manager
77
ms.topic: quickstart
8-
ms.date: 04/12/2024
8+
ms.date: 10/22/2024
99
ms.custom: template-quickstart, mode-api, engagement-fy23, devx-track-azurepowershell
1010
---
1111

@@ -47,13 +47,13 @@ Install the latest *Az.Network* Azure PowerShell module by using this command:
4747

4848
## Create a resource group
4949

50-
Before you can create an Azure Virtual Network Manager instance, you have to create a resource group to host it. Create a resource group by using [New-AzResourceGroup](/powershell/module/az.Resources/New-azResourceGroup). This example creates a resource group named *vnm-learn-eastus-001ResourceGroup* in the East US location:
50+
Before you can create an Azure Virtual Network Manager instance, you have to create a resource group to host it. Create a resource group by using [New-AzResourceGroup](/powershell/module/az.Resources/New-azResourceGroup). This example creates a resource group named *resource-group* in the *West US 2* region:
5151

5252
```azurepowershell
53-
54-
$location = "East US"
53+
# Create a resource group
54+
$location = "West US 2"
5555
$rg = @{
56-
Name = 'rg-learn-eastus-001'
56+
Name = 'resource-group'
5757
Location = $location
5858
}
5959
New-AzResourceGroup @rg
@@ -65,11 +65,10 @@ New-AzResourceGroup @rg
6565
Define the scope and access type for the Azure Virtual Network Manager instance by using [New-AzNetworkManagerScope](/powershell/module/az.network/new-aznetworkmanagerscope). This example defines a scope with a single subscription and sets the access type to *Connectivity*. Replace `<subscription_id>` with the ID of the subscription that you want to manage through Azure Virtual Network Manager.
6666

6767
```azurepowershell
68-
69-
Import-Module -Name Az.Network -RequiredVersion "5.3.0"
68+
$subID= <subscription_id>
7069
7170
[System.Collections.Generic.List[string]]$subGroup = @()
72-
$subGroup.Add("/subscriptions/<subscription_id>")
71+
$subGroup.Add("/subscriptions/$subID")
7372
7473
[System.Collections.Generic.List[String]]$access = @()
7574
$access.Add("Connectivity");
@@ -80,12 +79,12 @@ $scope = New-AzNetworkManagerScope -Subscription $subGroup
8079

8180
## Create a Virtual Network Manager instance
8281

83-
Create a Virtual Network Manager instance by using [New-AzNetworkManager](/powershell/module/az.network/new-aznetworkmanager). This example creates an instance named *vnm-learn-eastus-001* in the East US location:
82+
Create a Virtual Network Manager instance by using [New-AzNetworkManager](/powershell/module/az.network/new-aznetworkmanager). This example creates an instance named *network-manager* in the *West US 2* region:
8483

8584
```azurepowershell
8685
$avnm = @{
87-
Name = 'vnm-learn-eastus-001'
88-
ResourceGroupName = $rg.Name
86+
Name = 'network-manager'
87+
ResourceGroupName = $rg.ResourceGroupName
8988
NetworkManagerScope = $scope
9089
NetworkManagerScopeAccess = $access
9190
Location = $location
@@ -95,87 +94,81 @@ $networkmanager = New-AzNetworkManager @avnm
9594

9695
## Create three virtual networks
9796

98-
Create three virtual networks by using [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). This example creates virtual networks named *vnet-learn-prod-eastus-001*, *vnet-learn-prod-eastus-002*, and *vnet-learn-test-eastus-003* in the East US location. If you already have virtual networks that you want create a mesh network with, you can skip to the next section.
97+
Create three virtual networks by using [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). This example creates virtual networks named *vnet-spoke-001*, *vnet-spoke-002*, and *vnet-hub-001* in the *West US 2* region. If you already have virtual networks that you want create a mesh network with, you can skip to the next section.
9998

10099
```azurepowershell
101-
$vnet001 = @{
102-
Name = 'vnet-learn-prod-eastus-001'
103-
ResourceGroupName = $rg.Name
100+
$vnetspoke001 = @{
101+
Name = 'vnet-spoke-001'
102+
ResourceGroupName = $rg.ResourceGroupName
104103
Location = $location
105104
AddressPrefix = '10.0.0.0/16'
106105
}
107106
108-
$vnet_learn_prod_eastus_001 = New-AzVirtualNetwork @vnet001
107+
$vnet_spoke_001 = New-AzVirtualNetwork @vnetspoke001
109108
110-
$vnet002 = @{
111-
Name = 'vnet-learn-prod-eastus-002'
112-
ResourceGroupName = $rg.Name
109+
$vnetspoke002 = @{
110+
Name = 'vnet-spoke-002'
111+
ResourceGroupName = $rg.ResourceGroupName
113112
Location = $location
114113
AddressPrefix = '10.1.0.0/16'
115114
}
116-
$vnet_learn_prod_eastus_002 = New-AzVirtualNetwork @vnet002
115+
$vnet_spoke_002 = New-AzVirtualNetwork @vnetspoke002
117116
118-
$vnet003 = @{
119-
Name = 'vnet-learn-test-eastus-003'
120-
ResourceGroupName = $rg.Name
117+
$vnethub001 = @{
118+
Name = 'vnet-hub-001'
119+
ResourceGroupName = $rg.ResourceGroupName
121120
Location = $location
122121
AddressPrefix = '10.2.0.0/16'
123122
}
124-
$vnet_learn_test_eastus_003 = New-AzVirtualNetwork @vnet003
123+
$vnet_hub_001 = New-AzVirtualNetwork @vnethub001
125124
```
126125

127126
### Add a subnet to each virtual network
128127

129128
To complete the configuration of the virtual networks, create a subnet configuration named *default* with a subnet address prefix of */24* by using [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). Then, use [Set-AzVirtualNetwork](/powershell/module/az.network/set-azvirtualnetwork) to apply the subnet configuration to the virtual network.
130129

131130
```azurepowershell
132-
$subnet_vnet001 = @{
131+
$subnet_vnetspoke001 = @{
133132
Name = 'default'
134-
VirtualNetwork = $vnet_learn_prod_eastus_001
133+
VirtualNetwork = $vnet_spoke_001
135134
AddressPrefix = '10.0.0.0/24'
136135
}
137-
$subnetConfig_vnet001 = Add-AzVirtualNetworkSubnetConfig @subnet_vnet001
138-
$vnet_learn_prod_eastus_001 | Set-AzVirtualNetwork
136+
$subnetConfig_vnetspoke001 = Add-AzVirtualNetworkSubnetConfig @subnet_vnetspoke001
137+
$vnet_spoke_001 | Set-AzVirtualNetwork
139138
140-
$subnet_vnet002 = @{
139+
$subnet_vnetspoke002 = @{
141140
Name = 'default'
142-
VirtualNetwork = $vnet_learn_prod_eastus_002
141+
VirtualNetwork = $vnet_spoke_002
143142
AddressPrefix = '10.1.0.0/24'
144143
}
145-
$subnetConfig_vnet002 = Add-AzVirtualNetworkSubnetConfig @subnet_vnet002
146-
$vnet_learn_prod_eastus_002 | Set-AzVirtualNetwork
144+
$subnetConfig_vnetspoke002 = Add-AzVirtualNetworkSubnetConfig @subnet_vnetspoke002
145+
$vnet_spoke_002 | Set-AzVirtualNetwork
147146
148-
$subnet_vnet003 = @{
147+
$subnet_vnet_hub_001 = @{
149148
Name = 'default'
150-
VirtualNetwork = $vnet_learn_test_eastus_003
149+
VirtualNetwork = $vnet_hub_001
151150
AddressPrefix = '10.2.0.0/24'
152151
}
153-
$subnetConfig_vnet003 = Add-AzVirtualNetworkSubnetConfig @subnet_vnet003
154-
$vnet_learn_test_eastus_003 | Set-AzVirtualNetwork
152+
$subnetConfig_vnet_hub_001 = Add-AzVirtualNetworkSubnetConfig @subnet_vnet_hub_001
153+
$vnet_hub_001 | Set-AzVirtualNetwork
155154
```
156155

157156
## Create a network group
158157

159-
Virtual Network Manager applies configurations to groups of virtual networks by placing them in network groups. Create a network group by using [New-AzNetworkManagerGroup](/powershell/module/az.network/new-aznetworkmanagergroup). This example creates a network group named *ng-learn-prod-eastus-001* in the East US location:
158+
Virtual Network Manager applies configurations to groups of virtual networks by placing them in network groups. Create a network group by using [New-AzNetworkManagerGroup](/powershell/module/az.network/new-aznetworkmanagergroup). This example creates a network group named *network-group* in the West US 2 region:
160159

161160
```azurepowershell
162161
$ng = @{
163-
Name = 'ng-learn-prod-eastus-001'
164-
ResourceGroupName = $rg.Name
162+
Name = 'network-group'
163+
ResourceGroupName = $rg.ResourceGroupName
165164
NetworkManagerName = $networkManager.Name
166165
}
167166
$ng = New-AzNetworkManagerGroup @ng
168167
```
169168

170169
## Define membership for a mesh configuration
171170

172-
After you create your network group, you define its membership by adding virtual networks. You can add these networks manually or by using Azure Policy.
173-
174-
# [Manual membership](#tab/manualmembership)
175-
176-
### Add membership manually
177-
178-
In this task, you add the static members *vnet-learn-prod-eastus-001* and *vnet-learn-prod-eastus-002* to the network group *ng-learn-prod-eastus-001* by using [New-AzNetworkManagerStaticMember](/powershell/module/az.network/new-aznetworkmanagerstaticmember).
171+
In this task, you add the static members *vnet-spoke-001* and *vnet-spoke-002* to the network group *network-group* by using [New-AzNetworkManagerStaticMember](/powershell/module/az.network/new-aznetworkmanagerstaticmember).
179172

180173
Static members must have a unique name that's scoped to the network group. We recommend that you use a consistent hash of the virtual network ID. This approach uses the Azure Resource Manager template's `uniqueString()` implementation.
181174

@@ -188,101 +181,30 @@ Static members must have a unique name that's scoped to the network group. We re
188181
```
189182

190183
```azurepowershell
191-
$sm_vnet001 = @{
192-
Name = Get-UniqueString $vnet_learn_prod_eastus_001.Id
193-
ResourceGroupName = $rg.Name
184+
$sm_vnetspoke001 = @{
185+
Name = Get-UniqueString $vnet_spoke_001.Id
186+
ResourceGroupName = $rg.ResourceGroupName
194187
NetworkGroupName = $ng.Name
195188
NetworkManagerName = $networkManager.Name
196-
ResourceId = $vnet_learn_prod_eastus_001.Id
189+
ResourceId = $vnet_spoke_001.Id
197190
}
198-
$sm_vnet001 = New-AzNetworkManagerStaticMember @sm_vnet001
191+
$sm_vnetspoke001 = New-AzNetworkManagerStaticMember @sm_vnetspoke001
199192
```
200193

201194
```azurepowershell
202-
$sm_vnet002 = @{
203-
Name = Get-UniqueString $vnet_learn_prod_eastus_002.Id
204-
ResourceGroupName = $rg.Name
195+
$sm_vnetspoke002 = @{
196+
Name = Get-UniqueString $vnet_spoke_002.Id
197+
ResourceGroupName = $rg.ResourceGroupName
205198
NetworkGroupName = $ng.Name
206199
NetworkManagerName = $networkManager.Name
207-
ResourceId = $vnet_learn_prod_eastus_002.Id
200+
ResourceId = $vnet_spoke_002.Id
208201
}
209-
$sm_vnet002 = New-AzNetworkManagerStaticMember @sm_vnet002
202+
$sm_vnetspoke002 = New-AzNetworkManagerStaticMember @sm_vnetspoke002
210203
```
211-
212-
# [Azure Policy](#tab/azurepolicy)
213-
214-
### Create a policy definition for dynamic membership
215-
216-
By using [Azure Policy](concept-azure-policy-integration.md), you define a condition to dynamically add two virtual networks to your network group when the name of the virtual network includes *-prod*.
217-
218-
> [!NOTE]
219-
> We recommend that you scope all of your conditionals to scan for only type `Microsoft.Network/virtualNetworks`, for efficiency.
220-
221-
1. Define the conditional statement and store it in a variable:
222-
223-
```azurepowershell
224-
$conditionalMembership = '{
225-
"if": {
226-
"allOf": [
227-
{
228-
"field": "type",
229-
"equals": "Microsoft.Network/virtualNetworks"
230-
},
231-
{
232-
"field": "name",
233-
"contains": "prod"
234-
}
235-
]
236-
},
237-
"then": {
238-
"effect": "addToNetworkGroup",
239-
"details": {
240-
"networkGroupId": "/subscriptions/<subscription_id>/resourceGroups/rg-learn-eastus-001/providers/Microsoft.Network/networkManagers/vnm-learn-eastus-001/networkGroups/ng-learn-prod-eastus-001"}
241-
},
242-
}'
243-
244-
```
245-
246-
1. Create the Azure Policy definition by using the conditional statement defined in the previous step and using [New-AzPolicyDefinition](/powershell/module/az.resources/new-azpolicydefinition).
247-
248-
In this example, the policy definition name is prefixed with *poldef-learn-prod-* and suffixed with a unique string that's generated from a consistent hash in the network group ID. Policy resources must have a scope unique name.
249-
250-
```azurepowershell
251-
function Get-UniqueString ([string]$id, $length=13)
252-
{
253-
$hashArray = (new-object System.Security.Cryptography.SHA512Managed).ComputeHash($id.ToCharArray())
254-
-join ($hashArray[1..$length] | ForEach-Object { [char]($_ % 26 + [byte][char]'a') })
255-
}
256-
257-
$UniqueString = Get-UniqueString $ng.Id
258-
```
259-
260-
```azurepowershell
261-
$polDef = @{
262-
Name = "poldef-learn-prod-"+$UniqueString
263-
Mode = 'Microsoft.Network.Data'
264-
Policy = $conditionalMembership
265-
}
266-
267-
$policyDefinition = New-AzPolicyDefinition @polDef
268-
```
269-
270-
1. Assign the policy definition at a scope within your network manager's scope so that it can begin taking effect:
271-
272-
```azurepowershell
273-
$polAssign = @{
274-
Name = "polassign-learn-prod-"+$UniqueString
275-
PolicyDefinition = $policyDefinition
276-
}
277-
278-
$policyAssignment = New-AzPolicyAssignment @polAssign
279-
```
280-
281-
---
282204

283205
## Create a connectivity configuration
284206

285-
In this task, you create a connectivity configuration with the network group *ng-learn-prod-eastus-001* by using [New-AzNetworkManagerConnectivityConfiguration](/powershell/module/az.network/new-aznetworkmanagerconnectivityconfiguration) and [New-AzNetworkManagerConnectivityGroupItem](/powershell/module/az.network/new-aznetworkmanagerconnectivitygroupitem):
207+
In this task, you create a connectivity configuration with the network group *network-group* by using [New-AzNetworkManagerConnectivityConfiguration](/powershell/module/az.network/new-aznetworkmanagerconnectivityconfiguration) and [New-AzNetworkManagerConnectivityGroupItem](/powershell/module/az.network/new-aznetworkmanagerconnectivitygroupitem):
286208

287209
1. Create a connectivity group item:
288210

@@ -304,8 +226,8 @@ In this task, you create a connectivity configuration with the network group *ng
304226
305227
```azurepowershell
306228
$config = @{
307-
Name = 'cc-learn-prod-eastus-001'
308-
ResourceGroupName = $rg.Name
229+
Name = 'connectivity-configuration'
230+
ResourceGroupName = $rg.ResourceGroupName
309231
NetworkManagerName = $networkManager.Name
310232
ConnectivityTopology = 'Mesh'
311233
AppliesToGroup = $configGroup
@@ -321,77 +243,26 @@ Commit the configuration to the target regions by using `Deploy-AzNetworkManager
321243
[System.Collections.Generic.List[string]]$configIds = @()
322244
$configIds.add($connectivityconfig.id)
323245
[System.Collections.Generic.List[string]]$target = @()
324-
$target.Add("westus")
246+
$target.Add("westus2")
325247
326248
$deployment = @{
327249
Name = $networkManager.Name
328-
ResourceGroupName = $rg.Name
250+
ResourceGroupName = $rg.ResourceGroupName
329251
ConfigurationId = $configIds
330252
TargetLocation = $target
331253
CommitType = 'Connectivity'
332254
}
333-
Deploy-AzNetworkManagerCommit @deployment
255+
Deploy-AzNetworkManagerCommit @deployment
334256
```
335257

336258
## Clean up resources
337259

338-
If you no longer need the Azure Virtual Network Manager instance, make sure all of following points are true before you delete the resource:
339-
340-
* There are no deployments of configurations to any region.
341-
* All configurations have been deleted.
342-
* All network groups have been deleted.
343-
344-
To delete the resource:
345-
346-
1. Remove the connectivity deployment by deploying an empty configuration via `Deploy-AzNetworkManagerCommit`:
347-
348-
```azurepowershell
349-
[System.Collections.Generic.List[string]]$configIds = @()
350-
[System.Collections.Generic.List[string]]$target = @()
351-
$target.Add("westus")
352-
$removedeployment = @{
353-
Name = 'vnm-learn-eastus-001'
354-
ResourceGroupName = $rg.Name
355-
ConfigurationId = $configIds
356-
Target = $target
357-
CommitType = 'Connectivity'
358-
}
359-
Deploy-AzNetworkManagerCommit @removedeployment
360-
```
361-
362-
1. Remove the connectivity configuration by using `Remove-AzNetworkManagerConnectivityConfiguration`:
363-
364-
```azurepowershell
365-
366-
Remove-AzNetworkManagerConnectivityConfiguration -Name $connectivityconfig.Name -ResourceGroupName $rg.Name -NetworkManagerName $networkManager.Name
367-
368-
```
369-
370-
1. Remove the policy resources by using `Remove-AzPolicy*`:
371-
372-
```azurepowershell
373-
374-
Remove-AzPolicyAssignment -Name $policyAssignment.Name
375-
Remove-AzPolicyAssignment -Name $policyDefinition.Name
376-
377-
```
378-
379-
1. Remove the network group by using `Remove-AzNetworkManagerGroup`:
380-
381-
```azurepowershell
382-
Remove-AzNetworkManagerGroup -Name $ng.Name -ResourceGroupName $rg.Name -NetworkManagerName $networkManager.Name
383-
```
384-
385-
1. Delete the Virtual Network Manager instance by using `Remove-AzNetworkManager`:
386-
387-
```azurepowershell
388-
Remove-AzNetworkManager -name $networkManager.Name -ResourceGroupName $rg.Name
389-
```
260+
If you no longer need the Azure Virtual Network Manager instance and it's associate resources, delete the resource group that contains them. Deleting the resource group also deletes the resources that you created.
390261

391-
1. If you no longer need the resource that you created, delete the resource group by using [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup):
262+
1. Delete the resource group using [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup):
392263

393264
```azurepowershell
394-
Remove-AzResourceGroup -Name $rg.Name -Force
265+
Remove-AzResourceGroup -Name $rg.ResourceGroupName -Force
395266
```
396267
397268
## Next steps

0 commit comments

Comments
 (0)