Skip to content

Commit a321a65

Browse files
Set-ResourceGroup
1 parent 02692a7 commit a321a65

File tree

3 files changed

+103
-32
lines changed

3 files changed

+103
-32
lines changed

experiments/Compute.Experiments/Az.Compute.psm1

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,33 @@ function New-AzVm {
8989
# Create!
9090

9191
Write-Info "Ensuring resource group...";
92-
$rg = Use-ResourceGroup -ResourceGroup $ResourceGroup -Location $Location;
92+
$rg = Use-ResourceGroup `
93+
-ResourceGroup $ResourceGroup `
94+
-Location $Location;
9395

9496
Write-Info "Ensuring Vnet...";
95-
$vnet = Use-Vnet -Name $VnetName -ResourceGroup $ResourceGroup -Location $Location -SubnetAddressPrefix $SubnetAddressPrefix -VnetAddressPrefix $VnetAddressPrefix;
97+
$vnet = Use-Vnet `
98+
-Name $VnetName `
99+
-ResourceGroup $ResourceGroup `
100+
-Location $Location `
101+
-SubnetAddressPrefix $SubnetAddressPrefix `
102+
-VnetAddressPrefix $VnetAddressPrefix;
96103

97104
Write-Info "Ensuring public IP...";
98-
$pip = Use-Pip -Name $PublicIpName -ResourceGroup $ResourceGroup -Location $Location -DnsLabel $PublicIpDnsLabel -AllocationMethod $PublicIpAllocationMethod -IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;
105+
$pip = Use-Pip `
106+
-Name $PublicIpName `
107+
-ResourceGroup $ResourceGroup `
108+
-Location $Location `
109+
-DnsLabel $PublicIpDnsLabel `
110+
-AllocationMethod $PublicIpAllocationMethod `
111+
-IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;
99112

100113
Write-Info "Ensuring NSG...";
101-
$nsg = Use-Nsg -Name $NsgName -ResourceGroup $ResourceGroup -Location $Location -OpenPorts $NsgOpenPorts;
114+
$nsg = Use-Nsg `
115+
-Name $NsgName `
116+
-ResourceGroup $ResourceGroup `
117+
-Location $Location `
118+
-OpenPorts $NsgOpenPorts;
102119

103120
Write-Info "Ensuring NIC...";
104121
$nic = Use-Nic `
@@ -113,10 +130,14 @@ function New-AzVm {
113130
# https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmosdisk?view=azurermps-4.2.0
114131

115132
# Create a virtual machine configuration
116-
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $Size | `
117-
Set-AzureRmVMOperatingSystem -Windows -ComputerName $Name -Credential $creds | `
118-
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
119-
Add-AzureRmVMNetworkInterface -Id $nic.Id
133+
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $Size `
134+
| Set-AzureRmVMOperatingSystem -Windows -ComputerName $Name -Credential $creds `
135+
| Set-AzureRmVMSourceImage `
136+
-PublisherName MicrosoftWindowsServer `
137+
-Offer WindowsServer `
138+
-Skus 2016-Datacenter `
139+
-Version latest `
140+
| Add-AzureRmVMNetworkInterface -Id $nic.Id
120141

121142
# Create a virtual machine
122143
$vm = New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
@@ -148,7 +169,9 @@ function Use-ResourceGroup {
148169
[Parameter(Mandatory=$true)] [string] $Location
149170
)
150171

151-
$rg = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $ResourceGroup } | Select-Object -First 1 -Wait;
172+
$rg = Get-AzureRmResourceGroup `
173+
| Where-Object { $_.ResourceGroupName -eq $ResourceGroup } `
174+
| Select-Object -First 1 -Wait;
152175

153176
if($rg -eq $null) {
154177
return New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location;
@@ -170,10 +193,17 @@ function Use-Vnet {
170193

171194
if($vnet -eq $null) {
172195
# Create a subnet configuration.
173-
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name "$($Name)Subnet" -AddressPrefix $SubnetAddressPrefix;
196+
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
197+
-Name "$($Name)Subnet" `
198+
-AddressPrefix $SubnetAddressPrefix;
174199

175200
# Create a virtual network.
176-
return New-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -AddressPrefix $VnetAddressPrefix -Subnet $subnetConfig
201+
return New-AzureRmVirtualNetwork `
202+
-ResourceGroupName $ResourceGroup `
203+
-Location $Location `
204+
-Name $Name `
205+
-AddressPrefix $VnetAddressPrefix `
206+
-Subnet $subnetConfig
177207
} else {
178208
return $vnet;
179209
}
@@ -193,7 +223,13 @@ function Use-Pip {
193223

194224
if($pip -eq $null) {
195225
# Create a public IP address and specify a DNS name.
196-
return New-AzureRmPublicIpAddress -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -DomainNameLabel $DnsLabel -AllocationMethod $AllocationMethod -IdleTimeoutInMinutes $IdleTimeoutInMinutes;
226+
return New-AzureRmPublicIpAddress `
227+
-ResourceGroupName $ResourceGroup `
228+
-Location $Location `
229+
-Name $Name `
230+
-DomainNameLabel $DnsLabel `
231+
-AllocationMethod $AllocationMethod `
232+
-IdleTimeoutInMinutes $IdleTimeoutInMinutes;
197233
} else {
198234
return $pip;
199235
}
@@ -210,19 +246,33 @@ function Use-Nsg {
210246
$nsg = Get-AzureRmNetworkSecurityGroup | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;
211247

212248
if($nsg -eq $null) {
213-
$rules = New-Object "System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSSecurityRule]";
249+
$rules = New-Object `
250+
"System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSSecurityRule]";
214251
$priority = 1000;
215252

216253
foreach($port in $OpenPorts)
217254
{
218-
$nsgRule = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority $priority -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $port -Access Allow;
255+
$nsgRule = New-AzureRmNetworkSecurityRuleConfig `
256+
-Name myNetworkSecurityGroupRuleRDP `
257+
-Protocol Tcp `
258+
-Direction Inbound `
259+
-Priority $priority `
260+
-SourceAddressPrefix * `
261+
-SourcePortRange * `
262+
-DestinationAddressPrefix * `
263+
-DestinationPortRange $port `
264+
-Access Allow;
219265
$rules.Add($nsgRule);
220266

221267
$priority--;
222268
}
223269

224270
# Create an NSG.
225-
return New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -SecurityRules $rules;
271+
return New-AzureRmNetworkSecurityGroup `
272+
-ResourceGroupName $ResourceGroup `
273+
-Location $Location `
274+
-Name $Name `
275+
-SecurityRules $rules;
226276
} else {
227277
return $nsg;
228278
}
@@ -242,7 +292,13 @@ function Use-Nic {
242292

243293
if($nic -eq $null) {
244294
# Create a virtual network card and associate with public IP address and NSG
245-
return New-AzureRmNetworkInterface -Name $Name -ResourceGroupName $resourceGroup -Location $location -SubnetId $SubnetId -PublicIpAddressId $PublicIpAddressId -NetworkSecurityGroupId $NetworkSecurityGroupId.ToString();
295+
return New-AzureRmNetworkInterface `
296+
-Name $Name `
297+
-ResourceGroupName $resourceGroup `
298+
-Location $location `
299+
-SubnetId $SubnetId `
300+
-PublicIpAddressId $PublicIpAddressId `
301+
-NetworkSecurityGroupId $NetworkSecurityGroupId.ToString();
246302
} else {
247303
return $nic;
248304
}

experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ function New-AzVm {
2323
throw "Unknown image: " + $ImageName
2424
}
2525

26-
# Location
27-
# $Location = (Get-AzureRmLocation | Select-Object -First 1 -Wait).Location
28-
2926
# Resource Group
30-
$resource = New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
27+
$resourceGroup = Set-ResourceGroup -Name $ResourceGroupName -Location $Location
3128

3229
# Virtual Network
3330
$virtualNetworkAddressPrefix = "192.168.0.0/16"
@@ -87,20 +84,20 @@ function New-AzVm {
8784
-NetworkSecurityGroupId $securityGroup.Id
8885

8986
# VM
90-
$vm = @{ Name = $Name; Size = "Standard_DS2" }
91-
$vmConfig = New-AzureRmVMConfig -VMName $vm.Name -VMSize $vm.Size
92-
$vmComputer = $vm.Name
87+
$vmSize = "Standard_DS2"
88+
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $vmSize
89+
$vmComputerName = $Name + "Computer"
9390
switch ($vmImage.Type) {
9491
"Windows" {
9592
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
9693
-Windows `
97-
-ComputerName $vmComputer `
94+
-ComputerName $vmComputerName `
9895
-Credential $Credential
9996
}
10097
"Linux" {
10198
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
10299
-Linux `
103-
-ComputerName $vmComputer `
100+
-ComputerName $vmComputerName `
104101
-Credential $Credential
105102
}
106103
}
@@ -114,16 +111,34 @@ function New-AzVm {
114111
-Version $vmImageImage.version `
115112
| Add-AzureRmVMNetworkInterface -Id $networkInterface.Id
116113

114+
$response = New-AzureRmVm `
115+
-ResourceGroupName $ResourceGroupName `
116+
-Location $Location `
117+
-VM $vmConfig
118+
117119
New-PsObject @{
118-
ResourceId = $resource.ResourceId;
119-
Response = New-AzureRmVm `
120-
-ResourceGroupName $ResourceGroupName `
121-
-Location $Location `
122-
-VM $vmConfig
120+
ResourceId = $resourceGroup.ResourceId;
121+
Response = $response
123122
}
124123
}
125124
}
126125

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+
}
140+
}
141+
127142
function New-PsObject {
128143
param([hashtable] $property)
129144

experiments/Compute.Experiments/publish-dev.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ $out = "..\build\AzureRM.Compute.Experiments\"
22
$repository = "sergey"
33
$dep = @("AzureRM.Resources", "AzureRM.Network", "AzureRM.Compute")
44
mkdir $out
5-
copy .\AzureRM.Compute.Experiments.psd1 $out
6-
copy .\AzureRM.Compute.Experiments.psm1 $out
5+
Copy-Item .\AzureRM.Compute.Experiments.psd1 $out
6+
Copy-Item .\AzureRM.Compute.Experiments.psm1 $out
77
foreach ($d in $dep) {
88
Install-Module $d -Repository $repository
99
}

0 commit comments

Comments
 (0)