@@ -4,86 +4,262 @@ function New-AzVm {
4
4
[Parameter ()][PSCredential ] $Credential ,
5
5
[Parameter ()][string ] $Name = " VM" ,
6
6
[Parameter ()][string ] $ImageName = " Win2012R2Datacenter" ,
7
- [Parameter ()][string ] $ResourceGroupName = $Name + " ResourceGroup " ,
7
+ [Parameter ()][string ] $ResourceGroupName ,
8
8
[Parameter ()][string ] $Location = " eastus" ,
9
- [Parameter ()][string ] $VirtualNetworkName = $Name + " VirtualNetwork " ,
10
- [Parameter ()][string ] $PublicIpAddressName = $Name + " PublicIpAddress " ,
11
- [Parameter ()][string ] $SecurityGroupName = $Name + " SecurityGroup " ,
12
- [Parameter ()][string ] $NetworkInterfaceName = $Name + " NetworkInterface "
9
+ [Parameter ()][string ] $VirtualNetworkName ,
10
+ [Parameter ()][string ] $PublicIpAddressName ,
11
+ [Parameter ()][string ] $SecurityGroupName ,
12
+ [Parameter ()][string ] $NetworkInterfaceName
13
13
)
14
14
15
15
PROCESS {
16
- if (-not $Credential ) {
17
- $Credential = Get-Credential
16
+ $rgi = [ResourceGroup ]::new($ResourceGroupName );
17
+ $vni = [VirtualNetwork ]::new($VirtualNetworkName );
18
+ $piai = [PublicIpAddress ]::new($PublicIpAddressName );
19
+ $sgi = [SecurityGroup ]::new($SecurityGroupName );
20
+ $nii = [NetworkInterface ]::new(
21
+ $NetworkInterfaceName ,
22
+ $vni ,
23
+ $piai ,
24
+ $sgi );
25
+ $vmi = [VirtualMachine ]::new($null , $nii , $rgi , $Credential , $ImageName , $images );
26
+
27
+ $locationi = [Location ]::new();
28
+ if (-not $Location ) {
29
+ $vmi.UpdateLocation ($locationi );
30
+ if (-not $locationi.Value ) {
31
+ $locationi.Value = " eastus" ;
32
+ }
33
+ } else {
34
+ $locationi.Value = $Location ;
18
35
}
19
36
20
- # Find VM Image
21
- $vmImage = $images | Where-Object { $_.Name -eq $ImageName } | Select-Object - First 1
22
- if (-not $vmImage ) {
23
- throw " Unknown image: " + $ImageName
37
+ $resourceGroup = $rgi.GetOrCreate ($Name + " ResourceGroup" , $locationi.Value , $null );
38
+ $vmResponse = $vmi.Create ($Name , $locationi.Value , $resourceGroup.ResourceGroupName );
39
+
40
+ New-PsObject @ {
41
+ ResourceId = $resourceGroup.ResourceId ;
42
+ Response = $vmResponse
43
+ }
44
+ }
45
+ }
46
+
47
+ class Location {
48
+ [int ] $Priority ;
49
+ [string ] $Value ;
50
+
51
+ Location() {
52
+ $this.Priority = 0 ;
53
+ $this.Value = $null ;
54
+ }
55
+ }
56
+
57
+ class AzureObject {
58
+ [string ] $Name ;
59
+ [AzureObject []] $Children ;
60
+ [int ] $Priority ;
61
+
62
+ AzureObject([string ] $name , [AzureObject []] $children ) {
63
+ $this.Name = $name ;
64
+ $this.Children = $children ;
65
+ $this.Priority = 0 ;
66
+ foreach ($child in $this.Children ) {
67
+ if ($this.Priority -lt $child.Priority ) {
68
+ $this.Priority = $child.Priority ;
69
+ }
70
+ }
71
+ $this.Priority ++ ;
72
+ }
73
+
74
+ # This function should be called only when $this.Name is not $null.
75
+ [object ] GetInfo() {
76
+ return $null ;
77
+ }
78
+
79
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
80
+ return $null ;
81
+ }
82
+
83
+ [void ] UpdateLocation([Location ] $location ) {
84
+ if ($this.Priority -gt $location.Priority ) {
85
+ if ($this.Name ) {
86
+ $location.Value = $this.GetInfo ().Location;
87
+ $location.Priority = $this.Priority ;
88
+ } else {
89
+ foreach ($child in $this.Children ) {
90
+ $child.UpdateLocation ($location );
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ [object ] GetOrCreate([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
97
+ if ($this.Name ) {
98
+ return $this.GetInfo ();
99
+ } else {
100
+ $result = $this.Create ($name , $location , $resourceGroupName );
101
+ $this.Name = $name ;
102
+ return $result ;
24
103
}
104
+ }
105
+ }
106
+
107
+ class ResourceGroup : AzureObject {
108
+ ResourceGroup([string ] $name ): base($name , @ ()) {
109
+ }
110
+
111
+ [object ] GetInfo() {
112
+ return Get-AzureRmResourceGroup - Name $this.Name ;
113
+ }
25
114
26
- # Resource Group
27
- $resourceGroup = Set-ResourceGroup - Name $ResourceGroupName - Location $Location
115
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
116
+ return New-AzureRmResourceGroup - Name $name - Location $location ;
117
+ }
118
+ }
119
+
120
+ class Resource1 : AzureObject {
121
+ Resource1([string ] $name ): base($name , @ ([ResourceGroup ]::new($null ))) {
122
+ }
123
+ }
28
124
29
- # Virtual Network
30
- $virtualNetworkAddressPrefix = " 192.168.0.0/16"
31
- $subnet = @ { Name = $Name + " Subnet" ; AddressPrefix = " 192.168.1.0/24" }
125
+ class VirtualNetwork : Resource1 {
126
+ VirtualNetwork([string ] $name ): base($name ) {
127
+ }
128
+
129
+ [object ] GetInfo() {
130
+ return Get-AzureRmVirtualNetwork - Name $this.Name ;
131
+ }
132
+
133
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
32
134
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
33
- - Name $subnet .Name `
34
- - AddressPrefix $subnet .AddressPrefix
35
- $virtualNetwork = New-AzureRmVirtualNetwork `
36
- - ResourceGroupName $ResourceGroupName `
37
- - Location $Location `
38
- - Name $VirtualNetworkName `
39
- - AddressPrefix $virtualNetworkAddressPrefix `
135
+ - Name " Subnet " `
136
+ - AddressPrefix " 192.168.1.0/24 "
137
+ return New-AzureRmVirtualNetwork `
138
+ - ResourceGroupName $resourceGroupName `
139
+ - Location $location `
140
+ - Name $name `
141
+ - AddressPrefix " 192.168.0.0/16 " `
40
142
- Subnet $subnetConfig
143
+ }
144
+ }
41
145
42
- # Piblic IP
43
- $publicIpAddress = New-AzureRmPublicIpAddress `
44
- - ResourceGroupName $ResourceGroupName `
45
- - Location $Location `
146
+ class PublicIpAddress : Resource1 {
147
+ PublicIpAddress([string ] $name ): base($name ) {
148
+ }
149
+
150
+ [object ] GetInfo() {
151
+ return Get-AzureRMPublicIpAddress - Name $this.Name ;
152
+ }
153
+
154
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
155
+ return New-AzureRmPublicIpAddress `
156
+ - ResourceGroupName $resourceGroupName `
157
+ - Location $location `
46
158
- AllocationMethod Static `
47
- - Name $PublicIpAddressName
48
-
49
- # Security Group (it may have several rules(ports))
50
- $securityRule = @ {
51
- Name = $Name + " SecurityRule" ;
52
- Protocol = " Tcp" ;
53
- Priority = 1000 ;
54
- Access = " Allow" ;
55
- Direction = " Inbound" ;
56
- SourcePortRange = " *" ;
57
- SourceAddressPrefix = " *" ;
58
- DestinationPortRange = 3389 ;
59
- DestinationAddressPrefix = " *" ;
60
- }
159
+ - Name $name
160
+ }
161
+ }
162
+
163
+ class SecurityGroup : Resource1 {
164
+ SecurityGroup([string ] $name ): base($name ) {
165
+ }
166
+
167
+ [object ] GetInfo() {
168
+ return Get-AzureRMSecurityGroup - Name $this.Name ;
169
+ }
170
+
171
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
61
172
$securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
62
- - Name $securityRule.Name `
63
- - Protocol $securityRule.Protocol `
64
- - Priority $securityRule.Priority `
65
- - Access $securityRule.Access `
66
- - Direction $securityRule.Direction `
67
- - SourcePortRange $securityRule.SourcePortRange `
68
- - SourceAddressPrefix $securityRule.SourceAddressPrefix `
69
- - DestinationPortRange $securityRule.DestinationPortRange `
70
- - DestinationAddressPrefix $securityRule.DestinationAddressPrefix
71
- $securityGroup = New-AzureRmNetworkSecurityGroup `
72
- - ResourceGroupName $ResourceGroupName `
73
- - Location $Location `
74
- - Name $SecurityGroupName `
173
+ - Name $name `
174
+ - Protocol " Tcp" `
175
+ - Priority 1000 `
176
+ - Access " Allow" `
177
+ - Direction " Inbound" `
178
+ - SourcePortRange " *" `
179
+ - SourceAddressPrefix " *" `
180
+ - DestinationPortRange 3389 `
181
+ - DestinationAddressPrefix " *"
182
+
183
+ return New-AzureRmNetworkSecurityGroup `
184
+ - ResourceGroupName $resourceGroupName `
185
+ - Location $location `
186
+ - Name $name `
75
187
- SecurityRules $securityRuleConfig
188
+ }
189
+ }
190
+
191
+ class NetworkInterface : AzureObject {
192
+ [VirtualNetwork ] $VirtualNetwork ;
193
+ [PublicIpAddress ] $PublicIpAddress ;
194
+ [SecurityGroup ] $SecurityGroup ;
76
195
77
- # Network Interface
78
- $networkInterface = New-AzureRmNetworkInterface `
79
- - ResourceGroupName $ResourceGroupName `
80
- - Location $Location `
81
- - Name $NetworkInterfaceName `
82
- - PublicIpAddressId $publicIpAddress.Id `
83
- - SubnetId $virtualNetwork.Subnets [0 ].Id `
84
- - NetworkSecurityGroupId $securityGroup.Id
196
+ NetworkInterface(
197
+ [string ] $name ,
198
+ [VirtualNetwork ] $virtualNetwork ,
199
+ [PublicIpAddress ] $publicIpAddress ,
200
+ [SecurityGroup ] $securityGroup
201
+ ): base($name , @ ($virtualNetwork , $publicIpAddress , $securityGroup )) {
202
+ $this.VirtualNetwork = $virtualNetwork ;
203
+ $this.PublicIpAddress = $publicIpAddress ;
204
+ $this.SecurityGroup = $securityGroup ;
205
+ }
206
+
207
+ [object ] GetInfo() {
208
+ return Get-AzureRMNetworkInterface - Name $this.Name ;
209
+ }
210
+
211
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
212
+ $xpublicIpAddress = $this.PublicIpAddress.GetOrCreate ($name , $location , $resourceGroupName );
213
+ $xvirtualNetwork = $this.VirtualNetwork.GetOrCreate ($name , $location , $resourceGroupName );
214
+ $xsecurityGroup = $this.SecurityGroup.GetOrCreate ($name , $location , $resourceGroupName );
215
+ return New-AzureRmNetworkInterface `
216
+ - ResourceGroupName $resourceGroupName `
217
+ - Location $location `
218
+ - Name $name `
219
+ - PublicIpAddressId $xpublicIpAddress.Id `
220
+ - SubnetId $xvirtualNetwork.Subnets [0 ].Id `
221
+ - NetworkSecurityGroupId $xsecurityGroup.Id
222
+ }
223
+ }
224
+
225
+ class VirtualMachine : AzureObject {
226
+ [NetworkInterface ] $NetworkInterface ;
227
+ [pscredential ] $Credential ;
228
+ [string ] $ImageName ;
229
+ [object ] $Images ;
230
+
231
+ VirtualMachine(
232
+ [string ] $name ,
233
+ [NetworkInterface ] $networkInterface ,
234
+ [ResourceGroup ] $resourceGroup ,
235
+ [PSCredential ] $credential ,
236
+ [string ] $imageName ,
237
+ [object ] $images ):
238
+ base($name , @ ($networkInterface , $resourceGroup )) {
239
+
240
+ $this.Credential = $credential ;
241
+ $this.ImageName = $imageName ;
242
+ $this.NetworkInterface = $networkInterface ;
243
+ $this.Images = $images ;
244
+ }
245
+
246
+ [object ] GetInfo() {
247
+ return Get-AzureRMVirtualMachine - Name $this.Name ;
248
+ }
249
+
250
+ [object ] Create([string ] $name , [string ] $location , [string ] $resourceGroupName ) {
251
+ $networkInterfaceInstance = $this.NetworkInterface.GetOrCreate ( `
252
+ $name , $location , $resourceGroupName );
253
+
254
+ if (-not $this.Credential ) {
255
+ $this.Credential = Get-Credential
256
+ }
257
+
258
+ $vmImage = $this.Images | Where-Object { $_.Name -eq $this.ImageName } | Select-Object - First 1
259
+ if (-not $vmImage ) {
260
+ throw " Unknown image: " + $this.ImageName
261
+ }
85
262
86
- # VM
87
263
$vmSize = " Standard_DS2"
88
264
$vmConfig = New-AzureRmVMConfig - VMName $Name - VMSize $vmSize
89
265
$vmComputerName = $Name + " Computer"
@@ -92,13 +268,13 @@ function New-AzVm {
92
268
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
93
269
- Windows `
94
270
- ComputerName $vmComputerName `
95
- - Credential $Credential
271
+ - Credential $this . Credential
96
272
}
97
273
" Linux" {
98
274
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
99
275
- Linux `
100
276
- ComputerName $vmComputerName `
101
- - Credential $Credential
277
+ - Credential $this . Credential
102
278
}
103
279
}
104
280
@@ -109,33 +285,12 @@ function New-AzVm {
109
285
- Offer $vmImageImage.offer `
110
286
- Skus $vmImageImage.sku `
111
287
- Version $vmImageImage.version `
112
- | Add-AzureRmVMNetworkInterface - Id $networkInterface .Id
288
+ | Add-AzureRmVMNetworkInterface - Id $networkInterfaceInstance .Id
113
289
114
- $response = New-AzureRmVm `
115
- - ResourceGroupName $ResourceGroupName `
116
- - Location $Location `
290
+ return New-AzureRmVm `
291
+ - ResourceGroupName $resourceGroupName `
292
+ - Location $location `
117
293
- VM $vmConfig
118
-
119
- New-PsObject @ {
120
- ResourceId = $resourceGroup.ResourceId ;
121
- Response = $response
122
- }
123
- }
124
- }
125
-
126
- function Set-ResourceGroup {
127
- param (
128
- [parameter (Mandatory = $true )][string ]$Name ,
129
- [parameter (Mandatory = $true )][string ]$Location
130
- )
131
-
132
- $resourceGroup = Get-AzureRmResourceGroup `
133
- | Where-Object { $_.ResourceGroupName -eq $Name } `
134
- | Select-Object - First 1 - Wait;
135
- if ($resourceGroup ) {
136
- $resourceGroup ;
137
- } else {
138
- New-AzureRmResourceGroup - Name $ResourceGroupName - Location $Location
139
294
}
140
295
}
141
296
0 commit comments