Skip to content

Commit 23caa4f

Browse files
committed
added posh and CLI commands to file
1 parent 27e3b38 commit 23caa4f

File tree

1 file changed

+341
-18
lines changed

1 file changed

+341
-18
lines changed

articles/virtual-network/quickstart-create-virtual-network.md

Lines changed: 341 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ If you don't have a service subscription, [create a free trial account](https://
3333

3434
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3535

36-
[!INCLUDE [cloud-shell-try-it.md](~/reusable-content/ce-skilling/azure/includes/cloud-shell-try-it.md)]
36+
- Azure Cloud Shell or Azure PowerShell.
3737

38-
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 1.0.0 or later. Run `Get-Module -ListAvailable Az` to find the installed version. If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-azure-powershell). If you're running PowerShell locally, you also need to run `Connect-AzAccount` to create a connection with Azure.
38+
The steps in this quickstart run the Azure PowerShell cmdlets interactively in [Azure Cloud Shell](/azure/cloud-shell/overview). To run the commands in the Cloud Shell, select **Open Cloudshell** at the upper-right corner of a code block. Select **Copy** to copy the code, and then paste it into Cloud Shell to run it. You can also run Cloud Shell from within the Azure portal.
39+
40+
You can also [install Azure PowerShell locally](/powershell/azure/install-azure-powershell) to run the cmdlets. The steps in this article require Azure PowerShell module version 5.4.1 or later. Run `Get-Module -ListAvailable Az` to find your installed version. If you need to upgrade, see [Update the Azure PowerShell module](/powershell/azure/install-Az-ps#update-the-azure-powershell-module).
41+
42+
If you run PowerShell locally, run `Connect-AzAccount` to connect to Azure.
3943

4044
### [CLI](#tab/cli)
4145

@@ -74,6 +78,341 @@ Sign in to the [Azure portal](https://portal.azure.com) with your Azure account.
7478

7579
[!INCLUDE [create-two-virtual-machines.md](../../includes/create-two-virtual-machines.md)]
7680

81+
### [Powershell](#tab/powershell)
82+
83+
## Create a resource group
84+
85+
Use [New-AzResourceGroup](/powershell/module/az.Resources/New-azResourceGroup) to create a resource group to host the virtual network. Run the following code to create a resource group named **test-rg** in the **eastus2** Azure region:
86+
87+
```azurepowershell-interactive
88+
$rg = @{
89+
Name = 'test-rg'
90+
Location = 'eastus2'
91+
}
92+
New-AzResourceGroup @rg
93+
```
94+
95+
## Create a virtual network
96+
97+
1. Use [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork) to create a virtual network named **vnet-1** with IP address prefix **10.0.0.0/16** in the **test-rg** resource group and **eastus2** location:
98+
99+
```azurepowershell-interactive
100+
$vnet = @{
101+
Name = 'vnet-1'
102+
ResourceGroupName = 'test-rg'
103+
Location = 'eastus2'
104+
AddressPrefix = '10.0.0.0/16'
105+
}
106+
$virtualNetwork = New-AzVirtualNetwork @vnet
107+
```
108+
109+
1. Azure deploys resources to a subnet within a virtual network. Use [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig) to create a subnet configuration named **subnet-1** with address prefix **10.0.0.0/24**:
110+
111+
```azurepowershell-interactive
112+
$subnet = @{
113+
Name = 'subnet-1'
114+
VirtualNetwork = $virtualNetwork
115+
AddressPrefix = '10.0.0.0/24'
116+
}
117+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnet
118+
```
119+
120+
1. Associate the subnet configuration to the virtual network by using [Set-AzVirtualNetwork](/powershell/module/az.network/Set-azVirtualNetwork):
121+
122+
```azurepowershell-interactive
123+
$virtualNetwork | Set-AzVirtualNetwork
124+
```
125+
126+
## Deploy Azure Bastion
127+
128+
Azure Bastion uses your browser to connect to VMs in your virtual network over Secure Shell (SSH) or Remote Desktop Protocol (RDP) by using their private IP addresses. The VMs don't need public IP addresses, client software, or special configuration. For more information about Bastion, see [What is Azure Bastion?](/azure/bastion/bastion-overview).
129+
130+
[!INCLUDE [Pricing](~/reusable-content/ce-skilling/azure/includes/bastion-pricing.md)]
131+
132+
1. Configure a Bastion subnet for your virtual network. This subnet is reserved exclusively for Bastion resources and must be named **AzureBastionSubnet**.
133+
134+
```azurepowershell-interactive
135+
$subnet = @{
136+
Name = 'AzureBastionSubnet'
137+
VirtualNetwork = $virtualNetwork
138+
AddressPrefix = '10.0.1.0/26'
139+
}
140+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnet
141+
```
142+
143+
1. Set the configuration:
144+
145+
```azurepowershell-interactive
146+
$virtualNetwork | Set-AzVirtualNetwork
147+
```
148+
149+
1. Create a public IP address for Bastion. The Bastion host uses the public IP to access SSH and RDP over port 443.
150+
151+
```azurepowershell-interactive
152+
$ip = @{
153+
ResourceGroupName = 'test-rg'
154+
Name = 'public-ip'
155+
Location = 'eastus2'
156+
AllocationMethod = 'Static'
157+
Sku = 'Standard'
158+
Zone = 1,2,3
159+
}
160+
New-AzPublicIpAddress @ip
161+
```
162+
163+
1. Use the [New-AzBastion](/powershell/module/az.network/new-azbastion) command to create a new Standard SKU Bastion host in **AzureBastionSubnet**:
164+
165+
```azurepowershell-interactive
166+
$bastion = @{
167+
Name = 'bastion'
168+
ResourceGroupName = 'test-rg'
169+
PublicIpAddressRgName = 'test-rg'
170+
PublicIpAddressName = 'public-ip'
171+
VirtualNetworkRgName = 'test-rg'
172+
VirtualNetworkName = 'vnet-1'
173+
Sku = 'Basic'
174+
}
175+
New-AzBastion @bastion
176+
```
177+
178+
It takes about 10 minutes to deploy the Bastion resources. You can create VMs in the next section while Bastion deploys to your virtual network.
179+
180+
## Create virtual machines
181+
182+
Use [New-AzVM](/powershell/module/az.compute/new-azvm) to create two VMs named **vm-1** and **vm-2** in the **subnet-1** subnet of the virtual network. When you're prompted for credentials, enter usernames and passwords for the VMs.
183+
184+
1. To create the first VM, use the following code:
185+
186+
```azurepowershell-interactive
187+
# Set the administrator and password for the VM. ##
188+
$cred = Get-Credential
189+
190+
## Place the virtual network into a variable. ##
191+
$vnet = Get-AzVirtualNetwork -Name 'vnet-1' -ResourceGroupName 'test-rg'
192+
193+
## Create a network interface for the VM. ##
194+
$nic = @{
195+
Name = "nic-1"
196+
ResourceGroupName = 'test-rg'
197+
Location = 'eastus2'
198+
Subnet = $vnet.Subnets[0]
199+
}
200+
$nicVM = New-AzNetworkInterface @nic
201+
202+
## Create a virtual machine configuration. ##
203+
$vmsz = @{
204+
VMName = "vm-1"
205+
VMSize = 'Standard_DS1_v2'
206+
}
207+
$vmos = @{
208+
ComputerName = "vm-1"
209+
Credential = $cred
210+
}
211+
$vmimage = @{
212+
PublisherName = 'Canonical'
213+
Offer = '0001-com-ubuntu-server-jammy'
214+
Skus = '22_04-lts-gen2'
215+
Version = 'latest'
216+
}
217+
$vmConfig = New-AzVMConfig @vmsz `
218+
| Set-AzVMOperatingSystem @vmos -Linux `
219+
| Set-AzVMSourceImage @vmimage `
220+
| Add-AzVMNetworkInterface -Id $nicVM.Id
221+
222+
## Create the VM. ##
223+
$vm = @{
224+
ResourceGroupName = 'test-rg'
225+
Location = 'eastus2'
226+
VM = $vmConfig
227+
}
228+
New-AzVM @vm
229+
```
230+
231+
1. To create the second VM, use the following code:
232+
233+
```azurepowershell-interactive
234+
# Set the administrator and password for the VM. ##
235+
$cred = Get-Credential
236+
237+
## Place the virtual network into a variable. ##
238+
$vnet = Get-AzVirtualNetwork -Name 'vnet-1' -ResourceGroupName 'test-rg'
239+
240+
## Create a network interface for the VM. ##
241+
$nic = @{
242+
Name = "nic-2"
243+
ResourceGroupName = 'test-rg'
244+
Location = 'eastus2'
245+
Subnet = $vnet.Subnets[0]
246+
}
247+
$nicVM = New-AzNetworkInterface @nic
248+
249+
## Create a virtual machine configuration. ##
250+
$vmsz = @{
251+
VMName = "vm-2"
252+
VMSize = 'Standard_DS1_v2'
253+
}
254+
$vmos = @{
255+
ComputerName = "vm-2"
256+
Credential = $cred
257+
}
258+
$vmimage = @{
259+
PublisherName = 'Canonical'
260+
Offer = '0001-com-ubuntu-server-jammy'
261+
Skus = '22_04-lts-gen2'
262+
Version = 'latest'
263+
}
264+
$vmConfig = New-AzVMConfig @vmsz `
265+
| Set-AzVMOperatingSystem @vmos -Linux `
266+
| Set-AzVMSourceImage @vmimage `
267+
| Add-AzVMNetworkInterface -Id $nicVM.Id
268+
269+
## Create the VM. ##
270+
$vm = @{
271+
ResourceGroupName = 'test-rg'
272+
Location = 'eastus2'
273+
VM = $vmConfig
274+
}
275+
New-AzVM @vm
276+
```
277+
278+
> [!TIP]
279+
> You can use the `-AsJob` option to create a VM in the background while you continue with other tasks. For example, run `New-AzVM @vm1 -AsJob`. When Azure starts creating the VM in the background, you get something like the following output:
280+
>
281+
> ```powershell
282+
> Id Name PSJobTypeName State HasMoreData Location Command
283+
> -- ---- ------------- ----- ----------- -------- -------
284+
> 1 Long Running... AzureLongRun... Running True localhost New-AzVM
285+
> ```
286+
287+
Azure takes a few minutes to create the VMs. When Azure finishes creating the VMs, it returns output to PowerShell.
288+
289+
> [!NOTE]
290+
> VMs in a virtual network with a Bastion host don't need public IP addresses. Bastion provides the public IP, and the VMs use private IPs to communicate within the network. You can remove the public IPs from any VMs in Bastion-hosted virtual networks. For more information, see [Dissociate a public IP address from an Azure VM](ip-services/remove-public-ip-address-vm.md).
291+
292+
[!INCLUDE [ephemeral-ip-note.md](~/reusable-content/ce-skilling/azure/includes/ephemeral-ip-note.md)]
293+
294+
### [CLI](#tab/cli)
295+
296+
## Create a resource group
297+
298+
Use [az group create](/cli/azure/group#az-group-create) to create a resource group to host the virtual network. Use the following code to create a resource group named **test-rg** in the **eastus2** Azure region:
299+
300+
```azurecli-interactive
301+
az group create \
302+
--name test-rg \
303+
--location eastus2
304+
```
305+
306+
## Create a virtual network and subnet
307+
308+
Use [az network vnet create](/cli/azure/network/vnet#az-network-vnet-create) to create a virtual network named **vnet-1** with a subnet named **subnet-1** in the **test-rg** resource group:
309+
310+
```azurecli-interactive
311+
az network vnet create \
312+
--name vnet-1 \
313+
--resource-group test-rg \
314+
--address-prefix 10.0.0.0/16 \
315+
--subnet-name subnet-1 \
316+
--subnet-prefixes 10.0.0.0/24
317+
```
318+
319+
## Deploy Azure Bastion
320+
321+
Azure Bastion uses your browser to connect to VMs in your virtual network over Secure Shell (SSH) or Remote Desktop Protocol (RDP) by using their private IP addresses. The VMs don't need public IP addresses, client software, or special configuration.
322+
323+
[!INCLUDE [Pricing](~/reusable-content/ce-skilling/azure/includes/bastion-pricing.md)] For more information about Bastion, see [What is Azure Bastion?](~/articles/bastion/bastion-overview.md).
324+
325+
1. Use [az network vnet subnet create](/cli/azure/network/vnet/subnet#az-network-vnet-subnet-create) to create a Bastion subnet for your virtual network. This subnet is reserved exclusively for Bastion resources and must be named **AzureBastionSubnet**.
326+
327+
```azurecli-interactive
328+
az network vnet subnet create \
329+
--name AzureBastionSubnet \
330+
--resource-group test-rg \
331+
--vnet-name vnet-1 \
332+
--address-prefix 10.0.1.0/26
333+
```
334+
335+
1. Create a public IP address for Bastion. This IP address is used to connect to the Bastion host from the internet. Use [az network public-ip create](/cli/azure/network/public-ip#az-network-public-ip-create) to create a public IP address named **public-ip** in the **test-rg** resource group:
336+
337+
```azurecli-interactive
338+
az network public-ip create \
339+
--resource-group test-rg \
340+
--name public-ip \
341+
--sku Standard \
342+
--location eastus2 \
343+
--zone 1 2 3
344+
```
345+
346+
1. Use [az network bastion create](/cli/azure/network/bastion#az-network-bastion-create) to create a Bastion host in **AzureBastionSubnet** for your virtual network:
347+
348+
```azurecli-interactive
349+
az network bastion create \
350+
--name bastion \
351+
--public-ip-address public-ip \
352+
--resource-group test-rg \
353+
--vnet-name vnet-1 \
354+
--location eastus2
355+
```
356+
357+
It takes about 10 minutes to deploy the Bastion resources. You can create VMs in the next section while Bastion deploys to your virtual network.
358+
359+
## Create virtual machines
360+
361+
Use [az vm create](/cli/azure/vm#az-vm-create) to create two VMs named **vm-1** and **vm-2** in the **subnet-1** subnet of the virtual network. When you're prompted for credentials, enter user names and passwords for the VMs.
362+
363+
1. To create the first VM, use the following command:
364+
365+
```azurecli-interactive
366+
az vm create \
367+
--resource-group test-rg \
368+
--admin-username azureuser \
369+
--authentication-type password \
370+
--name vm-1 \
371+
--image Ubuntu2204 \
372+
--public-ip-address ""
373+
```
374+
375+
1. To create the second VM, use the following command:
376+
377+
```azurecli-interactive
378+
az vm create \
379+
--resource-group test-rg \
380+
--admin-username azureuser \
381+
--authentication-type password \
382+
--name vm-2 \
383+
--image Ubuntu2204 \
384+
--public-ip-address ""
385+
```
386+
387+
> [!TIP]
388+
> You can also use the `--no-wait` option to create a VM in the background while you continue with other tasks.
389+
390+
The VMs take a few minutes to create. After Azure creates each VM, the Azure CLI returns output similar to the following message:
391+
392+
```output
393+
{
394+
"fqdns": "",
395+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Compute/virtualMachines/vm-2",
396+
"location": "eastus2",
397+
"macAddress": "00-0D-3A-23-9A-49",
398+
"powerState": "VM running",
399+
"privateIpAddress": "10.0.0.5",
400+
"publicIpAddress": "",
401+
"resourceGroup": "test-rg"
402+
"zones": ""
403+
}
404+
```
405+
406+
> [!NOTE]
407+
> VMs in a virtual network with a Bastion host don't need public IP addresses. Bastion provides the public IP, and the VMs use private IPs to communicate within the network. You can remove the public IPs from any VMs in Bastion-hosted virtual networks. For more information, see [Dissociate a public IP address from an Azure VM](ip-services/remove-public-ip-address-vm.md).
408+
409+
[!INCLUDE [ephemeral-ip-note.md](~/reusable-content/ce-skilling/azure/includes/ephemeral-ip-note.md)]
410+
411+
### [ARM](#tab/arm)
412+
413+
414+
---
415+
77416
## Connect to a virtual machine
78417

79418
1. In the portal, search for and select **Virtual machines**.
@@ -122,22 +461,6 @@ Sign in to the [Azure portal](https://portal.azure.com) with your Azure account.
122461
123462
1. Close the Bastion connection to **vm-2**.
124463
125-
[!INCLUDE [portal-clean-up.md](~/reusable-content/ce-skilling/azure/includes/portal-clean-up.md)]
126-
127-
128-
### [Powershell](#tab/powershell)
129-
130-
### [CLI](#tab/cli)
131-
132-
### [ARM](#tab/arm)
133-
134-
### [Bicep](#tab/bicep)
135-
136-
### [Terraform](#tab/terraform)
137-
138-
---
139-
140-
141464
## Clean up resources
142465
143466

0 commit comments

Comments
 (0)