1
1
function New-AzVm {
2
- # Images
3
- Write-Host " Load images..."
4
- $jsonImages = Get-Content - Path " images.json" | ConvertFrom-Json
5
- Write-Host " done"
2
+ [CmdletBinding ()]
3
+ param ()
6
4
7
- # an array of @{ Type = ...; Name = ...; Image = ... }
8
- $images = $jsonImages.outputs.aliases.value.psobject.Properties | ForEach-Object {
9
- # e.g. "Linux"
10
- $type = $_.Name
11
- $_.Value.psobject.Properties | ForEach-Object {
12
- New-Object - TypeName psobject - Property @ {
13
- # e.g. "Linux"
14
- Type = $type ;
15
- # e.g. "CentOs"
16
- Name = $_.Name ;
17
- # e.g. @{ publisher = "OpenLogic"; offer = "CentOS"; sku = "7.3"; version = "latest" }
18
- Image = $_.Value
5
+ PROCESS {
6
+ # Images
7
+ Write-Host " Load images..."
8
+ $jsonImages = Get-Content - Path " images.json" | ConvertFrom-Json
9
+ Write-Host " done"
10
+
11
+ # an array of @{ Type = ...; Name = ...; Image = ... }
12
+ $images = $jsonImages.outputs.aliases.value.psobject.Properties | ForEach-Object {
13
+ # e.g. "Linux"
14
+ $type = $_.Name
15
+ $_.Value.psobject.Properties | ForEach-Object {
16
+ New-Object - TypeName psobject - Property @ {
17
+ # e.g. "Linux"
18
+ Type = $type ;
19
+ # e.g. "CentOs"
20
+ Name = $_.Name ;
21
+ # e.g. @{ publisher = "OpenLogic"; offer = "CentOS"; sku = "7.3"; version = "latest" }
22
+ Image = $_.Value
23
+ }
19
24
}
20
25
}
21
- }
22
26
23
- # Find VM Image
24
- $vmImageName = " Win2012R2Datacenter"
25
- $vmImage = $images | Where-Object { $_.Name -eq $vmImageName } | Select-Object - First 1
27
+ # Find VM Image
28
+ $vmImageName = " Win2012R2Datacenter"
29
+ $vmImage = $images | Where-Object { $_.Name -eq $vmImageName } | Select-Object - First 1
26
30
27
- Write-Host $vmImage
31
+ Write-Host $vmImage
28
32
29
- # Location
30
- Write-Host " Load locations..."
31
- $location = (Get-AzureRmLocation | Select-Object - First 1 - Wait).Location
32
- $location = " eastus"
33
- Write-Host " done"
33
+ # Location
34
+ Write-Host " Load locations..."
35
+ $location = (Get-AzureRmLocation | Select-Object - First 1 - Wait).Location
36
+ $location = " eastus"
37
+ Write-Host " done"
34
38
35
- # Resource Group
36
- $resourceGroupName = " resourceGroupTest"
37
- New-AzureRmResourceGroup - Name $resourceGroupName - Location $location
39
+ # Resource Group
40
+ $resourceGroupName = " resourceGroupTest"
41
+ New-AzureRmResourceGroup - Name $resourceGroupName - Location $location
38
42
39
- # Virtual Network
40
- $virtualNetworkName = " virtualNetworkTest"
41
- $virtualNetworkAddressPrefix = " 192.168.0.0/16"
42
- $subnet = @ { Name = " subnetTest" ; AddressPrefix = " 192.168.1.0/24" }
43
- $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig - Name $subnet.Name - AddressPrefix $subnet.AddressPrefix
44
- $virtualNetwork = New-AzureRmVirtualNetwork `
45
- - ResourceGroupName $resourceGroupName `
46
- - Location $location `
47
- - Name $virtualNetworkName `
48
- - AddressPrefix $virtualNetworkAddressPrefix `
49
- - Subnet $subnetConfig
43
+ # Virtual Network
44
+ $virtualNetworkName = " virtualNetworkTest"
45
+ $virtualNetworkAddressPrefix = " 192.168.0.0/16"
46
+ $subnet = @ { Name = " subnetTest" ; AddressPrefix = " 192.168.1.0/24" }
47
+ $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig - Name $subnet.Name - AddressPrefix $subnet.AddressPrefix
48
+ $virtualNetwork = New-AzureRmVirtualNetwork `
49
+ - ResourceGroupName $resourceGroupName `
50
+ - Location $location `
51
+ - Name $virtualNetworkName `
52
+ - AddressPrefix $virtualNetworkAddressPrefix `
53
+ - Subnet $subnetConfig
50
54
51
- # Piblic IP
52
- $publicIpAddressName = " publicIpAddressTest"
53
- $publicIpAddress = New-AzureRmPublicIpAddress `
54
- - ResourceGroupName $resourceGroupName `
55
- - Location $location `
56
- - AllocationMethod Static `
57
- - Name $publicIpAddressName
55
+ # Piblic IP
56
+ $publicIpAddressName = " publicIpAddressTest"
57
+ $publicIpAddress = New-AzureRmPublicIpAddress `
58
+ - ResourceGroupName $resourceGroupName `
59
+ - Location $location `
60
+ - AllocationMethod Static `
61
+ - Name $publicIpAddressName
58
62
59
- # Security Group (it may have several rules(ports))
60
- $securityGroupName = " securityGroupTest"
61
- $securityRule = @ {
62
- Name = " securityRuleTest" ;
63
- Protocol = " Tcp" ;
64
- Priority = 1000 ;
65
- Access = " Allow" ;
66
- Direction = " Inbound" ;
67
- SourcePortRange = " *" ;
68
- SourceAddressPrefix = " *" ;
69
- DestinationPortRange = 3389 ;
70
- DestinationAddressPrefix = " *" ;
71
- }
72
- $securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
73
- - Name $securityRule.Name `
74
- - Protocol $securityRule.Protocol `
75
- - Priority $securityRule.Priority `
76
- - Access $securityRule.Access `
77
- - Direction $securityRule.Direction `
78
- - SourcePortRange $securityRule.SourcePortRange `
79
- - SourceAddressPrefix $securityRule.SourceAddressPrefix `
80
- - DestinationPortRange $securityRule.DestinationPortRange `
81
- - DestinationAddressPrefix $securityRule.DestinationAddressPrefix
82
- $securityGroup = New-AzureRmNetworkSecurityGroup `
83
- - ResourceGroupName $resourceGroupName `
84
- - Location $location `
85
- - Name $securityGroupName `
86
- - SecurityRules $securityRuleConfig
63
+ # Security Group (it may have several rules(ports))
64
+ $securityGroupName = " securityGroupTest"
65
+ $securityRule = @ {
66
+ Name = " securityRuleTest" ;
67
+ Protocol = " Tcp" ;
68
+ Priority = 1000 ;
69
+ Access = " Allow" ;
70
+ Direction = " Inbound" ;
71
+ SourcePortRange = " *" ;
72
+ SourceAddressPrefix = " *" ;
73
+ DestinationPortRange = 3389 ;
74
+ DestinationAddressPrefix = " *" ;
75
+ }
76
+ $securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
77
+ - Name $securityRule.Name `
78
+ - Protocol $securityRule.Protocol `
79
+ - Priority $securityRule.Priority `
80
+ - Access $securityRule.Access `
81
+ - Direction $securityRule.Direction `
82
+ - SourcePortRange $securityRule.SourcePortRange `
83
+ - SourceAddressPrefix $securityRule.SourceAddressPrefix `
84
+ - DestinationPortRange $securityRule.DestinationPortRange `
85
+ - DestinationAddressPrefix $securityRule.DestinationAddressPrefix
86
+ $securityGroup = New-AzureRmNetworkSecurityGroup `
87
+ - ResourceGroupName $resourceGroupName `
88
+ - Location $location `
89
+ - Name $securityGroupName `
90
+ - SecurityRules $securityRuleConfig
87
91
88
- # Network Interface
89
- $networkInterfaceName = " networkInterfaceTest"
90
- $networkInterface = New-AzureRmNetworkInterface `
91
- - ResourceGroupName $resourceGroupName `
92
- - Location $location `
93
- - Name $networkInterfaceName `
94
- - PublicIpAddressId $publicIpAddress.Id `
95
- - SubnetId $virtualNetwork.Subnets [0 ].Id `
96
- - NetworkSecurityGroupId $securityGroup.Id
92
+ # Network Interface
93
+ $networkInterfaceName = " networkInterfaceTest"
94
+ $networkInterface = New-AzureRmNetworkInterface `
95
+ - ResourceGroupName $resourceGroupName `
96
+ - Location $location `
97
+ - Name $networkInterfaceName `
98
+ - PublicIpAddressId $publicIpAddress.Id `
99
+ - SubnetId $virtualNetwork.Subnets [0 ].Id `
100
+ - NetworkSecurityGroupId $securityGroup.Id
97
101
98
- # VM
99
- # $vmCredentials = Get-Credential
100
- $vm = @ { Name = " vmTest" ; Size = " Standard_DS2" }
101
- $vmConfig = New-AzureRmVMConfig - VMName $vm.Name - VMSize $vm.Size
102
- $vmComputer = $vm.Name
103
- $vmComputerPassword = " E5v7e9!@%f" ;
104
- $vmComputerUser = " special" ;
105
- switch ($vmImage.Type ) {
106
- " Windows" {
107
- $password = ConvertTo-SecureString $vmComputerPassword - AsPlainText - Force;
108
- $cred = New-Object System.Management.Automation.PSCredential ($vmComputerUser , $password );
109
- $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
110
- - Windows `
111
- - ComputerName $vmComputer `
112
- - Credential $cred
113
- }
114
- " Linux" {
102
+ # VM
103
+ # $vmCredentials = Get-Credential
104
+ $vm = @ { Name = " vmTest" ; Size = " Standard_DS2" }
105
+ $vmConfig = New-AzureRmVMConfig - VMName $vm.Name - VMSize $vm.Size
106
+ $vmComputer = $vm.Name
107
+ $vmComputerPassword = " E5v7e9!@%f" ;
108
+ $vmComputerUser = " special" ;
109
+ switch ($vmImage.Type ) {
110
+ " Windows" {
111
+ $password = ConvertTo-SecureString $vmComputerPassword - AsPlainText - Force;
112
+ $cred = New-Object System.Management.Automation.PSCredential ($vmComputerUser , $password );
113
+ $vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
114
+ - Windows `
115
+ - ComputerName $vmComputer `
116
+ - Credential $cred
117
+ }
118
+ " Linux" {
115
119
120
+ }
116
121
}
117
- }
118
122
119
- $vmImageImage = $vmImage.Image
120
- Write-Host $vmImageImage
121
- $vmConfig = $vmConfig `
122
- | Set-AzureRmVMSourceImage `
123
- - PublisherName $vmImageImage.publisher `
124
- - Offer $vmImageImage.offer `
125
- - Skus $vmImageImage.sku `
126
- - Version $vmImageImage.version `
127
- | Add-AzureRmVMNetworkInterface - Id $networkInterface.Id
123
+ $vmImageImage = $vmImage.Image
124
+ Write-Host $vmImageImage
125
+ $vmConfig = $vmConfig `
126
+ | Set-AzureRmVMSourceImage `
127
+ - PublisherName $vmImageImage.publisher `
128
+ - Offer $vmImageImage.offer `
129
+ - Skus $vmImageImage.sku `
130
+ - Version $vmImageImage.version `
131
+ | Add-AzureRmVMNetworkInterface - Id $networkInterface.Id
128
132
129
- New-AzureRmVm - ResourceGroupName $resourceGroupName - Location $location - VM $vmConfig
133
+ New-AzureRmVm - ResourceGroupName $resourceGroupName - Location $location - VM $vmConfig
134
+ }
130
135
}
131
136
132
137
Export-ModuleMember - Function New-AzVm
0 commit comments