@@ -15,12 +15,13 @@ function New-AzVm {
15
15
PROCESS {
16
16
$rgi = [ResourceGroup ]::new($ResourceGroupName );
17
17
$vni = [VirtualNetwork ]::new($VirtualNetworkName );
18
+ $piai = [PublicIpAddress ]::new($PublicIpAddressName );
19
+ $sgi = [SecurityGroup ]::new($SecurityGroupName );
18
20
$nii = [NetworkInterface ]::new(
19
21
$NetworkInterfaceName ,
20
22
$vni ,
21
- [PublicIpAddress ]::new($PublicIpAddressName ),
22
- [SecurityGroup ]::new($SecurityGroupName )
23
- );
23
+ $piai ,
24
+ $sgi );
24
25
$vmi = [VirtualMachine ]::new($null , $nii , $rgi );
25
26
26
27
$locationi = [Location ]::new();
@@ -33,12 +34,19 @@ function New-AzVm {
33
34
$locationi.Value = $Location ;
34
35
}
35
36
36
- # Resource Group
37
37
$resourceGroup = $rgi.GetOrCreate ($Name + " ResourceGroup" , $locationi.Value , $null );
38
38
$virtualNetwork = $vni.GetOrCreate (
39
39
$Name + " VirtualNetwork" ,
40
40
$locationi.Value ,
41
41
$resourceGroup.ResourceGroupName );
42
+ $publicIpAddress = $piai.GetOrCreate (
43
+ $Name + " PublicIpAddress" ,
44
+ $locationi.Value ,
45
+ $resourceGroup.ResourceGroupName );
46
+ $securityGroup = $sgi.GetOrCreate (
47
+ $Name + " SecurityGroup" ,
48
+ $locationi.Value ,
49
+ $resourceGroup.ResourceGroupName )
42
50
43
51
if (-not $Credential ) {
44
52
$Credential = Get-Credential
@@ -65,59 +73,6 @@ function New-AzVm {
65
73
throw " Unknown image: " + $ImageName
66
74
}
67
75
68
- # Resource Group
69
- # $resourceGroup = Set-ResourceGroup -Name $ResourceGroupName -Location $Location
70
-
71
- # Virtual Network
72
- <#
73
- $virtualNetworkAddressPrefix = "192.168.0.0/16"
74
- $subnet = @{ Name = $Name + "Subnet"; AddressPrefix = "192.168.1.0/24" }
75
- $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
76
- -Name $subnet.Name `
77
- -AddressPrefix $subnet.AddressPrefix
78
- $virtualNetwork = New-AzureRmVirtualNetwork `
79
- -ResourceGroupName $ResourceGroupName `
80
- -Location $Location `
81
- -Name $VirtualNetworkName `
82
- -AddressPrefix $virtualNetworkAddressPrefix `
83
- -Subnet $subnetConfig
84
- #>
85
-
86
- # Piblic IP
87
- $publicIpAddress = New-AzureRmPublicIpAddress `
88
- - ResourceGroupName $ResourceGroupName `
89
- - Location $Location `
90
- - AllocationMethod Static `
91
- - Name $PublicIpAddressName
92
-
93
- # Security Group (it may have several rules(ports))
94
- $securityRule = @ {
95
- Name = $Name + " SecurityRule" ;
96
- Protocol = " Tcp" ;
97
- Priority = 1000 ;
98
- Access = " Allow" ;
99
- Direction = " Inbound" ;
100
- SourcePortRange = " *" ;
101
- SourceAddressPrefix = " *" ;
102
- DestinationPortRange = 3389 ;
103
- DestinationAddressPrefix = " *" ;
104
- }
105
- $securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
106
- - Name $securityRule.Name `
107
- - Protocol $securityRule.Protocol `
108
- - Priority $securityRule.Priority `
109
- - Access $securityRule.Access `
110
- - Direction $securityRule.Direction `
111
- - SourcePortRange $securityRule.SourcePortRange `
112
- - SourceAddressPrefix $securityRule.SourceAddressPrefix `
113
- - DestinationPortRange $securityRule.DestinationPortRange `
114
- - DestinationAddressPrefix $securityRule.DestinationAddressPrefix
115
- $securityGroup = New-AzureRmNetworkSecurityGroup `
116
- - ResourceGroupName $ResourceGroupName `
117
- - Location $Location `
118
- - Name $SecurityGroupName `
119
- - SecurityRules $securityRuleConfig
120
-
121
76
# Network Interface
122
77
$networkInterface = New-AzureRmNetworkInterface `
123
78
- ResourceGroupName $ResourceGroupName `
@@ -289,6 +244,14 @@ class PublicIpAddress: Resource1 {
289
244
[object ] GetInfo() {
290
245
return Get-AzureRMPublicIpAddress - Name $this.Name ;
291
246
}
247
+
248
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
249
+ return New-AzureRmPublicIpAddress `
250
+ - ResourceGroupName $resourceGroupName `
251
+ - Location $location `
252
+ - AllocationMethod Static `
253
+ - Name $name
254
+ }
292
255
}
293
256
294
257
class SecurityGroup : Resource1 {
@@ -298,6 +261,37 @@ class SecurityGroup: Resource1 {
298
261
[object ] GetInfo() {
299
262
return Get-AzureRMSecurityGroup - Name $this.Name ;
300
263
}
264
+
265
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
266
+ $securityRule = @ {
267
+ Name = $name ;
268
+ Protocol = " Tcp" ;
269
+ Priority = 1000 ;
270
+ Access = " Allow" ;
271
+ Direction = " Inbound" ;
272
+ SourcePortRange = " *" ;
273
+ SourceAddressPrefix = " *" ;
274
+ DestinationPortRange = 3389 ;
275
+ DestinationAddressPrefix = " *" ;
276
+ }
277
+
278
+ $securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
279
+ - Name $name `
280
+ - Protocol " Tcp" `
281
+ - Priority 1000 `
282
+ - Access " Allow" `
283
+ - Direction " Inbound" `
284
+ - SourcePortRange " *" `
285
+ - SourceAddressPrefix " *" `
286
+ - DestinationPortRange 3389 `
287
+ - DestinationAddressPrefix " *"
288
+
289
+ return New-AzureRmNetworkSecurityGroup `
290
+ - ResourceGroupName $resourceGroupName `
291
+ - Location $location `
292
+ - Name $name `
293
+ - SecurityRules $securityRuleConfig
294
+ }
301
295
}
302
296
303
297
class NetworkInterface : AzureObject {
@@ -316,15 +310,16 @@ class NetworkInterface: AzureObject {
316
310
317
311
class VirtualMachine : AzureObject {
318
312
VirtualMachine(
319
- [string ] $name , [NetworkInterface ] $networkInterface , [ResourceGroup ] $resourceGroup
313
+ [string ] $name ,
314
+ [NetworkInterface ] $networkInterface ,
315
+ [ResourceGroup ] $resourceGroup
320
316
): base($name , @ ($networkInterface , $resourceGroup )) {
321
317
}
322
318
323
319
[object ] GetInfo() {
324
320
return Get-AzureRMVirtualMachine - Name $this.Name ;
325
321
}
326
322
}
327
-
328
323
function New-PsObject {
329
324
param ([hashtable ] $property )
330
325
0 commit comments