-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm.bicep
233 lines (211 loc) · 6.17 KB
/
vm.bicep
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
@description('Location for all resources.')
param location string = resourceGroup().location
@description('Types of VMs to deploy.')
@allowed([
'hostPool'
'firstVersion'
'newVersion'
])
param vmType string
@description('Size of the VM.')
@allowed([
'Standard_DS1_v2'
'Standard_D2s_v3'
'Standard_D8s_v5'
'Standard_F8s_v2'
'Standard_D8as_v4'
'Standard_D16s_v5'
])
param vmSize string = 'Standard_DS1_v2'
@description('VM name prefix')
param vmName string = 'imagevm'
@description('Number of VMs to deploy.')
param vmCount int = 1
@description('VM administrator username needed for vmType hostPool or firstVersion')
param adminUserName string = ''
@description('VM administrator password needed for vmType hostPool or firstVersion')
@secure()
param adminPassword string = ''
@description('Snapshot id needed for vmType newVersion')
param snapshotId string = ''
@description('OS image version id needed for vmType hostPool')
param osImageId string = ''
@description('Hostpool registration token is needed for vmType hostPool')
param registrationToken string = ''
@description('Storage account name is needed for vmType hostPool')
param storageAccountName string = ''
@description('Storage account key is needed for vmType hostPool')
@secure()
param storageAccountKey string = ''
@description('setWVDClient.ps1 powershell script URI is needed for vmType hostPool')
param fileUri string = ''
@description('Id of destination subnet for the VM')
param subnetId string
@description('Definition of the properties for each kind of VM')
var properties = [for i in range(0, vmCount): {
hostPool: {
hardwareProfile: {
vmSize: vmSize
}
networkProfile: networkProfile[i]
storageProfile: {
imageReference: {
id: osImageId
}
osDisk: {
osType: 'Windows'
createOption: 'FromImage'
caching: 'ReadWrite'
managedDisk: {
storageAccountType: 'Premium_LRS'
}
deleteOption: 'Delete'
}
}
osProfile: osProfile[i]
licenseType: 'Windows_Client'
}
firstVersion: {
hardwareProfile: {
vmSize: vmSize
}
networkProfile: networkProfile[i]
storageProfile:{
imageReference: {
publisher: 'MicrosoftWindowsDesktop'
offer: 'windows-11'
sku: 'win11-22h2-avd'
version: 'latest'
}
osDisk: {
osType: 'Windows'
createOption: 'FromImage'
caching: 'None'
managedDisk: {
storageAccountType: 'Premium_LRS'
}
deleteOption: 'Delete'
}
}
osProfile: osProfile[i]
}
newVersion: {
hardwareProfile: {
vmSize: vmSize
}
networkProfile: networkProfile[i]
storageProfile: {
osDisk: {
osType: 'Windows'
createOption: 'Attach'
caching: 'None'
managedDisk: {
storageAccountType: 'Premium_LRS'
id: disk[i].id
}
deleteOption: 'Delete'
}
}
}
}]
@description('networkProfile is the same for all VM kinds')
var networkProfile = [for i in range(0, vmCount): {
networkInterfaces: [
{
id: networkInterface[i].id
properties: {
deleteOption: 'Delete'
}
}
]
}]
@description('osProfile is the same for VM kinds hostPool and firstVersion')
var osProfile = [for i in range(0, vmCount): {
computerName: '${vmName}-${i}'
adminUsername: adminUserName
adminPassword: adminPassword
windowsConfiguration: {
provisionVMAgent: true
enableAutomaticUpdates: false
patchSettings: {
patchMode: 'Manual'
assessmentMode: 'ImageDefault'
}
enableVMAgentPlatformUpdates: false
}
allowExtensionOperations: true
}]
resource virtualMachine 'Microsoft.Compute/virtualMachines@2023-03-01' = [for i in range(0, vmCount): {
name: '${vmName}-${i}'
location: location
identity:{
type: (vmType == 'hostPool') ? 'SystemAssigned' : 'None'
}
properties: properties[i][vmType]
}]
resource AADLoginForWindows 'Microsoft.Compute/virtualMachines/extensions@2022-11-01' = [for i in range(0, vmCount): if (vmType == 'hostPool') {
name: 'AADLoginForWindows'
parent: virtualMachine[i]
location: location
properties: {
publisher: 'Microsoft.Azure.ActiveDirectory'
type: 'AADLoginForWindows'
typeHandlerVersion: '1.0'
autoUpgradeMinorVersion: true
}
}]
resource customScriptExtension 'Microsoft.Compute/virtualMachines/extensions@2022-11-01' = [for i in range(0, vmCount): if (vmType == 'hostPool') {
name: 'CustomScriptExtension'
parent: virtualMachine[i]
location: location
properties: {
publisher: 'Microsoft.Compute'
type: 'CustomScriptExtension'
typeHandlerVersion: '1.10'
autoUpgradeMinorVersion: true
protectedSettings: {
commandToExecute: 'powershell -ExecutionPolicy Unrestricted -File setWVDClient.ps1 -registrationtoken ${registrationToken}'
storageAccountName: storageAccountName
storageAccountKey: storageAccountKey
fileUris: [
fileUri
]
}
}
} ]
resource networkInterface 'Microsoft.Network/networkInterfaces@2022-11-01' = [for i in range(0, vmCount): {
name: '${vmName}-${i}-VMNic'
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig${vmName}-${i}'
type: 'Microsoft.Network/networkInterfaces/ipConfigurations'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: subnetId
}
primary: true
privateIPAddressVersion: 'IPv4'
}
}
]
}
}]
resource disk 'Microsoft.Compute/disks@2022-07-02' = [for i in range(0, vmCount): if (vmType == 'newVersion') {
name: '${vmName}-${i}-osDisk_1'
location: location
sku: {
name: 'Premium_LRS'
}
properties: {
hyperVGeneration: 'V2'
creationData: {
createOption: 'Copy'
sourceResourceId: snapshotId
}
}
}]
output vmId string = virtualMachine[0].id
output osDiskId string = virtualMachine[0].properties.storageProfile.osDisk.managedDisk.id