forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure-rm-vm-create.ps1
374 lines (326 loc) · 14.2 KB
/
azure-rm-vm-create.ps1
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<#
script to create multiple vm's into existing azure rm infrastructure
to enable script execution, you may need to Set-ExecutionPolicy Bypass -Force
Copyright 2017 Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#170914
#>
param(
[parameter(Mandatory = $true, HelpMessage = "Enter admin user name: ex:vmadmin")]
[string]$adminUsername = "",
[parameter(Mandatory = $true, HelpMessage = "Enter admin password complex 13 char:")]
[string]$adminPassword = "",
[switch]$enumerateSub,
[switch]$force,
[string]$galleryImage = "2016-Datacenter",
[parameter(Mandatory = $true, HelpMessage = "Enter location: ex:eastus")]
[string]$location = "",
[string]$offername = "WindowsServer",
[switch]$publicIp,
[string]$pubName = "MicrosoftWindowsServer",
[parameter(Mandatory = $true, HelpMessage = "Enter resource group name:")]
[string]$resourceGroupName,
[string]$StorageAccountName,
[string]$StorageType = "Standard_GRS",
[string]$subnetName = "",
[string]$subscription,
[string]$vmBaseName = "tpl",
[int]$vmCount = 1,
[string]$vmSize = "Standard_A2",
[int]$vmStartCount = 1,
[string]$VNetAddressPrefix = "10.0.0.0/16",
[string]$VNetSubnetAddressPrefix = "10.0.0.0/24",
[string]$vnetName = ""
)
$vnetNamePrefix = "vnet"
$subnetNamePrefix = "subnet"
$storagePrefix = "storage"
$global:credential
$global:storageAccount
$global:vnet
# ----------------------------------------------------------------------------------------------------------------
function main()
{
manage-credential
authenticate-azureRm
if ($enumerateSub)
{
enum-subscription
return
}
if (!($location) -or `
!($resourceGroupName) -or `
!($galleryImage) -or `
!($pubName) -or `
!($VMSize))
{
write-host "missing required argument"
turn
}
# check to make sure vm doesnt exist
$i = $startCount
$jobs = @()
$newVmNames = new-object Collections.ArrayList
$Error.Clear()
$resourceGroupName = check-resourceGroupName -resourceGroupName $resourceGroupName
$storageAccountName = check-storageAccountName -resourceGroupName $resourceGroupName -storageAccountName $storageAccountName
$vnetName = check-vnetName -resourceGroupName $resourceGroupName -vnetName $vnetName
$subnetName = check-subnetName -resourceGroupName $resourceGroupName -vnetName $vnetName -subnetName $subnetName
for ($i = $vmstartCount; $i -lt $vmstartcount + $VMCount; $i++)
{
$newVmName = "$($vmBaseName)-$($i.ToString("D2"))"
if (Get-AzureRMVM -resourceGroupName $resourceGroupName -Name $newVMName -ErrorAction SilentlyContinue)
{
Write-Host "vm already exists $newVMName. skipping..."
}
else
{
write-host "adding new machine name to list: $($newvmName)"
$newVmNames.Add($newVmName)
}
}
foreach ($VMName in $newVMNames)
{
# todo make concurrent with start-job?
# would need cert conn to azure
Write-Host "creating vm $VMName"
$OSDiskName = $VMName + "OSDisk"
$InterfaceName = "$($vmName)Interface1"
# Network
if ($publicIp)
{
write-host "creating public ip"
$PIp = New-AzureRmPublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $global:vnet.Subnets[0].Id -PublicIpAddressId $PIp.Id
}
else
{
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $global:vnet.Subnets[0].Id
}
# Compute
## Setup local VM object
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VMName -Credential $global:Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName $pubName -Offer $offerName -Skus $galleryImage -Version "latest"
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$OSDiskUri = $global:StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
## Create the VM in Azure
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine
}
write-host "finished"
return $newVmNames
}
# ----------------------------------------------------------------------------------------------------------------
function authenticate-azureRm()
{
# authenticate
if(!(Get-AzureRmResourceGroup))
{
$error.Clear()
if(!(connect-azurermaccount))
{
log-info "exception authenticating. exiting $($error | out-string)" -ForegroundColor Yellow
exit 1
}
}
if ($force)
{
connect-azurermaccount
}
#if($force -or (!($adminPassword) -or !($adminUsername)))
#{
if ($global:credential -eq $null)
{
$global:Credential = Get-Credential
}
#}
}
# ----------------------------------------------------------------------------------------------------------------
function check-resourceGroupName($resourceGroupName)
{
if (!($resourceGroupName) -and @(Get-AzureRmResourceGroup).Count -eq 1)
{
$resourceGroupName = (Get-AzureRmResourceGroup).Name
}
elseif (!($resourceGroupName))
{
write-host (Get-AzureRmResourceGroup | fl * | out-string)
$resourceGroupName = read-host "enter resource group"
}
elseif (!(Get-AzureRmResourceGroup $resourceGroupName))
{
write-host "creating resource group: $($resourceGroupName)"
New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location
}
return $resourceGroupName
}
# ----------------------------------------------------------------------------------------------------------------
function check-storageAccountName($resourceGroupName, $StorageAccountName)
{
# see if only one storage account if name empty and use that
if (!($storageAccountName) -and @(Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName).Count -eq 1)
{
$global:storageAccount = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName
write-host = "using default storage account:$($storageAccount.Name)"
}
elseif (!($storageAccountName) -and @(Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName).Count -gt 1)
{
$storageList = @{}
$count = 1
foreach ($storageName in Get-AzureRmStorageAccount -resourcegroupname $resourcegroupname)
{
$storageList.Add($count, $storageName.StorageAccountName)
write-host "$($count). $($storageName.StorageAccountName)"
$count++
}
$storageAccountNumber = [int](read-host "enter number of storage account to use or '0' to create new storage account:")
if ($storageAccountNumber -eq 0)
{
$storageAccountName = "vm$((get-random).ToString("D9"))"
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName `
-Location $Location `
-Kind Storage `
-SkuName $StorageType `
-Name $storageAccountName
}
elseif ($storageAccountNumber)
{
$storageAccountName = $storageList.Item($storageAccountNumber)
}
else
{
Write-Error "no storage selected. exiting"
exit 1
}
$global:storageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $resourceGroupName
}
elseif ((!($storageAccountName) -and @(Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName).Count -lt 1) `
-or !(Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $resourceGroupName))
{
if (!($storageAccountName))
{
$storageAccountName = ("$($storagePrefix)$($resourceGroupName)").ToLower()
$storageAccountName = $storageAccountName.Substring(0, [Math]::Min($storageAccountName.Length, 23))
}
write-host "creating storage account: $($storageAccountName)"
$global:StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Type $StorageType -Location $Location
}
elseif ((Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $resourceGroupName))
{
$global:StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
}
elseif (!(Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $resourceGroupName))
{
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName `
-Location $Location `
-Kind Storage `
-SkuName $StorageType `
-Name $storageAccountName
}
else
{
write-host "need storage account name. exiting"
exit 1
}
write-host "using name: $($storageAccountName)"
return $storageAccountName
}
# ----------------------------------------------------------------------------------------------------------------
function check-vnetName($resourceGroupName, $vnetName)
{
# see if only one vnet if name empty and use that
if (!($vnetName) -and @(Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName).Count -eq 1)
{
$global:vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName
write-host = "using default vnet:$($vnet.Name)"
}
elseif (!(Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $resourceGroupName))
{
if (!($VNetName))
{
$VNetName = "$vnetNamePrefix$($resourceGroupName)"
}
if (!($subnetName))
{
$subNetName = "$subnetNamePrefix$($resourceGroupName)"
}
write-host "creating vnet: $($vnetName)"
write-host "creating subnet: $($subnetName)"
$SubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix
$global:vnet = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig
}
else
{
$global:vnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName
}
return $vnetName
}
# ----------------------------------------------------------------------------------------------------------------
function check-subnetName($resourceGroupName, $vnetName, $subnetName)
{
# see if only one subnet if name empty and use that
if (!($subnetName) -and @(Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).Count -eq 1)
{
$subnetConfig = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet
write-host = "using default subnet:$($subnetConfig.Name)"
}
elseif (!(Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnetName))
{
write-host "creating subnet: $($subnetName)"
$SubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix
$vnet.Subnets.Add($SubnetConfig)
}
else
{
$SubnetConfig = Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork (Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName)
}
}
# ----------------------------------------------------------------------------------------------------------------
function enum-subscription()
{
If (!($location))
{
write-host "AVAILABLE LOCATIONS:" -ForegroundColor Green
write-host (Get-AzureRmLocation | fl * | out-string)
write-host "need location:exiting"
return
}
write-host "CURRENT SUBSCRIPTION:" -ForegroundColor Green
Get-AzureRmSubscription
write-host "CURRENT VMS:" -ForegroundColor Green
Get-AzureRmVM | out-gridview
write-host "AVAILABLE LOCATIONS:" -ForegroundColor Green
Get-AzureRmLocation | out-gridview
write-host "AVAILABLE IMAGES:" -ForegroundColor Green
Get-AzureRMVMImage -Location $location -PublisherName $pubName -Offer $offerName -Skus $galleryImage | out-gridview
write-host "AVAILABLE ROLES:" -ForegroundColor Green
Get-AzureRoleSize | out-gridview
write-host "AVAILABLE STORGE:" -ForegroundColor Green
Get-AzureRmStorageAccount | out-gridview
write-host "AVAILABLE NETWORKS:" -ForegroundColor Green
Get-AzureRmVirtualNetwork | out-gridview
write-host "AVAILABLE SUBNETS:" -ForegroundColor Green
#write-host (Get-AzureRmVirtualNetworkSubnetConfig | fl * | out-gridview)
}
# ----------------------------------------------------------------------------------------------------------------
function manage-credential()
{
if (!($adminPassword) -or !($adminUsername))
{
write-host "either admin and / or adminpassword were empty, returning."
return
}
$SecurePassword = $adminPassword | ConvertTo-SecureString -AsPlainText -Force
$global:credential = new-object System.Management.Automation.PSCredential -ArgumentList $adminUsername, $SecurePassword
}
# ----------------------------------------------------------------------------------------------------------------
main