-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCreate_VM_add_existing_virtual_Network.ps1
More file actions
59 lines (46 loc) · 1.64 KB
/
Create_VM_add_existing_virtual_Network.ps1
File metadata and controls
59 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzSubscription -SubscriptionName "MSDN Platforms" | Select-AzSubscription
Get-AzContext
#Search for the resource groups
Get-AzResourceGroup | Format-Table
#Whats in a specific resource group
Get-AzResource -ResourceGroupName tw-azuredemo-rg | Format-Table
#Some variables
$RGName = "tw-azuredemo-rg"
$VnetName = "tw-vnet-workload"
$Location = "westeurope"
$VMName = "twsrv2021"
$credential = Get-Credential
#We need all infos about the virtual network
$VirtualNetwork = (Get-AzVirtualNetwork -Name $VnetName -ResourceGroupName $RGName)
#Let's have a look at the variable
$VirtualNetwork
#Create a network interface
$nic = New-AzNetworkInterface `
-ResourceGroupName $RGName `
-Name "twsrv2021-nic" `
-Location $Location `
-SubnetId $VirtualNetwork.Subnets[0].Id
#Define your VM
$vmConfig = New-AzVMConfig -VMName $VMName -VMSize "Standard_D2s_v4"
#Create the rest of your VM configuration
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig `
-Windows `
-ComputerName $VMName `
-Credential $credential `
-ProvisionVMAgent `
-EnableAutoUpdate
$vmConfig = Set-AzVMSourceImage -VM $vmConfig `
-PublisherName "MicrosoftWindowsServer" `
-Offer "WindowsServer" `
-Skus "2016-Datacenter" `
-Version "latest"
#Attach the network interface that you previously created
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
#Create your VM
New-AzVM -VM $vmConfig -ResourceGroupName $RGName -Location $Location