1616using Microsoft . Azure . Commands . Common . Authentication ;
1717using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
1818using Microsoft . Azure . Commands . Common . Authentication . Models ;
19+ using Microsoft . Azure . Commands . Common . Strategies ;
1920using Microsoft . Azure . Commands . Common . Strategies . Compute ;
2021using Microsoft . Azure . Commands . Common . Strategies . Network ;
2122using Microsoft . Azure . Commands . Common . Strategies . ResourceManager ;
2223using Microsoft . Azure . Commands . Compute . Common ;
2324using Microsoft . Azure . Commands . Compute . Models ;
2425using Microsoft . Azure . Management . Compute ;
2526using Microsoft . Azure . Management . Compute . Models ;
27+ using Microsoft . Azure . Management . Network ;
28+ using Microsoft . Azure . Management . ResourceManager ;
2629using Microsoft . Azure . Management . Storage ;
2730using Microsoft . Azure . Management . Storage . Models ;
31+ using Microsoft . Rest ;
2832using System ;
2933using System . Collections ;
3034using System . Linq ;
3135using System . Management . Automation ;
3236using System . Reflection ;
37+ using System . Threading ;
3338using CM = Microsoft . Azure . Management . Compute . Models ;
3439
3540namespace Microsoft . Azure . Commands . Compute
@@ -38,14 +43,19 @@ namespace Microsoft.Azure.Commands.Compute
3843 [ OutputType ( typeof ( PSAzureOperationResponse ) ) ]
3944 public class NewAzureVMCommand : VirtualMachineBaseCmdlet
4045 {
46+ public const string DefaultParameterSet = "DefaultParameterSet" ;
47+ public const string StrategyParameterSet = "StrategyParameterSet" ;
48+
4149 [ Parameter (
50+ ParameterSetName = DefaultParameterSet ,
4251 Mandatory = true ,
4352 Position = 0 ,
4453 ValueFromPipelineByPropertyName = true ) ]
4554 [ ValidateNotNullOrEmpty ]
4655 public string ResourceGroupName { get ; set ; }
4756
4857 [ Parameter (
58+ ParameterSetName = DefaultParameterSet ,
4959 Mandatory = true ,
5060 Position = 1 ,
5161 ValueFromPipelineByPropertyName = true ) ]
@@ -54,6 +64,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
5464
5565 [ Alias ( "VMProfile" ) ]
5666 [ Parameter (
67+ ParameterSetName = DefaultParameterSet ,
5768 Mandatory = true ,
5869 Position = 2 ,
5970 ValueFromPipeline = true ,
@@ -62,23 +73,24 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
6273 public PSVirtualMachine VM { get ; set ; }
6374
6475 [ Parameter (
76+ ParameterSetName = DefaultParameterSet ,
6577 Position = 3 ,
6678 HelpMessage = "Disable BG Info Extension" ) ]
6779 public SwitchParameter DisableBginfoExtension { get ; set ; }
6880
6981 [ Parameter (
82+ ParameterSetName = DefaultParameterSet ,
7083 Mandatory = false ,
7184 ValueFromPipelineByPropertyName = true ) ]
7285 public Hashtable Tags { get ; set ; }
7386
7487 [ Parameter (
88+ ParameterSetName = DefaultParameterSet ,
7589 Mandatory = false ,
7690 ValueFromPipelineByPropertyName = false ) ]
7791 [ ValidateNotNullOrEmpty ]
7892 public string LicenseType { get ; set ; }
7993
80- public const string StrategyParameterSet = "StrategyParameterSet" ;
81-
8294 [ Parameter (
8395 ParameterSetName = StrategyParameterSet ,
8496 Mandatory = true ) ]
@@ -94,7 +106,7 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
94106 Mandatory = false ) ]
95107 public string SubnetAddressPrefix { get ; } = "192.168.1.0/24" ;
96108
97- [ Parameter ( ParameterSetName = StrategyParameterSet , Mandatory = false ) ]
109+ [ Parameter ( ParameterSetName = StrategyParameterSet , Mandatory = true ) ]
98110 public PSCredential Credential { get ; }
99111
100112 public override void ExecuteCmdlet ( )
@@ -110,6 +122,24 @@ public override void ExecuteCmdlet()
110122 }
111123 }
112124
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+
113143 public void StrategyExecuteCmdlet ( )
114144 {
115145 var resourceGroup = ResourceGroupStrategy . CreateResourceGroupConfig ( Name ) ;
@@ -124,6 +154,19 @@ public void StrategyExecuteCmdlet()
124154 networkInterface ,
125155 Credential . UserName ,
126156 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 ( ) ;
127170 }
128171
129172 public void DefaultExecuteCmdlet ( )
0 commit comments