New-WindowsDataScienceVM.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<#PSScriptInfo
 
.VERSION 1.2
 
.GUID 1e639a45-89fa-4cd3-8956-15495357edad
 
.AUTHOR Rob Sewell
 
.COMPANYNAME Sewells Consulting
 
.COPYRIGHT Rob Sewell - please credit Rob Sewell - https://sqldbawithbeard.com if used
 
.DESCRIPTION Creates a Windows Data Science Virtual Machine in Azure with a simple switch to remove complexity or the ability to configure all resources
 
.TAGS Azure,Virtual Machine, Data Science
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES sqlserver
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES Initial
#>

function New-WindowsDataScienceVM
{
    <#
.Synopsis
   This function will create a Windows Data Science Virtual Machine in Azure with all the relevant
   infrastructure with a simple switch to remove all the hassle
.DESCRIPTION
   This function will create a Windows Data Science Virtual Machine in Azure using an ARM template
   deployment, Creating a Resource Group, storage account, virtual network, public ip address and
   a windows data science virtual machine in Azure. The simple switch will require only the admin
   credentials for the virtual machine to be added and will create the following with RANDOM being
   a randomly generated 5 character string. Note the virtual machine size is set to Standard_DS1_v2
   and the location to ukwest
 
    resourcegroupname = DS-RANDOM
    location = ukwest
    virtualMachineName = DSVMRANDOM
    virtualMachineSize = Standard_DS1_v2
    storageAccountName = dsstorageRANDOM in lowercase
    virtualNetworkName = dsnetRANDOM in lowercase
    networkInterfaceName = dsinterRANDOM in lowercase
    networkSecurityGroupName = dssecgrpRANDOM in lowercase
    storageAccountType = Standard_LRS
    diagnosticsStorageAccountName = diagdsRANDOM in lowercase
    diagnosticsStorageAccountType = Standard_LRS
    publicIpAddressName = dspubipRANDOM
    publicIpAddressType = Dynamic
 
    If the simple switch is not used every parameter above is configurable
 
.PARAMETER Simple
   Enables Simple Mode - As little as possible is asked and everything is created with random names
   The simple switch will require only the admin credentials for the virtual machine to be added and
   will create the resources required with RANDOM being a randomly generated 5 character string. Note
   the virtual machine size is set to Standard_DS1_v2 and the location to ukwest
 
.PARAMETER resourcegroupname
    The name of the Resource Group - Not required for Simple mode
 
.PARAMETER location
    The location - Run (Get-AzureRmLocation).Location for values Not required for Simple mode
 
.PARAMETER virtualmachinename
    The name of the Virtual Machine - Not required for Simple mode
 
.PARAMETER virtualmachinesize
    The size of the Virtual Machine - Run (Get-AzureRmVmSize -Location location).Name for values - Not
    required for Simple mode
 
.PARAMETER storageaccountname
    The name of the Storage Account - Unique across Azure - Check if Name already exists with
    Test-AzureName -Storage -Name NAME - Not required for Simple mode
 
.PARAMETER virtualNetworkName
    The name of the Virtual Network- Not required for Simple mode
 
.PARAMETER networkInterfaceName
    The name of the Network Interface - Not required for Simple mode
 
.PARAMETER networkSecurityGroupName
    The name of the Network Security Group - Not required for Simple mode
 
.PARAMETER storageAccountType
    The Storage Account type valid values are Standard_LRS,Standard_ZRS,Standard_GRS,Standard_RAGRS,
    Premium_LRS - Not required for Simple mode
 
.PARAMETER diagnosticstorageaccountname
    The name of the diagnostic storage account - Unique across Azure - Check if Name already exists
    with Test-AzureName -Storage -Name NAME - Not required for Simple mode
 
.PARAMETER diagnosticsStorageAccountType
    The diagnostic Storage Account type valid values are Standard_LRS,Standard_ZRS,Standard_GRS,
    Standard_RAGRS,Premium_LRS - Not required for Simple mode
 
.PARAMETER addressPrefix
    The network address prefix - defaults to 10.0.0.0/24 - Not required for Simple mode
 
.PARAMETER subnetPrefix
    The subnet prefix - defaults to 10.0.0.0/24 - Not required for Simple mode
 
.PARAMETER publicIpAddressName
    The Public IP Address name - Not required for Simple mode
 
.PARAMETER TemplateJsonFilePath
    The path to the template JSON file - defaults to users document folder - Not required for Simple mode
 
.PARAMETER ParameterJsonFilePath
    The path to the parameter JSON file - defaults to users document folder - Not required for Simple mode
 
.PARAMETER credential
    The credential for the admin user for the Virtual Machine - not reuired for simple mode
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.EXAMPLE
    Login-AzureRmAccount
    New-WindowsDataScienceVM -Simple
 
    Logs into Azure and enables Simple Mode which will pop-up a request for an admin username and password and
    then create a Windows Data Science Virtual machine and resources as shown and named as below with RANDOM
    being a randomly generated 5 character string. Note the virtual machine size is set to Standard_DS1_v2 and
    the location to ukwest
 
    resourcegroupname = DS-RANDOM
    location = ukwest
    virtualMachineName = DSVMRANDOM
    virtualMachineSize = Standard_DS1_v2
    storageAccountName = dsstorageRANDOM in lowercase
    virtualNetworkName = dsnetRANDOM in lowercase
    networkInterfaceName = dsinterRANDOM in lowercase
    networkSecurityGroupName = dssecgrpRANDOM in lowercase
    storageAccountType = Standard_LRS
    diagnosticsStorageAccountName = diagdsRANDOM in lowercase
    diagnosticsStorageAccountType = Standard_LRS
    publicIpAddressName = dspubipRANDOM
    publicIpAddressType = Dynamic
.EXAMPLE
   Login-AzureRmAccount
   $cred = Get-Credential
    New-WindowsDataScienceVM -resourcegroupname $resourcegroupname -location $location -virtualmachinename $virtualmachinename `
-virtualmachinesize $virtualmachinesize -storageaccountname $storageaccountname -virtualNetworkName $virtualNetworkName `
-networkInterfaceName $networkInterfaceName -networkSecurityGroupName $networkSecurityGroupName `
-storageAccountType $storageAccountType -diagnosticstorageaccountname $diagnosticsStorageAccountName `
-diagnosticsStorageAccountType $diagnosticsStorageAccountType -publicIpAddressName $publicIpAddressName `
-TemplateJsonFilePath $TemplateJsonFilePath -ParameterJsonFilePath $ParameterJsonFilePath -credential $cred
 
Creates a Windows Data Science Virtual Machine in Azure using the values for each variable
 
.NOTES
   AUTHOR - Rob Sewell 14/12/2016 sqldbawithabeard.com @sqldbawithbeard
 
   Read the blog post for instructions on pre-requisite actions required in the portal prior to running this script
#>

[CmdletBinding(SupportsShouldProcess=$true)]
param 
(
[Parameter(Mandatory=$false, Position=0, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Enables Simple Mode - As little as possible is asked and everything is created with random names")]  
[switch]$Simple,
[Parameter(mandatory=$false, Position=0, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Resource Group Name")]        
[ValidatePattern("^[a-zA-Z0-9_-]{1,64}$")] ## 1-64 characters Alphanumeric, underscore and dash
[string]$resourcegroupname,
[Parameter(mandatory=$false, Position=0, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Location for VM - Run (Get-AzureRmLocation).Location for values")]
[ValidateScript({(Get-AzureRmLocation).Location -contains $_})]
[string]$location,
[pscredential][System.Management.Automation.Credential()]$credential,
[Parameter(mandatory=$false, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Machine Name")]
[ValidatePattern("^[a-zA-Z0-9_-]{0,15}$")] ## 1-15 characters Alphanumeric, underscore and dash
[ValidateScript({if($resourcegroupname){(Get-AzureRmVM -ResourceGroupName $resourcegroupname).Name -notcontains $_} else {$true}})]
[string]$virtualmachinename,
[Parameter(mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Machine Size - Run (Get-AzureRmVmSize -Location DataCentre).Name for values")]
[ValidatePattern("^[a-zA-Z0-9_-]{0,15}$")] ## 1-15 characters Alphanumeric, underscore and dash
[ValidateScript({if($location){(Get-AzureRmVMSize -Location $location).Name -contains $_}else {$true}})]
[string]$virtualmachinesize,
[Parameter(mandatory=$false, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Storage Account Name must be unique across Azure")]
[ValidatePattern("^[a-z0-9]{3,24}$")] ## 3-24 Alphanumeric, underscore and dash
[ValidateScript({(Test-AzureName -Storage -Name $_) -ne $true})]
[string]$storageaccountname,
[Parameter(mandatory=$false, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Network Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{2,64}$")] ## 2-64 characters Alphanumeric, underscore, space and dash
[ValidateScript({if($resourcegroupname){(Get-AzureRmVirtualNetwork -ResourceGroupName $resourcegroupname).Name -notcontains $_}else {$true}})]
[string]$virtualNetworkName,
[Parameter(mandatory=$false, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Network Interface Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{1,80}$")] ## 1-80 characters Alphanumeric, underscore, space and dash
[ValidateScript({if($resourcegroupname){(Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroupname).Name -notcontains $_}else {$true}})]
[string]$networkInterfaceName,
[Parameter(mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Network Security Group Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{1,80}$")] ## 1-80 characters Alphanumeric, underscore, space and dash
[ValidateScript({if($resourcegroupname){(Get-AzureRmNetworkSecurityGroup -ResourceGroupName $resourcegroupname).Name -notcontains $_}else {$true}})]
[string]$networkSecurityGroupName,
[Parameter(mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Storage Account Type - 'Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS'")]
[ValidateSet('Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS')]
[string]$storageAccountType,
[Parameter(mandatory=$false, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Diagnostic Storage Account Name must be unique across Azure")]
[ValidatePattern("^[a-z0-9]{3,24}$")] ## 3-24 Alphanumeric, underscore and dash
[ValidateScript({(Test-AzureName -Storage -Name $_) -ne $true})]
[string]$diagnosticstorageaccountname,
[Parameter(mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Diagnostic Storage Account Type - 'Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS'")]
[ValidateSet('Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS')]
[string]$diagnosticsStorageAccountType,
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$false, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Address Prefix - normally leave to default")]
[string]$addressPrefix = "10.0.0.0/24", ## I'm not working out the regex for this, sorry :-)
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Subnet Name - normally leave to default")]
                   [ValidatePattern("^[a-zA-Z0-9 _-]{2,80}$")] ## 2-80 characters Alphanumeric, underscore, space and dash

[string]$subnetName ='default',
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Subnet Prefix - normally leave to default")]
[string]$subnetPrefix = "10.0.0.0/24", ## nor this
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Public IP Address Name - unique accross Resource Group")]
                   [ValidatePattern("^[a-zA-Z0-9 _-]{2,80}$")] ## 2-80 characters Alphanumeric, underscore, space and dash
[ValidateScript({if($resourcegroupname){(Get-AzureRmPublicIpAddress -ResourceGroupName $resourcegroupname).ResourceGroupName -notcontains $_}else {$true}})]
[string]$publicIpAddressName,
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Template Json file path")]
[string]$TemplateJsonFilePath,
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Parameter Json file path")]
[string]$ParameterJsonFilePath
)

if ($simple)
{
$rand = -join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_})
$resourcegroupname = 'DSRG-' + $rand
$docs = [Environment]::GetFolderPath("mydocuments")
$ParameterJsonFilePath = "$docs\newparameter.json"
$TemplateJsonFilePath = "$docs\newtemplate.json"
$cred = Get-Credential -Message 'Enter Local Admin Credentials for the VM - Password must have 3 of the following 1 Upper case, 1 lower case, I special character and 1 number'
$location = 'ukwest'
$virtualMachineName = 'DSVM-' + $rand
$virtualMachineSize = 'Standard_DS1_v2'
$adminUsername = $adminusername
$storageAccountName = 'dsstorage' + $rand.ToLower()
$virtualNetworkName = 'dsnet' + $rand.ToLower()
$networkInterfaceName = 'dsinter' + $rand.ToLower()
$networkSecurityGroupName = 'dssecgrp' + $rand.ToLower()
$storageAccountType = 'Standard_LRS' 
$diagnosticsStorageAccountName = 'diagds' + $rand.ToLower()
$diagnosticsStorageAccountType = 'Standard_LRS'
$publicIpAddressName = 'dspubip' + $rand

}
Function Set-ParameterJson
{
    [CmdletBinding(SupportsShouldProcess=$true)]
param 
(
[Parameter(Mandatory=$true, Position=0, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Resource Group Name")]        
[ValidatePattern("^[a-zA-Z0-9_-]{1,64}$")] ## 1-64 characters Alphanumeric, underscore and dash
[string]$resourcegroupname,
[Parameter(Mandatory=$true, Position=0, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Location for VM - Run (Get-AzureRmLocation).Location for values")]
[ValidateScript({(Get-AzureRmLocation).Location -contains $_})]
[string]$location,
[pscredential][System.Management.Automation.Credential()]$credential,
[Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Machine Name")]
[ValidatePattern("^[a-zA-Z0-9_-]{0,15}$")] ## 1-15 characters Alphanumeric, underscore and dash
[ValidateScript({(Get-AzureRmVM -ResourceGroupName $resourcegroupname).Name -notcontains $_})]
[string]$virtualmachinename,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Machine Size - Run (Get-AzureRmVmSize -Location DataCentre).Name for values")]
[ValidatePattern("^[a-zA-Z0-9_-]{0,15}$")] ## 1-15 characters Alphanumeric, underscore and dash
[ValidateScript({(Get-AzureRmVMSize -Location $location).Name -contains $_})]
[string]$virtualmachinesize,
[Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Storage Account Name must be unique across Azure")]
[ValidatePattern("^[a-z0-9]{3,24}$")] ## 3-24 Alphanumeric, underscore and dash
[ValidateScript({(Test-AzureName -Storage -Name $storageaccountname) -ne $true})]
[string]$storageaccountname,
[Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Network Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{2,64}$")] ## 2-64 characters Alphanumeric, underscore, space and dash
[ValidateScript({(Get-AzureRmVirtualNetwork -ResourceGroupName $resourcegroupname).Name -notcontains $_})]
[string]$virtualNetworkName,
[Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Virtual Network Interface Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{1,80}$")] ## 1-80 characters Alphanumeric, underscore, space and dash
[ValidateScript({(Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroupname).Name -notcontains $_})]
[string]$networkInterfaceName,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Network Security Group Name - must be unique in Resource Group")]
[ValidatePattern("^[a-zA-Z0-9 _-]{1,80}$")] ## 1-80 characters Alphanumeric, underscore, space and dash
[ValidateScript({(Get-AzureRmNetworkSecurityGroup -ResourceGroupName $resourcegroupname).Name -notcontains $_})]
[string]$networkSecurityGroupName,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Storage Account Type - 'Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS'")]
[ValidateSet('Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS')]
[string]$storageAccountType,
[Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Diagnostic Storage Account Name must be unique across Azure")]
[ValidatePattern("^[a-z0-9]{3,24}$")] ## 3-24 Alphanumeric, underscore and dash
[ValidateScript({(Test-AzureName -Storage -Name $_) -ne $true})]
[string]$diagnosticstorageaccountname,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Diagnostic Storage Account Type - 'Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS'")]
[ValidateSet('Standard_LRS','Standard_ZRS','Standard_GRS','Standard_RAGRS','Premium_LRS')]
[string]$diagnosticsStorageAccountType,
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$false, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Address Prefix - normally leave to default")]
[string]$addressPrefix = "10.0.0.0/24", ## I'm not working out the regex for this, sorry :-)
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Subnet Name - normally leave to default")]
                   [ValidatePattern("^[a-zA-Z0-9 _-]{2,80}$")] ## 2-80 characters Alphanumeric, underscore, space and dash

[string]$subnetName ='default',
[Parameter(Mandatory=$false,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Subnet Prefix - normally leave to default")]
[string]$subnetPrefix = "10.0.0.0/24", ## nor this
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Public IP Address Name - unique accross Resource Group")]
                   [ValidatePattern("^[a-zA-Z0-9 _-]{2,80}$")] ## 2-80 characters Alphanumeric, underscore, space and dash
                   [ValidateScript({(Get-AzureRmPublicIpAddress -ResourceGroupName $resourcegroupname).ResourceGroupName -notcontains $_})]
[string]$publicIpAddressName,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Template Json file path")]
[string]$TemplateJsonFilePath,
[Parameter(Mandatory=$true,  
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Parameter Json file path")]
[string]$ParameterJsonFilePath
)

    $ParameterJson = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/SQLDBAWithABeard/DataScienceVM/master/parameters.json').Content | ConvertFrom-Json 
    $adminusername =$credential.UserName
    $adminuserpassword = $credential.GetNetworkCredential().SecurePassword
    
    $ParameterJson.parameters.location.value = $location
    $ParameterJson.parameters.virtualMachineName.value = $virtualmachinename
    $ParameterJson.parameters.virtualMachineSize.value = $virtualmachinesize
    $ParameterJson.parameters.adminUsername.value = $adminusername
    $ParameterJson.parameters.adminPassword.value = $adminuserpassword
    $ParameterJson.parameters.storageAccountName.value = $storageaccountname
    $ParameterJson.parameters.virtualNetworkName.value = $virtualNetworkName
    $ParameterJson.parameters.networkInterfaceName.value = $networkInterfaceName
    $ParameterJson.parameters.networkSecurityGroupName.value = $networkSecurityGroupName
    $ParameterJson.parameters.storageAccountType.value = $storageAccountType 
    $ParameterJson.parameters.diagnosticsStorageAccountName.value = $diagnosticsStorageAccountName
    $ParameterJson.parameters.diagnosticsStorageAccountType.value = $diagnosticsStorageAccountType
    $ParameterJson.parameters.diagnosticsStorageAccountId.value = "Microsoft.Storage/storageAccounts/$diagnosticsStorageAccountName"
    $ParameterJson.parameters.addressPrefix.value = $addressPrefix 
    $ParameterJson.parameters.subnetName.value = $subnetName
    $ParameterJson.parameters.subnetPrefix.value = $subnetPrefix
    $ParameterJson.parameters.publicIpAddressName.value = $publicIpAddressName
    $ParameterJson.parameters.publicIpAddressType.value = 'Dynamic'
  
    
    If ($Pscmdlet.ShouldProcess($ParameterJsonFilePath, "Creating Parameter File"))
    {
        $ParameterJson | ConvertTo-Json | Set-Content -Path $ParameterJsonFilePath
    }
    If ($Pscmdlet.ShouldProcess("https://raw.githubusercontent.com/SQLDBAWithABeard/DataScienceVM/master/template.json", "Saving Template json file to $TemplateJsonFilePath"))
    {
    Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/SQLDBAWithABeard/DataScienceVM/master/template.json' -OutFile $TemplateJsonFilePath
    }

    (Get-Content $TemplateJsonFilePath).Replace("parameters('resourcegroupname')","'$resourcegroupname'")|Out-File $TemplateJsonFilePath
}

# Register RPs
$resourceProviders = @("microsoft.compute","microsoft.storage","microsoft.network");
if($resourceProviders.length) {
    foreach($resourceProvider in $resourceProviders) {
        $null = Register-AzureRmResourceProvider -ProviderNamespace $resourceProvider;
    }
}

#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
   try
   {
        If ($Pscmdlet.ShouldProcess($resourceGroupName, "Creating Resource group $resourcegroupname in $location"))
        {
            New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location -erroraction stop
        }
    }
    catch
    {
        Write-warning -message "Something Went wrong- Run `$Error[0] | fl -force to get more information"
    }
}

Set-ParameterJson -resourcegroupname $resourcegroupname -location $location -virtualmachinename $virtualmachinename `
-virtualmachinesize $virtualmachinesize -storageaccountname $storageaccountname -virtualNetworkName $virtualNetworkName `
-networkInterfaceName $networkInterfaceName -networkSecurityGroupName $networkSecurityGroupName `
-storageAccountType $storageAccountType -diagnosticstorageaccountname $diagnosticsStorageAccountName `
-diagnosticsStorageAccountType $diagnosticsStorageAccountType -publicIpAddressName $publicIpAddressName `
-TemplateJsonFilePath $TemplateJsonFilePath -ParameterJsonFilePath $ParameterJsonFilePath -credential $cred

# Start the deployment

if((Test-Path $ParameterJsonFilePath) -and (Test-Path $TemplateJsonFilePath )) 
{
    try
    {
        If ($Pscmdlet.ShouldProcess($resourceGroupName, "Deploying using $TemplateJsonFilePath and $ParameterJsonFilePath"))
        {
            New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $TemplateJsonFilePath -TemplateParameterFile $ParameterJsonFilePath -Verbose -erroraction stop;
        }
    }
    catch
    {
        Write-warning -message "Something Went wrong- Run `$Error[0] | fl -force to get more information"
    } 

    try
    {
        Set-AzureRmVMAccessExtension -ResourceGroupName $resourcegroupname -VMName $virtualmachinename  `
    -Name "myVMAccess" -Location $location -UserName $cred.GetNetworkCredential().Username `
    -Password $cred.GetNetworkCredential().Password -typeHandlerVersion "2.0"

    }
    catch
    {
        Write-Warning "Something went wrong setting the username and password Run `$Error[0] | fl -force to get more information - You may need to visit the portal and find the Virtual machine and reset the password there"
    }
}
 else 
 {
    Write-Warning -Message "Something went wrong the files were not available"
 }
    $RDPFile = "$docs\$virtualmachinename" + ".rdp"
    Write-Output "All Done! To download the RDP file and connect to your machine"
    Write-output "Run Get-AzureRmRemoteDesktopFile -ResourceGroupName $resourcegroupname -Name $virtualmachinename -LocalPath $RDPFile -Launch "
    Write-Output "The RDP file will be saved to $RDPFile"
    Write-Output "Remember the User name you need to enter is $virtualmachinename\$($cred.GetNetworkCredential().Username)"
 }