Skip to content

Commit d37b566

Browse files
authored
Merge pull request #207403 from mbender-ms/avnm-update-create-ps
AVNM - Update Create PS doc with Azure Policy integration
2 parents 294fcbf + 02bcff2 commit d37b566

File tree

2 files changed

+140
-81
lines changed

2 files changed

+140
-81
lines changed

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

Lines changed: 140 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: mbender-ms
55
ms.author: mbender
66
ms.service: virtual-network-manager
77
ms.topic: quickstart
8-
ms.date: 06/27/2022
8+
ms.date: 08/9/2022
99
ms.custom: template-quickstart, ignite-fall-2021, mode-api
1010
---
1111

@@ -42,18 +42,22 @@ Install the latest *Az.Network* Azure PowerShell module using this command:
4242
Before you can create an Azure Virtual Network Manager, you have to create a resource group to host the Network Manager. Create a resource group with [New-AzResourceGroup](/powershell/module/az.Resources/New-azResourceGroup). This example creates a resource group named **myAVNMResourceGroup** in the **WestUS** location.
4343

4444
```azurepowershell-interactive
45+
46+
$location = "West US"
4547
$rg = @{
4648
Name = 'myAVNMResourceGroup'
47-
Location = 'WestUS'
49+
Location = $location
4850
}
49-
New-AzResourceGroup @rg
51+
New-AzResourceGroup $rg
52+
5053
```
5154

5255
## Create Virtual Network Manager
5356

5457
1. Define the scope and access type this Azure Virtual Network Manager instance will have. You can choose to create the scope with subscriptions group or management group or a combination of both. Create the scope by using New-AzNetworkManagerScope.
5558

5659
```azurepowershell-interactive
60+
5761
Import-Module -Name Az.Network -RequiredVersion "4.15.1"
5862
5963
[System.Collections.Generic.List[string]]$subGroup = @()
@@ -64,19 +68,20 @@ New-AzResourceGroup @rg
6468
[System.Collections.Generic.List[String]]$access = @()
6569
$access.Add("Connectivity");
6670
$access.Add("SecurityAdmin");
67-
71+
6872
$scope = New-AzNetworkManagerScope -Subscription $subGroup -ManagementGroup $mgGroup
73+
6974
```
7075
7176
1. Create the Virtual Network Manager with New-AzNetworkManager. This example creates an Azure Virtual Network Manager named **myAVNM** in the West US location.
72-
77+
7378
```azurepowershell-interactive
7479
$avnm = @{
7580
Name = 'myAVNM'
76-
ResourceGroupName = 'myAVNMResourceGroup'
81+
ResourceGroupName = $rg.Name
7782
NetworkManagerScope = $scope
7883
NetworkManagerScopeAccess = $access
79-
Location = 'West US'
84+
Location = $location
8085
}
8186
$networkmanager = New-AzNetworkManager @avnm
8287
```
@@ -89,31 +94,32 @@ Create three virtual networks with [New-AzVirtualNetwork](/powershell/module/az.
8994
$vnetA = @{
9095
Name = 'VNetA'
9196
ResourceGroupName = 'myAVNMResourceGroup'
92-
Location = 'West US'
97+
Location = $location
9398
AddressPrefix = '10.0.0.0/16'
9499
}
100+
95101
$virtualNetworkA = New-AzVirtualNetwork @vnetA
96102
97103
$vnetB = @{
98104
Name = 'VNetB'
99105
ResourceGroupName = 'myAVNMResourceGroup'
100-
Location = 'West US'
106+
Location = $location
101107
AddressPrefix = '10.1.0.0/16'
102108
}
103109
$virtualNetworkB = New-AzVirtualNetwork @vnetB
104110
105111
$vnetC = @{
106112
Name = 'VNetC'
107113
ResourceGroupName = 'myAVNMResourceGroup'
108-
Location = 'West US'
114+
Location = $location
109115
AddressPrefix = '10.2.0.0/16'
110116
}
111117
$virtualNetworkC = New-AzVirtualNetwork @vnetC
112118
```
113119

114120
### Add a subnet to each virtual network
115121

116-
To complete the configuration of the virtual networks add a /24 subnet to each one. Create a subnet configuration named **default** with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig).
122+
To complete the configuration of the virtual networks, add a /24 subnet to each one. Create a subnet configuration named **default** with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig).
117123

118124
```azurepowershell-interactive
119125
$subnetA = @{
@@ -143,62 +149,119 @@ $virtualnetworkC | Set-AzVirtualNetwork
143149

144150
## Create a network group
145151

146-
### Static membership
147-
148-
1. Create a static virtual network member with New-AzNetworkManagerGroupMembersItem.
152+
1. Create a network group to add virtual networks to.
149153

150154
```azurepowershell-interactive
151155
$ng = @{
152-
Name = 'myNetworkGroup'
153-
ResourceGroupName = 'myAVNMResourceGroup'
154-
NetworkManagerName = 'myAVNM'
155-
MemberType = 'Microsoft.Network/VirtualNetwork'
156-
}
157-
$networkgroup = New-AzNetworkManagerGroup @ng
156+
Name = 'myNetworkGroup'
157+
ResourceGroupName = $rg.Name
158+
NetworkManagerName = $networkManager.Name
159+
}
160+
$networkgroup = New-AzNetworkManagerGroup @ng
158161
```
159-
160-
1. Add the static member to the static membership group with the following commands:
162+
163+
### Option 1: Static membership
161164
165+
166+
1. Add the static member to the network group with the following commands:
167+
1. Static members must have a network group scoped unique name. It's recommended to use a consistent hash of the virtual network ID. Below is an approach using the ARM Templates uniqueString() implementation.
168+
162169
```azurepowershell-interactive
163-
$sm = @{
164-
Name = 'myStaticMember'
165-
ResourceGroupName = 'myAVNMResourceGroup'
166-
NetworkGroupName = 'myNetworkGroup'
167-
NetworkManagerName = 'myAVNM'
168-
ResourceId = '/subscriptions/abcdef12-3456-7890-abcd-ef1234567890/resourceGroups/myAVNMResourceGroup/providers/Microsoft.Network/virtualNetworks/VNetA'
169-
}
170-
$statimember = New-AzNetworkManagerStaticMember @sm
170+
function Get-UniqueString ([string]$id, $length=13)
171+
{
172+
$hashArray = (new-object System.Security.Cryptography.SHA512Managed).ComputeHash($id.ToCharArray())
173+
-join ($hashArray[1..$length] | ForEach-Object { [char]($_ % 26 + [byte][char]'a') })
174+
}
171175
```
176+
177+
```azurepowershell-interactive
178+
$smA = @{
179+
Name = Get-UniqueString $virtualNetworkA.Id
180+
ResourceGroupName = $rg.Name
181+
NetworkGroupName = $networkGroup.Name
182+
NetworkManagerName = $networkManager.Name
183+
ResourceId = $virtualNetworkA.Id
184+
}
185+
$statimemberA = New-AzNetworkManagerStaticMember @sm
186+
```
187+
188+
```azurepowershell-interactive
189+
$smB = @{
190+
Name = Get-UniqueString $virtualNetworkB.Id
191+
ResourceGroupName = $rg.Name
192+
NetworkGroupName = $networkGroup.Name
193+
NetworkManagerName = $networkManager.Name
194+
ResourceId = $virtualNetworkB.Id
195+
}
196+
$statimemberB = New-AzNetworkManagerStaticMember @sm
197+
```
198+
199+
```azurepowershell-interactive
200+
$smC = @{
201+
Name = Get-UniqueString $virtualNetworkC.Id
202+
ResourceGroupName = $rg.Name
203+
NetworkGroupName = $networkGroup.Name
204+
NetworkManagerName = $networkManager.Name
205+
ResourceId = $virtualNetworkC.Id
206+
}
207+
$statimemberC = New-AzNetworkManagerStaticMember @sm
208+
```
209+
210+
### Option 2: Dynamic membership
172211
173-
### Dynamic membership
212+
1. Define the conditional statement and store it in a variable.
213+
> [!NOTE]
214+
> It is recommended to scope all of your conditionals to only scan for type `Microsoft.Network/virtualNetwork` for efficiency.
174215
175-
1. Define the conditional statement and store it in a variable:
216+
```azurepowershell-interactive
217+
$conditionalMembership = '{
218+
"allof":[
219+
{
220+
"field": "type",
221+
"equals": "Microsoft.Network/virtualNetwork"
222+
}
223+
{
224+
"field": "name",
225+
"contains": "VNet"
226+
}
227+
]
228+
}'
229+
```
230+
231+
1. Create the Azure Policy definition using the conditional statement defined in the last step using New-AzPolicyDefinition.
176232

177-
```azurepowershell-interactive
178-
$conditionalMembership = '{
179-
"allof":[
180-
{
181-
"field": "name",
182-
"contains": "VNet"
183-
}
184-
]
185-
}'
186-
```
233+
> [!IMPORTANT]
234+
> Policy resources must have a scope unique name. It is recommended to use a consistent hash of the network group. Below is an approach using the ARM Templates uniqueString() implementation.
235+
236+
```azurepowershell-interactive
237+
function Get-UniqueString ([string]$id, $length=13)
238+
{
239+
$hashArray = (new-object System.Security.Cryptography.SHA512Managed).ComputeHash($id.ToCharArray())
240+
-join ($hashArray[1..$length] | ForEach-Object { [char]($_ % 26 + [byte][char]'a') })
241+
}
242+
```
187243

188-
1. Create the network group using the conditional statement defined in the last step using New-AzNetworkManagerGroup.
244+
```azurepowershell-interactive
245+
$defn = @{
246+
Name = Get-UniqueString $networkgroup.Id
247+
Mode = 'Microsoft.Network.Data'
248+
Policy = $conditionalMembership
249+
}
250+
251+
$policyDefinition = New-AzPolicyDefinition $defn
252+
```
253+
254+
1. Assign the policy definition at a scope within your network managers scope for it to begin taking effect.
189255

190256
```azurepowershell-interactive
191-
$ng = @{
192-
Name = 'myNetworkGroup'
193-
ResourceGroupName = 'myAVNMResourceGroup'
194-
GroupMember = $groupMembers
195-
ConditionalMembership = $conditionalMembership
196-
NetworkManagerName = 'myAVNM'
197-
MemberType = 'Microsoft.Network/VirtualNetwork'
257+
$assgn = @{
258+
Name = Get-UniqueString $networkgroup.Id
259+
PolicyDefinition = $policyDefinition
198260
}
199-
$networkgroup = New-AzNetworkManagerGroup @ng
261+
262+
$policyAssignment = New-AzPolicyAssignment $assgn
200263
```
201-
264+
202265
## Create a configuration
203266
204267
1. Create a connectivity group item to add a network group to with New-AzNetworkManagerConnectivityGroupItem.
@@ -209,30 +272,30 @@ $virtualnetworkC | Set-AzVirtualNetwork
209272
}
210273
$groupItem = New-AzNetworkManagerConnectivityGroupItem @gi
211274
```
212-
275+
213276
1. Create a configuration group and add the group item from the previous step.
214277
215278
```azurepowershell-interactive
216279
[System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSNetworkManagerConnectivityGroupItem]]$configGroup = @()
217280
$configGroup.Add($groupItem)
218281
```
219-
282+
220283
1. Create the connectivity configuration with New-AzNetworkManagerConnectivityConfiguration.
221284
222285
```azurepowershell-interactive
223286
$config = @{
224287
Name = 'connectivityconfig'
225-
ResourceGroupName = 'myAVNMResourceGroup'
226-
NetworkManagerName = 'myAVNM'
288+
ResourceGroupName = $rg.Name
289+
NetworkManagerName = $networkManager.Name
227290
ConnectivityTopology = 'Mesh'
228291
AppliesToGroup = $configGroup
229292
}
230293
$connectivityconfig = New-AzNetworkManagerConnectivityConfiguration @config
231-
```
294+
```
232295
233296
## Commit deployment
234297
235-
Commit the configuration to the target regions with Deploy-AzNetworkManagerCommit.
298+
Commit the configuration to the target regions with Deploy-AzNetworkManagerCommit. This will trigger your configuration to begin taking effect.
236299
237300
```azurepowershell-interactive
238301
[System.Collections.Generic.List[string]]$configIds = @()
@@ -241,8 +304,8 @@ $configIds.add($connectivityconfig.id)
241304
$target.Add("westus")
242305
243306
$deployment = @{
244-
Name = 'myAVNM'
245-
ResourceGroupName = 'myAVNMResourceGroup'
307+
Name = $networkManager.Name
308+
ResourceGroupName = $rg.Name
246309
ConfigurationId = $configIds
247310
TargetLocation = $target
248311
CommitType = 'Connectivity'
@@ -277,36 +340,32 @@ If you no longer need the Azure Virtual Network Manager, you'll need to make sur
277340
1. Remove the connectivity configuration with Remove-AzNetworkManagerConnectivityConfiguration
278341
279342
```azurepowershell-interactive
280-
$removeconfig = @{
281-
Name = 'connectivityconfig'
282-
ResourceGroupName = 'myAVNMResourceGroup'
283-
NetworkManagerName = 'myAVNM'
284-
}
285-
Remove-AzNetworkManagerConnectivityConfiguration @removeconfig
343+
344+
Remove-AzNetworkManagerConnectivityConfiguration @connectivityconfig.Id
345+
346+
```
347+
2. Remove the policy resources with Remove-AzPolicy*
348+
349+
```azurepowershell-interactive
350+
351+
Remove-AzPolicyAssignment $policyAssignment.Id
352+
Remove-AzPolicyAssignment $policyDefinition.Id
353+
286354
```
287355
288-
1. Remove the network group with Remove-AzNetworkManagerGroup.
356+
3. Remove the network group with Remove-AzNetworkManagerGroup.
289357
290358
```azurepowershell-interactive
291-
$removegroup = @{
292-
Name = 'myNetworkGroup'
293-
ResourceGroupName = 'myAVNMResourceGroup'
294-
NetworkManagerName = 'myAVNM'
295-
}
296-
Remove-AzNetworkManagerGroup @removegroup
359+
Remove-AzNetworkManagerGroup $networkGroup.Id
297360
```
298361
299-
1. Delete the network manager instance with Remove-AzNetworkManager.
362+
4. Delete the network manager instance with Remove-AzNetworkManager.
300363
301364
```azurepowershell-interactive
302-
$removenetworkmanager = @{
303-
Name = 'myAVNM'
304-
ResourceGroupName = 'myAVNMResourceGroup'
305-
}
306-
Remove-AzNetworkManager @removenetworkmanager
365+
Remove-AzNetworkManager $networkManager.Id
307366
```
308367
309-
1. If you no longer need the resource created, delete the resource group with [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup).
368+
5. If you no longer need the resource created, delete the resource group with [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup).
310369
311370
```azurepowershell-interactive
312371
Remove-AzResourceGroup -Name 'myAVNMResourceGroup'
@@ -317,4 +376,4 @@ If you no longer need the Azure Virtual Network Manager, you'll need to make sur
317376
After you've created the Azure Virtual Network Manager, continue on to learn how to block network traffic by using the security admin configuration:
318377
319378
> [!div class="nextstepaction"]
320-
> [Block network traffic with security admin rules](how-to-block-network-traffic-powershell.md)
379+
> [Block network traffic with security admin rules](how-to-block-network-traffic-powershell.md)

0 commit comments

Comments
 (0)