@@ -22,7 +22,7 @@ function New-AzVm {
22
22
$vni ,
23
23
$piai ,
24
24
$sgi );
25
- $vmi = [VirtualMachine ]::new($null , $nii , $rgi );
25
+ $vmi = [VirtualMachine ]::new($null , $nii , $rgi , $Credential , $ImageName , $images );
26
26
27
27
$locationi = [Location ]::new();
28
28
if (-not $Location ) {
@@ -35,57 +35,11 @@ function New-AzVm {
35
35
}
36
36
37
37
$resourceGroup = $rgi.GetOrCreate ($Name + " ResourceGroup" , $locationi.Value , $null );
38
- $networkInterface = $nii.GetOrCreate ($Name , $locationi.Value , $resourceGroup.ResourceGroupName );
39
-
40
- if (-not $Credential ) {
41
- $Credential = Get-Credential
42
- }
43
- if (-not $ResourceGroupName ) {
44
- $ResourceGroupName = $Name + " ResourceGroup" ;
45
- }
46
-
47
- # Find VM Image
48
- $vmImage = $images | Where-Object { $_.Name -eq $ImageName } | Select-Object - First 1
49
- if (-not $vmImage ) {
50
- throw " Unknown image: " + $ImageName
51
- }
52
-
53
- # VM
54
- $vmSize = " Standard_DS2"
55
- $vmConfig = New-AzureRmVMConfig - VMName $Name - VMSize $vmSize
56
- $vmComputerName = $Name + " Computer"
57
- switch ($vmImage.Type ) {
58
- " Windows" {
59
- $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
60
- - Windows `
61
- - ComputerName $vmComputerName `
62
- - Credential $Credential
63
- }
64
- " Linux" {
65
- $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
66
- - Linux `
67
- - ComputerName $vmComputerName `
68
- - Credential $Credential
69
- }
70
- }
71
-
72
- $vmImageImage = $vmImage.Image
73
- $vmConfig = $vmConfig `
74
- | Set-AzureRmVMSourceImage `
75
- - PublisherName $vmImageImage.publisher `
76
- - Offer $vmImageImage.offer `
77
- - Skus $vmImageImage.sku `
78
- - Version $vmImageImage.version `
79
- | Add-AzureRmVMNetworkInterface - Id $networkInterface.Id
80
-
81
- $response = New-AzureRmVm `
82
- - ResourceGroupName $ResourceGroupName `
83
- - Location $Location `
84
- - VM $vmConfig
38
+ $vmResponse = $vmi.Create ($Name , $locationi.Value , $resourceGroup.ResourceGroupName );
85
39
86
40
New-PsObject @ {
87
41
ResourceId = $resourceGroup.ResourceId ;
88
- Response = $response
42
+ Response = $vmResponse
89
43
}
90
44
}
91
45
}
@@ -285,16 +239,75 @@ class NetworkInterface: AzureObject {
285
239
}
286
240
287
241
class VirtualMachine : AzureObject {
242
+ [NetworkInterface ] $NetworkInterface ;
243
+ [pscredential ] $Credential ;
244
+ [string ] $ImageName ;
245
+ [object ] $Images ;
246
+
288
247
VirtualMachine(
289
248
[string ] $name ,
290
249
[NetworkInterface ] $networkInterface ,
291
- [ResourceGroup ] $resourceGroup
292
- ): base($name , @ ($networkInterface , $resourceGroup )) {
250
+ [ResourceGroup ] $resourceGroup ,
251
+ [PSCredential ] $credential ,
252
+ [string ] $imageName ,
253
+ [object ] $images ):
254
+ base($name , @ ($networkInterface , $resourceGroup )) {
255
+
256
+ $this.Credential = $credential ;
257
+ $this.ImageName = $imageName ;
258
+ $this.NetworkInterface = $networkInterface ;
259
+ $this.Images = $images ;
293
260
}
294
261
295
262
[object ] GetInfo() {
296
263
return Get-AzureRMVirtualMachine - Name $this.Name ;
297
264
}
265
+
266
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
267
+ $networkInterfaceInstance = $this.NetworkInterface.GetOrCreate ( `
268
+ $name , $location , $resourceGroupName );
269
+
270
+ if (-not $this.Credential ) {
271
+ $this.Credential = Get-Credential
272
+ }
273
+
274
+ $vmImage = $this.Images | Where-Object { $_.Name -eq $this.ImageName } | Select-Object - First 1
275
+ if (-not $vmImage ) {
276
+ throw " Unknown image: " + $this.ImageName
277
+ }
278
+
279
+ $vmSize = " Standard_DS2"
280
+ $vmConfig = New-AzureRmVMConfig - VMName $Name - VMSize $vmSize
281
+ $vmComputerName = $Name + " Computer"
282
+ switch ($vmImage.Type ) {
283
+ " Windows" {
284
+ $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
285
+ - Windows `
286
+ - ComputerName $vmComputerName `
287
+ - Credential $this.Credential
288
+ }
289
+ " Linux" {
290
+ $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
291
+ - Linux `
292
+ - ComputerName $vmComputerName `
293
+ - Credential $this.Credential
294
+ }
295
+ }
296
+
297
+ $vmImageImage = $vmImage.Image
298
+ $vmConfig = $vmConfig `
299
+ | Set-AzureRmVMSourceImage `
300
+ - PublisherName $vmImageImage.publisher `
301
+ - Offer $vmImageImage.offer `
302
+ - Skus $vmImageImage.sku `
303
+ - Version $vmImageImage.version `
304
+ | Add-AzureRmVMNetworkInterface - Id $networkInterfaceInstance.Id
305
+
306
+ return New-AzureRmVm `
307
+ - ResourceGroupName $resourceGroupName `
308
+ - Location $location `
309
+ - VM $vmConfig
310
+ }
298
311
}
299
312
function New-PsObject {
300
313
param ([hashtable ] $property )
0 commit comments