Skip to content

Commit a034a9b

Browse files
AsJob
1 parent 35ba386 commit a034a9b

File tree

4 files changed

+105
-89
lines changed

4 files changed

+105
-89
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Import-Module AzureRM.Profile -MinimumVersion 3.3.2
2-
# Import-Module AzureRM.Resources -MinimumVersion 4.3.2
3-
# Import-Module AzureRM.Network -MinimumVersion 4.3.2
4-
# Import-Module AzureRM.Compute -MinimumVersion 3.3.2
5-
Import-Module .\..\..\experiments\Compute.Experiments\AzureRM.Compute.Experiments.psd1
1+
# $build = "..\build\"
2+
# $out = $build + "AzureRM.Compute.Experiments\"
3+
# Copy-Item .\AzureRM.Compute.Experiments.psd1 $out
4+
# Copy-Item .\AzureRM.Compute.Experiments.psm1 $out
5+
6+
$env:PSModulePath = $env:PSModulePath + ";" + $build
67

78
# Login
89
$credentials = Get-Content -Path "C:\Users\sergey\Desktop\php-test.json" | ConvertFrom-Json
@@ -17,20 +18,26 @@ $vmCredential = New-Object System.Management.Automation.PSCredential ($vmCompute
1718

1819
New-AzVm -Name MyVM -Credential $vmCredential -WhatIf
1920

20-
$job = New-AzVm -Name MyVMA -Credential $vmCredential -AsJob
21-
22-
$vm = Receive-Job $job
23-
24-
$vm
21+
# $job = New-AzVm -Name MyVMA -Credential $vmCredential -AsJob
22+
# Receive-Job $job
2523

26-
exit
24+
# exit
2725

2826
# $vm = New-AzVm
2927
# $vm = New-AzVm -Credential $vmCredential
30-
$vm = New-AzVm -Name MyVMA -Credential $vmCredential
28+
# $vm = New-AzVm -Name MyVMA -Credential $vmCredential
3129
# $vm = New-AzVm -Name MyVMA
3230

3331
$vm
3432

33+
Write-Host "<async>"
34+
35+
$job = New-AzVm -Name MyVMA2 -Credential $vmCredential -AsJob
36+
$vm = Receive-Job $job
37+
38+
$vm
39+
40+
Write-Host "</async>"
41+
3542
# clean-up
3643
Remove-AzureRmResourceGroup -ResourceId $vm.ResourceGroupId

experiments/Compute.Experiments/AzureRM.Compute.Experiments.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = ".\AzureRM.Compute.Experiments.psm1"
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.9'
15+
ModuleVersion = '1.0.19'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -73,7 +73,7 @@ RequiredModules = @(
7373
# NestedModules = @()
7474

7575
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
76-
FunctionsToExport = 'New-AzVm'
76+
FunctionsToExport = 'New-AzVm', 'New-AzVmInternal'
7777

7878
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7979
# CmdletsToExport =

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

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@ function New-AzVm {
2626
[Parameter()][string] $ImageName = "Win2016Datacenter",
2727
[Parameter()][string] $Size = "Standard_DS1_v2",
2828

29+
[Parameter()][object] $AzureRmContext,
2930
[Parameter()][switch] $AsJob
3031
)
3132

3233
PROCESS {
33-
$rgi = [ResourceGroup]::new($ResourceGroupName);
34+
# TODO: make sure it's logged in.
35+
$context = if ($AzureRmContext) {
36+
Get-AzureRmContext -AzureRmContext $AzureRmContext
37+
} else {
38+
Get-AzureRmContext
39+
}
40+
41+
$rgi = [ResourceGroup]::new($ResourceGroupName)
3442

35-
$vni = [VirtualNetwork]::new($VirtualNetworkName, $AddressPrefix);
36-
$subnet = [Subnet]::new($SubnetName, $vni, $SubnetAddressPrefix);
37-
$piai = [PublicIpAddress]::new($PublicIpAddressName, $DomainNameLabel, $AllocationMethod);
38-
$sgi = [SecurityGroup]::new($SecurityGroupName, $OpenPorts);
43+
$vni = [VirtualNetwork]::new($VirtualNetworkName, $AddressPrefix)
44+
$subnet = [Subnet]::new($SubnetName, $vni, $SubnetAddressPrefix)
45+
$piai = [PublicIpAddress]::new($PublicIpAddressName, $DomainNameLabel, $AllocationMethod)
46+
$sgi = [SecurityGroup]::new($SecurityGroupName, $OpenPorts)
3947

4048
# we don't allow to reuse NetworkInterface so $name is $null.
4149
$nii = [NetworkInterface]::new(
4250
$null,
4351
$subnet,
4452
$piai,
45-
$sgi);
53+
$sgi)
4654

4755
# the purpouse of the New-AzVm cmdlet is to create (not get) a VM so $name is $null.
4856
$vmi = [VirtualMachine]::new(
@@ -57,52 +65,42 @@ function New-AzVm {
5765
# infer a location
5866
$locationi = [Location]::new();
5967
if (-not $Location) {
60-
$vmi.UpdateLocation($locationi);
68+
$vmi.UpdateLocation($locationi, $context);
6169
if (-not $locationi.Value) {
6270
$locationi.Value = "eastus";
6371
}
6472
} else {
6573
$locationi.Value = $Location;
6674
}
6775

68-
$createParams = [CreateParams]::new($Name, $locationi.Value, $Name);
76+
$createParams = [CreateParams]::new($Name, $locationi.Value, $Name, $context);
6977

7078
if ($PSCmdlet.ShouldProcess($Name, "Creating a virtual machine")) {
79+
7180
if ($AsJob) {
81+
$boundParams = $PSCmdlet.MyInvocation.BoundParameters
82+
$arguments = @{ 'AzureRmContext' = $context }
83+
foreach ($argName in $boundParams.Keys) {
84+
if ($argName -ne 'AsJob' -and $argName -ne 'AzureRmContext') {
85+
$arguments[$argName] = $boundParams[$argName]
86+
}
87+
}
7288
$script = {
73-
param($rgi, $vmi, $createParams)
74-
New-AzVmInternal `
75-
-ResourceGroup $rgi `
76-
-VirtualMachine $vmi `
77-
-CreateParams $createParams `
78-
-ErrorAction Stop
89+
[hashtable] $params = $args[0]
90+
New-AzVm @params
7991
}
80-
return Start-Job -ScriptBlock $script -ArgumentList @($rgi, $vmi, $createParams)
92+
return Start-Job $script -ArgumentList $arguments
8193
} else {
82-
return New-AzVmInternal `
83-
-ResourceGroup $rgi `
84-
-VirtualMachine $vmi `
85-
-CreateParams $createParams `
86-
-ErrorAction Stop
94+
$rg = $rgi.GetOrCreate($createParams);
95+
$vmResponse = $vmi.Create($createParams);
96+
return [PSAzureVm]::new(
97+
$rg.ResourceId,
98+
$VirtualMachine.Name
99+
);
87100
}
88101
}
89102
}
90103
}
91-
function New-AzVmInternal {
92-
param (
93-
[ResourceGroup] $ResourceGroup,
94-
[VirtualMachine] $VirtualMachine,
95-
[CreateParams] $createParams
96-
)
97-
98-
$rg = $ResourceGroup.GetOrCreate($createParams);
99-
$vmResponse = $VirtualMachine.Create($createParams);
100-
101-
return [PSAzureVm]::new(
102-
$rg.ResourceId,
103-
$VirtualMachine.Name
104-
);
105-
}
106104

107105
class PSAzureVm {
108106
[string] $ResourceGroupId;
@@ -128,11 +126,18 @@ class CreateParams {
128126
[string] $Name;
129127
[string] $Location;
130128
[string] $ResourceGroupName;
129+
[object] $Context;
131130

132-
CreateParams([string] $name, [string] $location, [string] $resourceGroupName) {
133-
$this.Name = $name;
134-
$this.Location = $location;
135-
$this.ResourceGroupName = $resourceGroupName;
131+
CreateParams(
132+
[string] $name,
133+
[string] $location,
134+
[string] $resourceGroupName,
135+
[object] $context)
136+
{
137+
$this.Name = $name
138+
$this.Location = $location
139+
$this.ResourceGroupName = $resourceGroupName
140+
$this.Context = $context
136141
}
137142
}
138143

@@ -154,30 +159,30 @@ class AzureObject {
154159
}
155160

156161
# This function should be called only when $this.Name is not $null.
157-
[object] GetInfo() {
162+
[object] GetInfo([object] $context) {
158163
return $null;
159164
}
160165

161166
[object] Create([CreateParams] $p) {
162167
return $null;
163168
}
164169

165-
[void] UpdateLocation([Location] $location) {
170+
[void] UpdateLocation([Location] $location, [object] $context) {
166171
if ($this.Priority -gt $location.Priority) {
167172
if ($this.Name) {
168-
$location.Value = $this.GetInfo().Location;
173+
$location.Value = $this.GetInfo($context).Location;
169174
$location.Priority = $this.Priority;
170175
} else {
171176
foreach ($child in $this.Children) {
172-
$child.UpdateLocation($location);
177+
$child.UpdateLocation($location, $context);
173178
}
174179
}
175180
}
176181
}
177182

178183
[object] GetOrCreate([CreateParams] $p) {
179184
if ($this.Name) {
180-
return $this.GetInfo();
185+
return $this.GetInfo($p.Context);
181186
} else {
182187
$result = $this.Create($p);
183188
$this.Name = $p.Name;
@@ -190,14 +195,15 @@ class ResourceGroup: AzureObject {
190195
ResourceGroup([string] $name): base($name, @()) {
191196
}
192197

193-
[object] GetInfo() {
194-
return Get-AzureRmResourceGroup -Name $this.Name;
198+
[object] GetInfo([object] $context) {
199+
return Get-AzureRmResourceGroup -Name $this.Name -AzureRmContext $context;
195200
}
196201

197202
[object] Create([CreateParams] $p) {
198203
return New-AzureRmResourceGroup `
199204
-Name $p.Name `
200205
-Location $p.Location `
206+
-AzureRmContext $p.Context `
201207
-WarningAction SilentlyContinue `
202208
-ErrorAction Stop;
203209
}
@@ -215,8 +221,8 @@ class VirtualNetwork: Resource1 {
215221
$this.AddressPrefix = $addressPrefix
216222
}
217223

218-
[object] GetInfo() {
219-
return Get-AzureRmVirtualNetwork -Name $this.Name
224+
[object] GetInfo([object] $context) {
225+
return Get-AzureRmVirtualNetwork -Name $this.Name -AzureRmContext $context
220226
}
221227

222228
[object] Create([CreateParams] $p) {
@@ -225,6 +231,7 @@ class VirtualNetwork: Resource1 {
225231
-Location $p.Location `
226232
-Name $p.Name `
227233
-AddressPrefix $this.AddressPrefix `
234+
-AzureRmContext $p.Context `
228235
-WarningAction SilentlyContinue `
229236
-ErrorAction Stop
230237
}
@@ -243,8 +250,8 @@ class PublicIpAddress: Resource1 {
243250
$this.AllocationMethod = $allocationMethod;
244251
}
245252

246-
[object] GetInfo() {
247-
return Get-AzureRMPublicIpAddress -Name $this.Name;
253+
[object] GetInfo([object] $context) {
254+
return Get-AzureRMPublicIpAddress -Name $this.Name -AzureRmContext $context;
248255
}
249256

250257
[object] Create([CreateParams] $p) {
@@ -254,6 +261,7 @@ class PublicIpAddress: Resource1 {
254261
-Name $p.Name `
255262
-DomainNameLabel $this.DomainNameLabel.ToLower() `
256263
-AllocationMethod $this.AllocationMethod `
264+
-AzureRmContext $p.Context `
257265
-WarningAction SilentlyContinue `
258266
-ErrorAction Stop
259267
}
@@ -266,8 +274,8 @@ class SecurityGroup: Resource1 {
266274
$this.OpenPorts = $OpenPorts;
267275
}
268276

269-
[object] GetInfo() {
270-
return Get-AzureRMSecurityGroup -Name $this.Name
277+
[object] GetInfo([object] $context) {
278+
return Get-AzureRMSecurityGroup -Name $this.Name -AzureRmContext $context
271279
}
272280

273281
[object] Create([CreateParams] $p) {
@@ -294,6 +302,7 @@ class SecurityGroup: Resource1 {
294302
-Location $p.Location `
295303
-Name $p.Name `
296304
-SecurityRules $rules `
305+
-AzureRmContext $p.Context `
297306
-WarningAction SilentlyContinue `
298307
-ErrorAction Stop
299308
}
@@ -305,13 +314,13 @@ class Subnet: AzureObject {
305314

306315
Subnet([string] $name, [VirtualNetwork] $virtualNetwork, [string] $subnetAddressPrefix):
307316
base($name, @($virtualNetwork)) {
308-
$this.VirtualNetwork = $virtualNetwork;
309-
$this.SubnetAddressPrefix = $subnetAddressPrefix;
317+
$this.VirtualNetwork = $virtualNetwork
318+
$this.SubnetAddressPrefix = $subnetAddressPrefix
310319
}
311320

312-
[object] GetInfo() {
313-
$virutalNetworkInfo = $this.VirtualNetwork.GetInfo();
314-
return $virutalNetworkInfo | Get-AzureRmVirtualNetworkSubnetConfig -Name $this.Name;
321+
[object] GetInfo([object] $context) {
322+
$virutalNetworkInfo = $this.VirtualNetwork.GetInfo($context)
323+
return $virutalNetworkInfo | Get-AzureRmVirtualNetworkSubnetConfig -Name $this.Name
315324
}
316325

317326
[object] Create([CreateParams] $p) {
@@ -320,9 +329,11 @@ class Subnet: AzureObject {
320329
-VirtualNetwork $virtualNetworkInfo `
321330
-Name $p.Name `
322331
-AddressPrefix $this.SubnetAddressPrefix;
323-
$virtualNetworkInfo = Set-AzureRmVirtualNetwork -VirtualNetwork $virtualNetworkInfo -ErrorAction Stop
324-
$result = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $virtualNetworkInfo -Name $p.Name
325-
return $result;
332+
$virtualNetworkInfo = Set-AzureRmVirtualNetwork `
333+
-VirtualNetwork $virtualNetworkInfo `
334+
-AzureRmContext $p.Context `
335+
-ErrorAction Stop
336+
return Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $virtualNetworkInfo -Name $p.Name
326337
}
327338
}
328339

@@ -342,8 +353,8 @@ class NetworkInterface: AzureObject {
342353
$this.SecurityGroup = $securityGroup;
343354
}
344355

345-
[object] GetInfo() {
346-
return Get-AzureRMNetworkInterface -Name $this.Name;
356+
[object] GetInfo([object] $context) {
357+
return Get-AzureRMNetworkInterface -Name $this.Name -AzureRmContext $context;
347358
}
348359

349360
[object] Create([CreateParams] $p) {
@@ -357,6 +368,7 @@ class NetworkInterface: AzureObject {
357368
-PublicIpAddressId $publicIpAddressInfo.Id `
358369
-SubnetId $subnetInfo.Id `
359370
-NetworkSecurityGroupId $securityGroupInfo.Id `
371+
-AzureRmContext $p.Context `
360372
-WarningAction SilentlyContinue `
361373
-ErrorAction Stop
362374
}
@@ -386,17 +398,13 @@ class VirtualMachine: AzureObject {
386398
$this.Size = $size;
387399
}
388400

389-
[object] GetInfo() {
390-
return Get-AzureRMVirtualMachine -Name $this.Name;
401+
[object] GetInfo([object] $context) {
402+
return Get-AzureRMVirtualMachine -Name $this.Name -AzureRmContext $context;
391403
}
392404

393405
[object] Create([CreateParams] $p) {
394406
$networkInterfaceInstance = $this.NetworkInterface.GetOrCreate($p);
395407

396-
if (-not $this.Credential) {
397-
$this.Credential = Get-Credential;
398-
}
399-
400408
$vmImage = $this.Images | Where-Object { $_.Name -eq $this.ImageName } | Select-Object -First 1;
401409
if (-not $vmImage) {
402410
throw "Unknown image: " + $this.ImageName;
@@ -437,6 +445,7 @@ class VirtualMachine: AzureObject {
437445
-ResourceGroupName $p.ResourceGroupName `
438446
-Location $p.Location `
439447
-VM $vmConfig `
448+
-AzureRmContext $p.Context `
440449
-WarningAction SilentlyContinue `
441450
-ErrorAction Stop
442451
}
@@ -539,4 +548,4 @@ $images = $staticImages.psobject.Properties | ForEach-Object {
539548
}
540549
}
541550

542-
Export-ModuleMember -Function New-AzVm
551+
Export-ModuleMember -Function New-AzVm

0 commit comments

Comments
 (0)