@@ -15,12 +15,13 @@ function New-AzVm {
1515 PROCESS {
1616 $rgi = [ResourceGroup ]::new($ResourceGroupName );
1717 $vni = [VirtualNetwork ]::new($VirtualNetworkName );
18+ $piai = [PublicIpAddress ]::new($PublicIpAddressName );
19+ $sgi = [SecurityGroup ]::new($SecurityGroupName );
1820 $nii = [NetworkInterface ]::new(
1921 $NetworkInterfaceName ,
2022 $vni ,
21- [PublicIpAddress ]::new($PublicIpAddressName ),
22- [SecurityGroup ]::new($SecurityGroupName )
23- );
23+ $piai ,
24+ $sgi );
2425 $vmi = [VirtualMachine ]::new($null , $nii , $rgi );
2526
2627 $locationi = [Location ]::new();
@@ -33,12 +34,19 @@ function New-AzVm {
3334 $locationi.Value = $Location ;
3435 }
3536
36- # Resource Group
3737 $resourceGroup = $rgi.GetOrCreate ($Name + " ResourceGroup" , $locationi.Value , $null );
3838 $virtualNetwork = $vni.GetOrCreate (
3939 $Name + " VirtualNetwork" ,
4040 $locationi.Value ,
4141 $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 )
4250
4351 if (-not $Credential ) {
4452 $Credential = Get-Credential
@@ -65,59 +73,6 @@ function New-AzVm {
6573 throw " Unknown image: " + $ImageName
6674 }
6775
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-
12176 # Network Interface
12277 $networkInterface = New-AzureRmNetworkInterface `
12378 - ResourceGroupName $ResourceGroupName `
@@ -289,6 +244,14 @@ class PublicIpAddress: Resource1 {
289244 [object ] GetInfo() {
290245 return Get-AzureRMPublicIpAddress - Name $this.Name ;
291246 }
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+ }
292255}
293256
294257class SecurityGroup : Resource1 {
@@ -298,6 +261,37 @@ class SecurityGroup: Resource1 {
298261 [object ] GetInfo() {
299262 return Get-AzureRMSecurityGroup - Name $this.Name ;
300263 }
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+ }
301295}
302296
303297class NetworkInterface : AzureObject {
@@ -316,15 +310,16 @@ class NetworkInterface: AzureObject {
316310
317311class VirtualMachine : AzureObject {
318312 VirtualMachine(
319- [string ] $name , [NetworkInterface ] $networkInterface , [ResourceGroup ] $resourceGroup
313+ [string ] $name ,
314+ [NetworkInterface ] $networkInterface ,
315+ [ResourceGroup ] $resourceGroup
320316 ): base($name , @ ($networkInterface , $resourceGroup )) {
321317 }
322318
323319 [object ] GetInfo() {
324320 return Get-AzureRMVirtualMachine - Name $this.Name ;
325321 }
326322}
327-
328323function New-PsObject {
329324 param ([hashtable ] $property )
330325
0 commit comments