26
26
27
27
function New-AzVm {
28
28
param (
29
- [Parameter (Mandatory = $true )] [string ] $Name ,
30
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
29
+ [Parameter (Mandatory = $true )] [string ] $Name ,
30
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
31
31
32
32
# Generate a random as a hash of the name so it will be idempotent (tack on resource group?).
33
33
[Parameter (DontShow)]
34
- $Random = $ (Get-Hash $Name ),
34
+ $Random = $ (Get-Hash $Name ),
35
35
36
- [string ] $Location = " " ,
36
+ [string ] $Location = " " ,
37
37
38
- [string ] $Image = " WinServer2016" ,
39
- [string ] $Size = " Standard_DS1_v2" ,
38
+ [string ] $Image = " WinServer2016" ,
39
+ [string ] $Size = " Standard_DS1_v2" ,
40
40
41
- [string ] $VnetName = " $ ( $Name ) Vnet" ,
42
- [string ] $SubnetAddressPrefix = " 192.168.1.0/24" ,
43
- [string ] $VnetAddressPrefix = " 192.168.0.0/16" ,
41
+ [string ] $VnetName = " $ ( $Name ) Vnet" ,
42
+ [string ] $SubnetAddressPrefix = " 192.168.1.0/24" ,
43
+ [string ] $VnetAddressPrefix = " 192.168.0.0/16" ,
44
44
45
- [string ] $PublicIpName = " $ ( $Name ) PublicIp" ,
46
- [string ] $PublicIpDnsLabel = " $Name -$Random " .ToLower(),
47
- [string ] $PublicIpAllocationMethod = " Static" ,
48
- [int ] $PublicIpIdleTimeoutInMinutes = 4 ,
45
+ [string ] $PublicIpName = " $ ( $Name ) PublicIp" ,
46
+ [string ] $PublicIpDnsLabel = " $Name -$Random " .ToLower(),
47
+ [string ] $PublicIpAllocationMethod = " Static" ,
48
+ [int ] $PublicIpIdleTimeoutInMinutes = 4 ,
49
49
50
- [string ] $NsgName = " $ ( $Name ) Nsg" ,
51
- [int []] $NsgOpenPorts = $null ,
50
+ [string ] $NsgName = " $ ( $Name ) Nsg" ,
51
+ [int []] $NsgOpenPorts = $null ,
52
52
53
53
[string ] $NicName = " $ ( $Name ) Nic"
54
54
55
55
# Storage - OS Disk Size.
56
56
# Compute: "this goes above and beyond the 80% scenario".
57
57
)
58
-
58
+
59
59
try {
60
60
61
61
# Build credentials.
@@ -98,11 +98,16 @@ function New-AzVm {
98
98
$pip = Use-Pip - Name $PublicIpName - ResourceGroup $ResourceGroup - Location $Location - DnsLabel $PublicIpDnsLabel - AllocationMethod $PublicIpAllocationMethod - IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes ;
99
99
100
100
Write-Info " Ensuring NSG..." ;
101
- $nsg = Use-Nsg - Name $PublicIpName - ResourceGroup $ResourceGroup - Location $Location - OpenPorts $NsgOpenPorts ;
102
- $Global :nsg = $nsg ;
101
+ $nsg = Use-Nsg - Name $NsgName - ResourceGroup $ResourceGroup - Location $Location - OpenPorts $NsgOpenPorts ;
103
102
104
103
Write-Info " Ensuring NIC..." ;
105
- $nic = Use-Nic - Name $NicName - ResourceGroup $ResourceGroup - Location $Location - SubnetId $vnet.Subnets [0 ].Id - PublicIpAddressId $pip.Id - NetworkSecurityGroupId $nsg.Id ;
104
+ $nic = Use-Nic `
105
+ - Name $NicName `
106
+ - ResourceGroup $ResourceGroup `
107
+ - Location $Location `
108
+ - SubnetId $vnet.Subnets [0 ].Id `
109
+ - PublicIpAddressId $pip.Id `
110
+ - NetworkSecurityGroupId $nsg.Id ;
106
111
107
112
# TODO: Add disk options (https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-sample-create-vm-from-managed-os-disks?toc=%2fpowershell%2fmodule%2ftoc.json)?
108
113
# https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmosdisk?view=azurermps-4.2.0
@@ -139,12 +144,12 @@ Export-ModuleMember -Function New-AzVm
139
144
140
145
function Use-ResourceGroup {
141
146
param (
142
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
147
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
143
148
[Parameter (Mandatory = $true )] [string ] $Location
144
149
)
145
-
150
+
146
151
$rg = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $ResourceGroup } | Select-Object - First 1 - Wait;
147
-
152
+
148
153
if ($rg -eq $null ) {
149
154
return New-AzureRmResourceGroup - Name $ResourceGroup - Location $Location ;
150
155
} else {
@@ -154,13 +159,13 @@ function Use-ResourceGroup {
154
159
155
160
function Use-Vnet {
156
161
param (
157
- [Parameter (Mandatory = $true )] [string ] $Name ,
158
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
159
- [Parameter (Mandatory = $true )] [string ] $Location ,
160
- [Parameter (Mandatory = $true )] [string ] $SubnetAddressPrefix ,
162
+ [Parameter (Mandatory = $true )] [string ] $Name ,
163
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
164
+ [Parameter (Mandatory = $true )] [string ] $Location ,
165
+ [Parameter (Mandatory = $true )] [string ] $SubnetAddressPrefix ,
161
166
[Parameter (Mandatory = $true )] [string ] $VnetAddressPrefix
162
167
)
163
-
168
+
164
169
$vnet = Get-AzureRmVirtualNetwork | Where-Object { $_.Name -eq $Name } | Select-Object - First 1 - Wait;
165
170
166
171
if ($vnet -eq $null ) {
@@ -176,14 +181,14 @@ function Use-Vnet {
176
181
177
182
function Use-Pip {
178
183
param (
179
- [Parameter (Mandatory = $true )] [string ] $Name ,
180
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
181
- [Parameter (Mandatory = $true )] [string ] $Location ,
182
- [Parameter (Mandatory = $true )] [string ] $DnsLabel ,
183
- [Parameter (Mandatory = $true )] [string ] $AllocationMethod ,
184
+ [Parameter (Mandatory = $true )] [string ] $Name ,
185
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
186
+ [Parameter (Mandatory = $true )] [string ] $Location ,
187
+ [Parameter (Mandatory = $true )] [string ] $DnsLabel ,
188
+ [Parameter (Mandatory = $true )] [string ] $AllocationMethod ,
184
189
[Parameter (Mandatory = $true )] [int ] $IdleTimeoutInMinutes
185
190
)
186
-
191
+
187
192
$pip = Get-AzureRmPublicIpAddress | Where-Object { $_.Name -eq $Name } | Select-Object - First 1 - Wait;
188
193
189
194
if ($pip -eq $null ) {
@@ -196,12 +201,12 @@ function Use-Pip {
196
201
197
202
function Use-Nsg {
198
203
param (
199
- [Parameter (Mandatory = $true )] [string ] $Name ,
200
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
201
- [Parameter (Mandatory = $true )] [string ] $Location ,
204
+ [Parameter (Mandatory = $true )] [string ] $Name ,
205
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
206
+ [Parameter (Mandatory = $true )] [string ] $Location ,
202
207
[Parameter (Mandatory = $true )] [int []] $OpenPorts
203
208
)
204
-
209
+
205
210
$nsg = Get-AzureRmNetworkSecurityGroup | Where-Object { $_.Name -eq $Name } | Select-Object - First 1 - Wait;
206
211
207
212
if ($nsg -eq $null ) {
@@ -212,7 +217,7 @@ function Use-Nsg {
212
217
{
213
218
$nsgRule = New-AzureRmNetworkSecurityRuleConfig - Name myNetworkSecurityGroupRuleRDP - Protocol Tcp - Direction Inbound - Priority $priority - SourceAddressPrefix * - SourcePortRange * - DestinationAddressPrefix * - DestinationPortRange $port - Access Allow;
214
219
$rules.Add ($nsgRule );
215
-
220
+
216
221
$priority -- ;
217
222
}
218
223
@@ -225,14 +230,14 @@ function Use-Nsg {
225
230
226
231
function Use-Nic {
227
232
param (
228
- [Parameter (Mandatory = $true )] [string ] $Name ,
229
- [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
230
- [Parameter (Mandatory = $true )] [string ] $Location ,
231
- [Parameter (Mandatory = $true )] [string ] $SubnetId ,
232
- [Parameter (Mandatory = $true )] [string ] $PublicIpAddressId ,
233
+ [Parameter (Mandatory = $true )] [string ] $Name ,
234
+ [Parameter (Mandatory = $true )] [string ] $ResourceGroup ,
235
+ [Parameter (Mandatory = $true )] [string ] $Location ,
236
+ [Parameter (Mandatory = $true )] [string ] $SubnetId ,
237
+ [Parameter (Mandatory = $true )] [string ] $PublicIpAddressId ,
233
238
[Parameter (Mandatory = $true )] [psobject ] $NetworkSecurityGroupId
234
239
)
235
-
240
+
236
241
$nic = Get-AzureRmNetworkInterface | Where-Object { $_.Name -eq $Name } | Select-Object - First 1 - Wait;
237
242
238
243
if ($nic -eq $null ) {
0 commit comments