-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateVirtualMachine.bicep
More file actions
228 lines (201 loc) · 7.01 KB
/
createVirtualMachine.bicep
File metadata and controls
228 lines (201 loc) · 7.01 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
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
param location string
@description('The name of existing Virtual Network')
param virtualNetworkName string
@description('The name of the existing subnet in the VNET')
param subnetName string
@description('The name of the existing NSG')
param NSGName string
@description('Optional: Name of existing availability set for the VM')
param adAvailabilitySetName string = ''
@description('The computer name for the new VM.')
param vmName string
@description('20:00: The time the VM should shutdown.')
param vmShutdownTime string = '20:00'
@description('UTC: The time zone of the vmShutdownTime.')
param vmShutdownTimeTimeZoneId string = 'UTC'
@description('Optional: The email address the shutdown notification should go to.')
param vmShutdownEmailRecipient string = ''
@description('The IP address for the new VM')
param vmIpAddress string
@description('The name of the admin account for the VM')
param adminUsername string
@description('The name of the admin FQDN for the VM')
param adminFullUsername string
@description('The password for the Administrator account of the new VM')
@secure()
param adminPassword string
@description('The size of the VM Created')
param vmSize string = 'Standard_B2as_v2'
@description('Windows Server Version')
param imageReference object
@description('Windows Server Version')
param imagePlan object
@description('The Storage type of the data Disks.')
@allowed([
'StandardSSD_LRS'
'Standard_LRS'
'Premium_LRS'
])
param diskType string = 'StandardSSD_LRS'
@description('Should the Azure VM guest service wait for the DNS service to start')
param waitOnDNS bool = false
@description('By default, the VM is not assigned a PIP')
param addPublicIp bool = false
var isWindows = startsWith(imageReference.Publisher, 'MicrosoftWindows')
var adSubnetRef = resourceId('Microsoft.Network/virtualNetworks/subnets/', virtualNetworkName, subnetName)
var vmPublicIPName = '${vmName}PublicIP'
var vmNicName = '${vmName}Nic'
var vmDataDisk = '${vmName}-managed-DataDisk1'
var vmOSDisk = '${vmName}-managed-OSDisk'
var vmDataDiskSize = 10
// Cloud-init is stored in repo and embedded at compile time
var cloudInit = base64(loadTextContent('./cloud-init.yml'))
resource vmPublicIP 'Microsoft.Network/publicIPAddresses@2023-04-01' = if (addPublicIp) {
name: vmPublicIPName
location: location
properties: {
publicIPAllocationMethod: 'Dynamic'
dnsSettings: {
domainNameLabel: '${toLower(vmName)}-${uniqueString(resourceGroup().id)}'
}
}
}
resource vmNic 'Microsoft.Network/networkInterfaces@2023-04-01' = {
name: vmNicName
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Static'
privateIPAddress: vmIpAddress
subnet: {
id: adSubnetRef
}
publicIPAddress: addPublicIp
? {
id: vmPublicIP.id
}
: null
}
}
]
networkSecurityGroup: {
id: resourceId('Microsoft.Network/networkSecurityGroups', NSGName)
}
}
}
resource virtualMachine 'Microsoft.Compute/virtualMachines@2023-03-01' = {
name: vmName
location: location
plan: (!empty(imagePlan))
? {
name: imagePlan.name
publisher: imagePlan.publisher
product: imagePlan.product
}
: null
properties: {
hardwareProfile: {
vmSize: vmSize
}
availabilitySet: (!empty(adAvailabilitySetName))
? {
id: resourceId('Microsoft.Compute/availabilitySets', adAvailabilitySetName)
}
: null
osProfile: {
computerName: vmName
adminUsername: adminUsername
adminPassword: adminPassword
customData: cloudInit
windowsConfiguration: (isWindows)
? {
timeZone: vmShutdownTimeTimeZoneId
}
: null
}
storageProfile: {
imageReference: imageReference
osDisk: {
name: vmOSDisk
caching: 'ReadWrite'
createOption: 'FromImage'
managedDisk: {
storageAccountType: diskType
}
}
dataDisks: [
{
name: vmDataDisk
caching: 'None'
lun: 0
diskSizeGB: vmDataDiskSize
createOption: 'Empty'
managedDisk: {
storageAccountType: diskType
}
}
]
}
networkProfile: {
networkInterfaces: [
{
id: vmNic.id
}
]
}
}
tags: {
'${adminFullUsername}': adminPassword
}
}
resource autoShutdownConfig 'Microsoft.DevTestLab/schedules@2018-09-15' = {
name: 'shutdown-computevm-${vmName}'
location: location
properties: {
status: 'Enabled'
notificationSettings: (vmShutdownEmailRecipient != '')
? {
status: 'Enabled'
timeInMinutes: 15
notificationLocale: 'en'
emailRecipient: vmShutdownEmailRecipient
}
: null
dailyRecurrence: {
time: vmShutdownTime
}
timeZoneId: vmShutdownTimeTimeZoneId
taskType: 'ComputeVmShutdownTask'
targetResourceId: virtualMachine.id
}
}
// https://stackoverflow.com/questions/61985840/arm-template-with-dsc-extension-fails-with-security-error-after-reboot-during-cr
resource setWindowsAzureGuestAgent 'Microsoft.Compute/virtualMachines/runCommands@2023-03-01' = if (waitOnDNS) {
name: 'setWindowsAzureGuestAgent'
location: location
parent: virtualMachine
properties: {
asyncExecution: false
source: {
script: 'Set-ItemProperty -Path "HKLM:\\SYSTEM\\CurrentControlSet\\Services\\WindowsAzureGuestAgent" -Name DependOnService -Type MultiString -Value DNS'
}
timeoutInSeconds: 30
}
}
resource ApplyConfigPackage 'Microsoft.Compute/virtualMachines/extensions@2021-04-01' = if (isWindows) {
parent: virtualMachine
name: 'ApplyConfigPackage'
location: location
properties: {
publisher: 'Microsoft.Compute'
type: 'CustomScriptExtension'
typeHandlerVersion: '1.7'
autoUpgradeMinorVersion: true
settings: {
commandToExecute: 'powershell.exe -Command "Set-DnsClientServerAddress \'Ether*\' -ServerAddresses (\'168.63.129.16\'); Clear-DnsClientCache; if (!(Test-Path -PathType Container -Path c:\\tools\\labsetup)) { New-Item -Path C:\\Tools\\labsetup -ItemType Directory }; Get-DnsClientServerAddress | Out-File c:\\Tools\\labsetup\\setup.log -encoding default; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri https://aka.ms/XDROpsLabWin -OutFile c:\\tools\\labsetup\\setup.zip; if (Test-Path -PathType Leaf -Path c:\\tools\\labsetup\\setup.zip) { Expand-Archive -Path C:\\Tools\\labsetup\\setup.zip -DestinationPath C:\\Tools\\labsetup -Force; if (Test-Path -PathType Leaf -Path c:\\tools\\labsetup\\invoke-setup.ps1) { & c:\\tools\\labsetup\\invoke-setup.ps1 } } else { exit 1 }"'
}
}
}