16
16
using Microsoft . Azure . Commands . Common . Authentication ;
17
17
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
18
18
using Microsoft . Azure . Commands . Common . Authentication . Models ;
19
+ using Microsoft . Azure . Commands . Common . Strategies ;
19
20
using Microsoft . Azure . Commands . Common . Strategies . Compute ;
20
21
using Microsoft . Azure . Commands . Common . Strategies . Network ;
21
22
using Microsoft . Azure . Commands . Common . Strategies . ResourceManager ;
22
23
using Microsoft . Azure . Commands . Compute . Common ;
23
24
using Microsoft . Azure . Commands . Compute . Models ;
24
25
using Microsoft . Azure . Management . Compute ;
25
26
using Microsoft . Azure . Management . Compute . Models ;
27
+ using Microsoft . Azure . Management . Network ;
28
+ using Microsoft . Azure . Management . ResourceManager ;
26
29
using Microsoft . Azure . Management . Storage ;
27
30
using Microsoft . Azure . Management . Storage . Models ;
31
+ using Microsoft . Rest ;
28
32
using System ;
29
33
using System . Collections ;
30
34
using System . Linq ;
31
35
using System . Management . Automation ;
32
36
using System . Reflection ;
37
+ using System . Threading ;
33
38
using CM = Microsoft . Azure . Management . Compute . Models ;
34
39
35
40
namespace Microsoft . Azure . Commands . Compute
@@ -38,14 +43,19 @@ namespace Microsoft.Azure.Commands.Compute
38
43
[ OutputType ( typeof ( PSAzureOperationResponse ) ) ]
39
44
public class NewAzureVMCommand : VirtualMachineBaseCmdlet
40
45
{
46
+ public const string DefaultParameterSet = "DefaultParameterSet" ;
47
+ public const string StrategyParameterSet = "StrategyParameterSet" ;
48
+
41
49
[ Parameter (
50
+ ParameterSetName = DefaultParameterSet ,
42
51
Mandatory = true ,
43
52
Position = 0 ,
44
53
ValueFromPipelineByPropertyName = true ) ]
45
54
[ ValidateNotNullOrEmpty ]
46
55
public string ResourceGroupName { get ; set ; }
47
56
48
57
[ Parameter (
58
+ ParameterSetName = DefaultParameterSet ,
49
59
Mandatory = true ,
50
60
Position = 1 ,
51
61
ValueFromPipelineByPropertyName = true ) ]
@@ -54,6 +64,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
54
64
55
65
[ Alias ( "VMProfile" ) ]
56
66
[ Parameter (
67
+ ParameterSetName = DefaultParameterSet ,
57
68
Mandatory = true ,
58
69
Position = 2 ,
59
70
ValueFromPipeline = true ,
@@ -62,23 +73,24 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
62
73
public PSVirtualMachine VM { get ; set ; }
63
74
64
75
[ Parameter (
76
+ ParameterSetName = DefaultParameterSet ,
65
77
Position = 3 ,
66
78
HelpMessage = "Disable BG Info Extension" ) ]
67
79
public SwitchParameter DisableBginfoExtension { get ; set ; }
68
80
69
81
[ Parameter (
82
+ ParameterSetName = DefaultParameterSet ,
70
83
Mandatory = false ,
71
84
ValueFromPipelineByPropertyName = true ) ]
72
85
public Hashtable Tags { get ; set ; }
73
86
74
87
[ Parameter (
88
+ ParameterSetName = DefaultParameterSet ,
75
89
Mandatory = false ,
76
90
ValueFromPipelineByPropertyName = false ) ]
77
91
[ ValidateNotNullOrEmpty ]
78
92
public string LicenseType { get ; set ; }
79
93
80
- public const string StrategyParameterSet = "StrategyParameterSet" ;
81
-
82
94
[ Parameter (
83
95
ParameterSetName = StrategyParameterSet ,
84
96
Mandatory = true ) ]
@@ -94,7 +106,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
94
106
Mandatory = false ) ]
95
107
public string SubnetAddressPrefix { get ; } = "192.168.1.0/24" ;
96
108
97
- [ Parameter ( ParameterSetName = StrategyParameterSet , Mandatory = false ) ]
109
+ [ Parameter ( ParameterSetName = StrategyParameterSet , Mandatory = true ) ]
98
110
public PSCredential Credential { get ; }
99
111
100
112
public override void ExecuteCmdlet ( )
@@ -110,6 +122,24 @@ public override void ExecuteCmdlet()
110
122
}
111
123
}
112
124
125
+ private sealed class Client : IClient
126
+ {
127
+ public string SubscriptionId { get ; }
128
+
129
+ IAzureContext Context { get ; }
130
+
131
+ public Client ( IAzureContext context )
132
+ {
133
+ Context = context ;
134
+ SubscriptionId = Context . Subscription . Id ;
135
+ }
136
+
137
+ public T GetClient < T > ( )
138
+ where T : ServiceClient < T >
139
+ => AzureSession . Instance . ClientFactory . CreateArmClient < T > (
140
+ Context , AzureEnvironment . Endpoint . ResourceManager ) ;
141
+ }
142
+
113
143
public void StrategyExecuteCmdlet ( )
114
144
{
115
145
var resourceGroup = ResourceGroupStrategy . CreateResourceGroupConfig ( Name ) ;
@@ -124,6 +154,19 @@ public void StrategyExecuteCmdlet()
124
154
networkInterface ,
125
155
Credential . UserName ,
126
156
new System . Net . NetworkCredential ( string . Empty , Credential . Password ) . Password ) ;
157
+
158
+ //
159
+ var client = new Client ( DefaultProfile . DefaultContext ) ;
160
+ var state = virtualMachine
161
+ . GetAsync ( client , new CancellationToken ( ) )
162
+ . GetAwaiter ( )
163
+ . GetResult ( ) ;
164
+ var location = state . GetLocation ( virtualMachine ) ;
165
+ var target = virtualMachine . GetTargetState ( client . SubscriptionId , location ) ;
166
+ var result = virtualMachine
167
+ . CreateOrUpdateAsync ( client , state , target , new CancellationToken ( ) )
168
+ . GetAwaiter ( )
169
+ . GetResult ( ) ;
127
170
}
128
171
129
172
public void DefaultExecuteCmdlet ( )
0 commit comments