Skip to content

Commit 040b552

Browse files
AzureRM.Compute.Experements
1 parent 57f9e73 commit 040b552

File tree

6 files changed

+283
-45
lines changed

6 files changed

+283
-45
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,5 @@ FakesAssemblies/
207207
/tools/*.dll
208208
*.GhostDoc.xml
209209
pingme.txt
210-
groupMapping*.json
210+
groupMapping*.json
211+
.vscode/

experiments/Compute.Experiments/Az.Compute.psm1

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,36 @@
2626

2727
function New-AzVm {
2828
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,
3131

3232
# Generate a random as a hash of the name so it will be idempotent (tack on resource group?).
3333
[Parameter(DontShow)]
34-
$Random = $(Get-Hash $Name),
34+
$Random = $(Get-Hash $Name),
3535

36-
[string] $Location = "",
36+
[string] $Location = "",
3737

38-
[string] $Image = "WinServer2016",
39-
[string] $Size = "Standard_DS1_v2",
38+
[string] $Image = "WinServer2016",
39+
[string] $Size = "Standard_DS1_v2",
4040

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",
4444

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,
4949

50-
[string] $NsgName = "$($Name)Nsg",
51-
[int[]] $NsgOpenPorts = $null,
50+
[string] $NsgName = "$($Name)Nsg",
51+
[int[]] $NsgOpenPorts = $null,
5252

5353
[string] $NicName = "$($Name)Nic"
5454

5555
# Storage - OS Disk Size.
5656
# Compute: "this goes above and beyond the 80% scenario".
5757
)
58-
58+
5959
try {
6060

6161
# Build credentials.
@@ -98,11 +98,16 @@ function New-AzVm {
9898
$pip = Use-Pip -Name $PublicIpName -ResourceGroup $ResourceGroup -Location $Location -DnsLabel $PublicIpDnsLabel -AllocationMethod $PublicIpAllocationMethod -IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;
9999

100100
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;
103102

104103
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;
106111

107112
# 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)?
108113
# 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
139144

140145
function Use-ResourceGroup {
141146
param (
142-
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
147+
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
143148
[Parameter(Mandatory=$true)] [string] $Location
144149
)
145-
150+
146151
$rg = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $ResourceGroup } | Select-Object -First 1 -Wait;
147-
152+
148153
if($rg -eq $null) {
149154
return New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location;
150155
} else {
@@ -154,13 +159,13 @@ function Use-ResourceGroup {
154159

155160
function Use-Vnet {
156161
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,
161166
[Parameter(Mandatory=$true)] [string] $VnetAddressPrefix
162167
)
163-
168+
164169
$vnet = Get-AzureRmVirtualNetwork | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;
165170

166171
if($vnet -eq $null) {
@@ -176,14 +181,14 @@ function Use-Vnet {
176181

177182
function Use-Pip {
178183
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,
184189
[Parameter(Mandatory=$true)] [int] $IdleTimeoutInMinutes
185190
)
186-
191+
187192
$pip = Get-AzureRmPublicIpAddress | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;
188193

189194
if($pip -eq $null) {
@@ -196,12 +201,12 @@ function Use-Pip {
196201

197202
function Use-Nsg {
198203
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,
202207
[Parameter(Mandatory=$true)] [int[]] $OpenPorts
203208
)
204-
209+
205210
$nsg = Get-AzureRmNetworkSecurityGroup | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;
206211

207212
if($nsg -eq $null) {
@@ -212,7 +217,7 @@ function Use-Nsg {
212217
{
213218
$nsgRule = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority $priority -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $port -Access Allow;
214219
$rules.Add($nsgRule);
215-
220+
216221
$priority--;
217222
}
218223

@@ -225,14 +230,14 @@ function Use-Nsg {
225230

226231
function Use-Nic {
227232
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,
233238
[Parameter(Mandatory=$true)] [psobject] $NetworkSecurityGroupId
234239
)
235-
240+
236241
$nic = Get-AzureRmNetworkInterface | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;
237242

238243
if($nic -eq $null) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1
2+
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Resources\AzureRM.Resources.psd1
3+
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Network\AzureRM.Network.psd1
4+
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Compute\AzureRM.Compute.psd1
5+
Import-Module .\..\..\experiments\Compute.Experiments\AzureRM.Compute.Experiments.psd1
6+
7+
# Login
8+
$credentials = Get-Content -Path "C:\Users\sergey\Desktop\php-test.json" | ConvertFrom-Json
9+
$clientSecret = ConvertTo-SecureString $credentials.clientSecret -AsPlainText -Force
10+
$pscredentials = New-Object System.Management.Automation.PSCredential($credentials.applicationId, $clientSecret)
11+
Login-AzureRmAccount -ServicePrincipal -Credential $pscredentials -TenantId $credentials.tenantId | Out-Null
12+
13+
New-AzVm
14+
15+
# clean-up
16+
Remove-AzureRmResourceGroup -Name $resourceGroupName
Binary file not shown.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
function New-AzVm {
2+
# Images
3+
Write-Host "Load images..."
4+
$jsonImages = Get-Content -Path "images.json" | ConvertFrom-Json
5+
Write-Host "done"
6+
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
19+
}
20+
}
21+
}
22+
23+
# Find VM Image
24+
$vmImageName = "Win2012R2Datacenter"
25+
$vmImage = $images | Where-Object { $_.Name -eq $vmImageName } | Select-Object -First 1
26+
27+
Write-Host $vmImage
28+
29+
# Location
30+
Write-Host "Load locations..."
31+
$location = (Get-AzureRmLocation | Select-Object -First 1 -Wait).Location
32+
$location = "eastus"
33+
Write-Host "done"
34+
35+
# Resource Group
36+
$resourceGroupName = "resourceGroupTest"
37+
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
38+
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
50+
51+
# Piblic IP
52+
$publicIpAddressName = "publicIpAddressTest"
53+
$publicIpAddress = New-AzureRmPublicIpAddress `
54+
-ResourceGroupName $resourceGroupName `
55+
-Location $location `
56+
-AllocationMethod Static `
57+
-Name $publicIpAddressName
58+
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
87+
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
97+
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" {
115+
116+
}
117+
}
118+
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
128+
129+
New-AzureRmVm -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig
130+
}
131+
132+
Export-ModuleMember -Function New-AzVm

0 commit comments

Comments
 (0)